【问题标题】:My dataGridView isnt updating when i have edited a row of values in a seperate form当我以单独的形式编辑一行值时,我的 dataGridView 没有更新
【发布时间】:2016-04-01 13:55:02
【问题描述】:

我在 MainForm 中有一个 dataGridView,它在表单加载时列出所有相关数据。

当我想去编辑一行时。我选择它并按编辑,然后它会加载一个 EditForm。在这里我可以编辑数据并保存。

信息已成功编辑并保存,但dataGridView没有更新。

是否有我没有看到的 autoRefresh 属性或关闭编辑表单时刷新它的方法?

主窗体

    private void EditAdminBtn_Click(object sender, EventArgs e)
    {

    EditAdminForm Admin = new EditAdminForm();

    Admin.idTxt.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
    Admin.usernameTxt.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
    Admin.firstnameTxt.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
    Admin.surnameTxt.Text = this.dataGridView1.CurrentRow.Cells[3].Value.ToString();
    Admin.emailTxt.Text = this.dataGridView1.CurrentRow.Cells[4].Value.ToString();
    Admin.statusCombo.Text = this.dataGridView1.CurrentRow.Cells[6].Value.ToString();

    Admin.ShowDialog();

    }

    public void MainForm_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'student_CBDataSetAdmin.Admin' table. You can move, or remove it, as needed.
        this.adminTableAdapter.Fill(this.student_CBDataSetAdmin.Admin);

    }

编辑表单

    private void SaveBtn_Click(object sender, EventArgs e)
    {
    //SQL Connection and SQL for updating admin information
    SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0; AttachDbFilename=C:\Users\Donald\Documents\Visual Studio 2013\Projects\DesktopApplication\DesktopApplication\Student_CB.mdf ;Integrated Security=True");
    SqlDataAdapter sda3 = new SqlDataAdapter("UPDATE Admin set Admin_Username='" + this.usernameTxt.Text + "' , Admin_FName='" + this.firstnameTxt.Text + "' , Admin_SName='" + this.surnameTxt.Text + "' , Admin_Email='" + this.emailTxt.Text + "', Admin_Status='" + this.statusCombo.Text + "' WHERE Admin_ID='" + this.idTxt.Text + "'", con);
    DataTable dt3 = new DataTable();
    sda3.Fill(dt3);

    MessageBox.Show("Information Successfully Updated!");

    dt3.Clear();

    this.Close();

    }

THIS IS WHAT IT LOOKS LIKE, GRIDVIEW SOURCE AND BINDING AT THE BOTTOM

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

    问题是您没有发送任何对数据绑定对象或其他对象的引用,而是将值从 datagridView 复制到弹出窗口中,然后您没有对它做任何事情。

    您需要在关闭弹出窗口时返回 dataTable,或者手动调用 dataAdapter 的刷新。第二个选项更简单,尽管它需要再访问一次数据库。

    在您的 EditAdminBtn_Click 末尾添加` 编辑:根据您的屏幕截图,我看到您使用了 bindingSource。刷新 dataTable 后,您可以重置绑定

    this.adminTableAdapter.Fill(this.student_CBDataSetAdmin.Admin);
    AdminBindingSource.ResetBindings(false);
    

    【讨论】:

    • 我已将此添加到 editadmin 按钮的末尾。现在我必须为 dataTable 创建一个返回值吗?
    • 通常你不会,因为你已经在更新数据库中的数据,而你只是在编辑完成后从数据库中刷新
    • 问题是datagridview实际上并没有刷新。所以已经进行了编辑,它显示在我的表中,并在数据库中显示了已经进行了编辑。但是在编辑表单关闭后打开 MainForm 时。它没有显示新值,只显示旧值。
    • 你能实际发布更多代码吗,如何将 dataGridView 绑定到值?
    • 我刚刚使用工具箱创建了我的datagridview,它似乎添加了自己的绑定
    猜你喜欢
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 2016-05-12
    • 1970-01-01
    相关资源
    最近更新 更多