【问题标题】:Data grid view not displaying the updated data in win form c#?数据网格视图未以win表单c#显示更新的数据?
【发布时间】:2016-10-07 00:51:57
【问题描述】:

好的,所以我有这个通过数据源连接到数据库的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 的 DataSource 属性在哪里绑定到 bindingsource 吗?
  • @SebastianSchulz 我通过将数据库添加到表单设计器本身的 gridview 来动态地做到这一点
  • 您在哪里添加了新值?
  • @TrầnAnhNam 以与此特定形式分开的另一种形式完成。我也为此添加了代码。
  • 你为logCallBindingSource增值了吗?

标签: c# winforms data-binding datagridview


【解决方案1】:

DataGridView.RefreshDataGridView.Update 是从 Control 继承的方法。所以有工作与重绘控件有关,这就是为什么不出现新行的原因。

您需要将数据重新绑定到DataGridView,如下所示:

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

【讨论】:

  • 那么,我应该在表单中添加一个按钮,点击事件将使用此代码,还是应该将它放在其他地方?
  • 这是您的选择。您可以在将新行添加到 datagridview 后立即执行此操作
  • 我尝试了你的建议,代码工作没有任何错误,但数据网格视图中仍然没有更新
  • 请更新问题并添加您更改的代码
  • 根据您的要求编辑
猜你喜欢
  • 1970-01-01
  • 2013-12-06
  • 2021-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多