简体   繁体   English

如何在编辑值 c# 后更新 dataGridview?

[英]How can i update dataGridview After editing values c#?

i have aa table ("UserPr") and i display the data to gridview, so when i edit values of one cell directly from the datagridview like: changing chexkbox**("canShow")** value from true or false at one row, the changes updates all rows values to the same value and the for the column which the value changed.我有一个表(“UserPr”) ,我将数据显示到 gridview,所以当我直接从 datagridview 编辑一个单元格的值时,例如:在一行中将 chexkbox**(“canShow”)** 值从真或假更改,更改将所有行的值更新为相同的值,并将值更改为列。 在此处输入图像描述 here is my code:-这是我的代码:-

 private void button4_Click(object sender,EventArgs e)
        {
            try
            {
                int UseID = Convert.ToInt32( TXT_ID.Text);
                Boolean CanShow = dataGridView2.Rows[0].Cells[5].Value.Equals(true || false);
                Boolean CanOpen = dataGridView2.Rows[0].Cells[6].Value.Equals(true || false);
                Boolean CanAdd = dataGridView2.Rows[0].Cells[7].Value.Equals(true || false);
                Boolean CanEdit = dataGridView2.Rows[0].Cells[8].Value.Equals(true || false);
                Boolean CanDelete = dataGridView2.Rows[0].Cells[9].Value.Equals(true || false);
                Boolean CanPrint = dataGridView2.Rows[0].Cells[10].Value.Equals(true || false);

                if (TXT_ID.Text != "")
                {
                    foreach (DataGridViewRow row in dataGridView2.Rows)
                    {
                        string constring = @"Data Source = MOHAMEDTHRWAT20\SQLEXPRESS; Initial Catalog = SharpControl; Integrated Security = True";
                        using (SqlConnection con = new SqlConnection(constring))
                        {
                            String updateData = "UPDATE UserPr SET CanShow=@CanShow,CanOpen=@CanOpen,CanAdd=@CanAdd,CanEdit=@CanEdit,CanDelete=@CanDelete,CanPrint=@CanPrint WHERE UseID='" + UseID + "'";
                            SqlCommand update = new SqlCommand(updateData, con);
                            {
                                con.Open();
                                update.Parameters.Add("@CanShow", SqlDbType.Bit).Value= CanShow;
                                update.Parameters.Add("@CanOpen", SqlDbType.Bit).Value= CanOpen;
                                update.Parameters.Add("@CanAdd", SqlDbType.Bit).Value = CanAdd;
                                update.Parameters.Add("@CanEdit", SqlDbType.Bit).Value = CanEdit;
                                update.Parameters.Add("@CanDelete", SqlDbType.Bit).Value = CanDelete;
                                update.Parameters.Add("@CanPrint", SqlDbType.Bit).Value = CanPrint;
                                update.ExecuteNonQuery();
                                con.Close();
                            }  
                        }
                    }
                    //updateaut();

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

I have a project like that.我有一个这样的项目。 The way I did, I clear the datagridview, than filled with data after clearing.^我这样做的方式是清除datagridview,而不是清除后填充数据。^

private void ClearDataGridView()
    {
        dgv.Rows.Clear();
    }

public static void AddDataToGridView(System.Windows.Forms.DataGridView dataGrid, string column1, string column2)
    {

        dataGrid.Rows.Add(column1, column2);
    }

The values from the columns I would fetch from database source我将从数据库源中获取的列中的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM