2016-06-07 3 views
2

Okay, ich habe diese Datagridview, die mit der Datenbank in Zugriff über die Datenquelle verbunden ist und es ist in Ordnung, aber wenn ich eine andere Zeile in der Tabelle aus einem Formular hinzufügen, und wenn ich zurück zu dem Formular mit dem Datagridview es zeigt nur die vorherigen Werte, nicht die neu hinzugefügten Werte.Datenrasteransicht, die die aktualisierten Daten in Win Form C# nicht anzeigt?

Sogar nachdem ich die Anwendung neu starten/erneut ausführen. Es passiert weiter.

Btw, ich bin mit einem Autofilter-Funktion, indem Sie den Link AutoFilter in DataGridView

Hier ist die CS-Datei:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using DataGridViewAutoFilter; 

namespace Call_Logger 
{ 
public partial class AutoFilter : Form 
{ 
    public AutoFilter() 
    { 
     InitializeComponent(); 

    } 

    private void AutoFilter_Load(object sender, EventArgs e) 
    { 
     // TODO: This line of code loads data into the 'datasetFilter.LogCall' table. You can move, or remove it, as needed. 

     this.logCallTableAdapter.Fill(this.datasetFilter.LogCall); 

    } 

    private void showAllLabel_Click(object sender, EventArgs e) 
    { 
     DataGridViewAutoFilterTextBoxColumn.RemoveFilter(dataGridView1); 
    } 

    private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) 
    { 

     String filterStatus = DataGridViewAutoFilterColumnHeaderCell.GetFilterStatus(dataGridView1); 
     if (String.IsNullOrEmpty(filterStatus)) 
     { 
      showAllLabel.Visible = false; 
      filterStatusLabel.Visible = false; 
     } 
     else 
     { 
      showAllLabel.Visible = true; 
      filterStatusLabel.Visible = true; 
      filterStatusLabel.Text = filterStatus; 
     } 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     dataGridView1.DataSource = null; 
     dataGridView1.DataSource = logCallBindingSource; 
    } 


} 
} 

