简体   繁体   中英

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. 在此处输入图像描述 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.^

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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