好的,所以我有这个datagridview可以通过数据源访问数据库,并且很好,但是当我从表单向表中添加另一行并且返回到具有datagridview的表单时,它仅显示前一个值,而不是新添加的值。
即使在我重新启动/重新运行应用程序之后。它一直在发生。
顺便说一句,我正在通过以下链接使用自动筛选功能
AutoFilter in DataGridView
这是.cs文件:
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;
}
}
}
这是执行添加功能的表格
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();
}
}
}
最佳答案
DataGridView.Refresh
和DataGridView.Update
是从Control继承的方法。因此,要做的工作就是重画控件,这就是为什么不出现新行的原因。
您需要将数据重新绑定到DataGridView
,如下所示:
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = null;
dataGridView1.DataSource = dtData; //add you data here
}
关于c# - 数据网格 View 未以Win形式C#显示更新的数据?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37673943/