在C#datagridview中我遇到了问题。更新记录时不刷新。在我的代码下面 [英] In C# datagridview I am getting problem. While updating records not refreshing. Below my code

查看:104
本文介绍了在C#datagridview中我遇到了问题。更新记录时不刷新。在我的代码下面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void button1_Click(object sender, EventArgs e)
       {
           con.Open();

           SqlDataAdapter sda = new SqlDataAdapter("UPDATE [dbo].[companyDB] SET[compname] = '" + bxcpname.Text + "' WHERE compcode='" + recbox.Text + "'", con);
           sda.SelectCommand.ExecuteNonQuery();
           MessageBox.Show("Record Updated");
           con.Close();
           dataload();

       }




public void dataload()
        {
            con.Open();
            SqlDataAdapter sda = new SqlDataAdapter("SELECT [compcode],[compname] FROM[dbo].[companyDB]", con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            foreach (DataRow item in dt.Rows)
            {
                int n = dataGridView1.Rows.Add();
                dataGridView1.Rows[n].Cells[0].Value = item["compcode"].ToString();
                dataGridView1.Rows[n].Cells[1].Value = item["compname"].ToString();
            }
            con.Close();





我尝试过:



我写了一个函数来loaddata agian。它正在努力补充。只更新问题。



What I have tried:

I wrote one function to loaddata agian. it is working in adding. Updating only problem.

推荐答案

也许你需要这个:
var cb = new SqlCommandBuilder(sda);
sda.DeleteCommand = cb.GetDeleteCommand(true);
sda.UpdateCommand = cb.GetUpdateCommand(true);
sda.InsertCommand = cb.GetInsertCommand(true);


这篇关于在C#datagridview中我遇到了问题。更新记录时不刷新。在我的代码下面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