Hier ist die Form, die die Funktionalität hinzufügen, führt

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Configuration; 
using System.Data; 
using System.Data.OleDb; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace Call_Logger 
{ 
public partial class AddingFunctionality : Form 
{ 
    private OleDbConnection con = new OleDbConnection(); 
    public AddingFunctionality() 
    { 
     InitializeComponent(); 
     con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString(); 
    } 

    private void btn_Save_Click(object sender, EventArgs e) 
    { 



     if (comboCallLogBy.Text == "") 
     { 
      comboCallLogBy.BackColor = Color.LightSalmon; 
      MessageBox.Show("Name of the person who is logging the call is required", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      comboCallLogBy.Focus(); 
      return; 
     } 

     if (comboCallType.Text == "") 
     { 
      comboCallType.BackColor = Color.LightSalmon; 
      MessageBox.Show("Specification of the type of call is required", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      comboCallType.Focus(); 
      return; 
     } 

     if (comboCallLocation.Text == "") 
     { 
      comboCallLocation.BackColor = Color.LightSalmon; 
      MessageBox.Show("Location of the call is required", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      comboCallLocation.Focus(); 
      return; 
     } 

     if (txtIncidentNumber.Text == "") 
     { 
      txtIncidentNumber.BackColor = Color.LightSalmon; 
      MessageBox.Show("Please enter the incident number", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      txtIncidentNumber.Focus(); 
      return; 
     } 
     else { 
      decimal OutNumber; 
      if (!decimal.TryParse(txtIncidentNumber.Text, out OutNumber)) 
      { 
       txtIncidentNumber.BackColor = Color.LightSalmon; 
       MessageBox.Show("Value for Incident number should be numerical", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information); 
       txtIncidentNumber.Focus(); 
       return; 

      } 

     } 

     if (comboCaller.Text == "") 
     { 
      comboCaller.BackColor = Color.LightSalmon; 
      MessageBox.Show("Name of the caller is required", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      comboCaller.Focus(); 
      return; 
     } 

     if (comboCallFor.Text == "") 
     { 
      comboCallFor.BackColor = Color.LightSalmon; 
      MessageBox.Show("Name of person the call is made for is required", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      comboCallFor.Focus(); 
      return; 
     } 

     if (comboAssignedBy.Text == "") 
     { 
      comboAssignedBy.BackColor = Color.LightSalmon; 
      MessageBox.Show("Name of the person assigning call is required", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      comboAssignedBy.Focus(); 
      return; 
     } 

     if (comboForwardTo.Text == "") 
     { 
      comboForwardTo.BackColor = Color.LightSalmon; 
      MessageBox.Show("Name of the person call is forwarded to is required", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      comboForwardTo.Focus(); 
      return; 
     } 

     if (comboContactPerson.Text == "") 
     { 
      comboContactPerson.BackColor = Color.LightSalmon; 
      MessageBox.Show("Name of the person contacting is required", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      comboContactPerson.Focus(); 
      return; 
     } 

     if (comboClosedBy.Text == "") 
     { 
      comboClosedBy.BackColor = Color.LightSalmon; 
      MessageBox.Show("Name of the person call closed by is required", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      comboClosedBy.Focus(); 
      return; 
     } 

     if (comboStatus.Text == "") 
     { 
      comboStatus.BackColor = Color.LightSalmon; 
      MessageBox.Show("Status of the call is required", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      comboStatus.Focus(); 
      return; 
     } 
     try 
     { 

      con.Open(); 
      OleDbCommand command = new OleDbCommand(); 
      command.Connection = con; 
      command.CommandText = ("insert into LogCall (CallLogBy, CallLogDate, CallLogTime, CallType, CallLocation, ProblemDescription1, ProblemDescription2, IncidentNo, Caller, CallFor, AssignedBy, AssignedDate, AssignedTime, ForwardTo, ContactPerson, ForwardDate, ForwardTime, ActionTaken1, ActionTaken2, NextStep1, NextStep2, ClosedBy, CloseDate, CloseTime, Remarks1, Remarks2, Status) values ('" + comboCallLogBy.Text + "','" + dateTimePicker1.Text + "','" + dateTimePicker2.Text + "','" + comboCallType.Text + "','" + comboCallLocation.Text + "','"+ textBox1.Text + "','" + textBox2.Text + "','" + txtIncidentNumber.Text + "','" + comboCaller.Text + "','" + comboCallFor.Text + "','" + comboAssignedBy.Text + "','" + dateTimePicker3.Text + "','" + dateTimePicker4.Text + "','" + comboForwardTo.Text + "','" + comboContactPerson.Text + "','" + dateTimePicker5.Text + "','" + dateTimePicker6.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + comboClosedBy.Text + "','" + dateTimePicker7.Text + "','" + dateTimePicker8.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + comboStatus.Text + "')"); 
      command.ExecuteNonQuery(); 

      MessageBox.Show("Data saved successfully", "Record Added", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      con.Dispose(); 
      this.Hide(); 
      Dashboard dboard = new Dashboard(); 
      dboard.ShowDialog(); 
      con.Close(); 
     } 

     catch (Exception ex) 
     { 
      MessageBox.Show("Error " + ex); 
      con.Close(); 
     } 




    } 

    private void AddingFunctionality_Load(object sender, EventArgs e) 
    { 
     try 
     { 
      con.Open(); 
      OleDbCommand command = new OleDbCommand(); 
      command.Connection = con; 
      string query = "select * from MiscData"; 
      //string query1 = "select * from Try"; 
      command.CommandText = query; 
      //command.CommandText = query1; 

      OleDbDataReader reader = command.ExecuteReader(); 
      while (reader.Read()) 
      { 
       comboCallLogBy.Items.Add(reader["CallLoggedBy"].ToString()); 
       //comboCallType.Items.Add(reader["CallType"].ToString()); 
       //comboCallLocation.Items.Add(reader["CallLocation"].ToString()); 
       comboCaller.Items.Add(reader["Employees"].ToString()); 
       comboCallFor.Items.Add(reader["Employees"].ToString()); 
       comboAssignedBy.Items.Add(reader["Employees"].ToString()); 
       comboForwardTo.Items.Add(reader["Employees"].ToString()); 
       comboContactPerson.Items.Add(reader["Employees"].ToString()); 
       comboClosedBy.Items.Add(reader["Employees"].ToString()); 
       //comboStatus.Items.Add(reader["Status"].ToString()); 

      } 
      con.Close(); 

     } 

     catch (Exception ex) 
     { 
      MessageBox.Show("Error " + ex); 

     } 

     try 
     { 
      con.Open(); 
      OleDbCommand command = new OleDbCommand(); 
      command.Connection = con; 
      string query = "select * from CallType"; 

      command.CommandText = query; 


      OleDbDataReader reader = command.ExecuteReader(); 
      while (reader.Read()) 
      { 

       comboCallType.Items.Add(reader["CallingType"].ToString()+ " " + reader["CallDesc"].ToString()); 

      } 
      con.Close(); 

     } 

     catch (Exception ex) 
     { 
      MessageBox.Show("Error " + ex); 

     } 

     try 
     { 
      con.Open(); 
      OleDbCommand command = new OleDbCommand(); 
      command.Connection = con; 
      string query = "select * from Location"; 

      command.CommandText = query; 


      OleDbDataReader reader = command.ExecuteReader(); 
      while (reader.Read()) 
      { 

       comboCallLocation.Items.Add(reader["LocationType"].ToString() + " " + reader["Description"].ToString()); 

      } 
      con.Close(); 

     } 

     catch (Exception ex) 
     { 
      MessageBox.Show("Error " + ex); 

     } 


     try 
     { 
      con.Open(); 
      OleDbCommand command = new OleDbCommand(); 
      command.Connection = con; 
      string query = "select * from Status"; 

      command.CommandText = query; 


      OleDbDataReader reader = command.ExecuteReader(); 
      while (reader.Read()) 
      { 

       comboStatus.Items.Add(reader["StatusType"].ToString() + " " + reader["Description"].ToString()); 

      } 
      con.Close(); 

     } 

     catch (Exception ex) 
     { 
      MessageBox.Show("Error " + ex); 

     } 



    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     this.Hide(); 
     Dashboard Add = new Dashboard(); 
     Add.ShowDialog(); 
    } 








} 
} 
+0

Können Sie uns zeigen, wo die Datasource Eigenschaft des Datagridview an Bindingsource gebunden ist? –

+0

@SebastianSchulz Ich habe das dynamisch getan, indem ich die Datenbank zur Gridview im Formular-Designer selbst hinzugefügt habe. –

+0

Wo haben Sie die neuen Werte hinzugefügt? –

Antwort

0

DataGridView.Refresh und DataGridView.Update sind Methoden, die von Control geerbt werden. Es ist also mit dem Neuzeichnen des Steuerelements zu tun, weshalb neue Zeilen nicht erscheinen.

Sie müssen wie unten Daten DataGridView erneut zu binden:

private void button1_Click(object sender, EventArgs e) 
{ 
    dataGridView1.DataSource = null; 
    dataGridView1.DataSource = dtData; //add you data here 
} 
+0

Also, sollte ich eine Schaltfläche zu dem Formular hinzufügen, in dem das click -Ereignis diesen Code verwenden würde oder sollte ich es woanders platzieren ? –

+0

Sie haben die Wahl. Sie können dies tun, nachdem Sie eine neue Zeile zu "datagridview" hinzufügen. –

+0

Ich habe versucht, was Sie vorgeschlagen und der Code funktioniert ohne Fehler, aber es gibt noch kein Update in der Datenansicht –

Verwandte Themen