【问题标题】:Refreshing dataGridView items on adding or deleting entry in database?在数据库中添加或删除条目时刷新dataGridView项目?
【发布时间】:2013-10-16 16:17:41
【问题描述】:

我正在 Winforms 中向数据库添加、删除和更新数据。我在所有添加、删除和更新表单上都有 gridview。 单击“删除”按钮删除记录后,删除的记录应立即从 dataGridView 中删除。

////请注意 DATABIND 给出错误,即缺少使用或程序集引用。

代码背后:

    private void btnDelete_Click(object sender, EventArgs e)
    {

        if (txtIDD.Text == "")
        {
            MessageBox.Show("Please fill ID no. of record to Delete", "Important Message");

        }
        else
        {
            try
            {
                OleDbCommand Cmd = new OleDbCommand();
                Cmd.Connection = conn;
                conn.Open();
                Cmd.CommandText = "DELETE FROM AddressBook WHERE ID="+txtIDD.Text;
                Cmd.CommandType = CommandType.Text;
                Cmd.ExecuteNonQuery();
                Cmd.Connection.Close();
                conn.Close();
                dataGridView3.Update();
                MessageBox.Show("Delete Succesfull");
            }
            catch (System.Exception err)
            {
                  dataGridView3.DataSource = dt;
                  dataGridView3.DataBind();
                  dataGridView3.Update();

                this.label27.Visible = true;
                this.label27.Text = err.Message.ToString();
            }
        }

    }

【问题讨论】:

  • 此代码易受 SQL 注入攻击
  • 只是我个人的意见,但是如果网格包含应该删除的数据,我会颠倒逻辑:用户输入文本,您在网格中搜索相关记录,执行在数据库中删除,然后直接从网格中删除记录或从底层绑定数据源中删除记录。 (额外的好处是您还可以直接对选定的行执行删除操作)

标签: c# winforms gridview


【解决方案1】:

在添加、更新和删除等操作后再次绑定网格以从数据库中获取值。

private void btnDelete_Click(object sender, EventArgs e)
{

    if (txtIDD.Text == "")
    {
        MessageBox.Show("Please fill ID no. of record to Delete", "Important Message");

    }
    else
    {
        try
        {
            OleDbCommand Cmd = new OleDbCommand();
            Cmd.Connection = conn;
            conn.Open();
            Cmd.CommandText = "DELETE FROM AddressBook WHERE ID="+txtIDD.Text;
            Cmd.CommandType = CommandType.Text;
            Cmd.ExecuteNonQuery();
            Cmd.Connection.Close();
            conn.Close();                

            **//Call a method that binds the grid or get DataTable from database and bind it like this**
             dataGridView3.DataSource = dt;               
            dataGridView3.Update();

            MessageBox.Show("Delete Succesfull");
        }
        catch (System.Exception err)
        {
            this.label27.Visible = true;
            this.label27.Text = err.Message.ToString();
        }
    }
}

【讨论】:

  • 嘿,兄弟,我正在制作 Windows 应用程序。 Databind() 在那里不起作用:(它给出了使用或程序集引用丢失的错误
  • Remove DataBind() 分配数据源就够了,但在分配给数据源之前获取最新数据
  • 我已经使用该函数来获取更新的数据 :)))) 它工作正常
  • 删除后调用该方法获取网格的最新变化
【解决方案2】:

创建一个可以重新加载网格的函数

private method ReloadGrid()
{
   // first load your datatable/dt then set it as the datasource of your grid
   dataGridView3.DataSource = dt;
   dataGridView3.DataBind();

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-14
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    • 2014-05-05
    • 2013-05-01
    相关资源
    最近更新 更多