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


This Content is from Stack Overflow. Question asked by Mohamed Thrwat

i have a a 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.
enter image description here
here is my code :-

 private void button4_Click(object sender,EventArgs e)
                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 = MOHAMEDTHRWAT20SQLEXPRESS; 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);
                                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;

            catch (Exception ex)


I have a project like that.
The way I did, I cleared the datagridview, than I filled with data.

private void ClearDataGridView()

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

This Question was asked in StackOverflow by Mohamed Thrwat and Answered by Belarmino Vicenzo It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?