i have adatagridview showinfg table content ,when i am trying to change a cell value at column directly from the datagridview
the update change all rows for the columns to the new value.
ex:- if checked row 1 column "CanShow " to "true" . all rows for the column "CanShow "aslo change to "true "
i just want to update only the cells i changed
here is my code :-
C#
private void button4_Click(object sender, EventArgs e)
{
try
{
if (TXT_ID.Text != "")
{
foreach (DataGridViewRow Row in dgv.Rows)
{
{
var conn2 = new SqlConnection(@"Data Source = MOHAMEDTHRWAT20\SQLEXPRESS; Initial Catalog = SharpControl; Integrated Security = True");
var cmd2 = new SqlCommand("", conn2);
cmd2.CommandText = "update UserPr set UseID=@UseID,UseName=@UseName,ScreenId=@ScreenId,ScreenName=@ScreenName, CanShow =@CanShow, CanOpen = @CanOpen , CanEdit = @CanEdit ,CanDelete = @CanDelete , CanPrint = @CanPrint WHERE UseID='" + TXT_ID.Text + "'";
cmd2.Parameters.AddWithValue("@UseID", dgv.Rows[0].Cells[1].Value.ToString());
cmd2.Parameters.AddWithValue("@UseName", dgv.Rows[0].Cells[2].Value.ToString());
cmd2.Parameters.AddWithValue("@ScreenId", dgv.Rows[0].Cells[3].Value.ToString());
cmd2.Parameters.AddWithValue("@ScreenName", dgv.Rows[0].Cells[4].Value.ToString());
cmd2.Parameters.AddWithValue("@CanShow", dgv.Rows[0].Cells[5].Value.Equals(true || false));
cmd2.Parameters.AddWithValue("@CanOpen", dgv.Rows[0].Cells[6].Value.Equals(true || false));
cmd2.Parameters.AddWithValue("@CanAdd", dgv.Rows[0].Cells[7].Value.Equals(true || false));
cmd2.Parameters.AddWithValue("@CanEdit", dgv.Rows[0].Cells[8].Value.Equals(true || false));
cmd2.Parameters.AddWithValue("@CanDelete", dgv.Rows[0].Cells[9].Value.Equals(true || false));
cmd2.Parameters.AddWithValue("@CanPrint", dgv.Rows[0].Cells[10].Value.Equals(true || false));
conn2.Open();
cmd2.ExecuteNonQuery();
access();
conn2.Close();
}
}
updateaut();
}
MessageBox.Show("update sucess");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Vishal JoshiRank #377 · 6.7K reputationPosted Sep 22, 2022, 10:32 PM EDT
Hello Ahmed,
Issue due to you hardcode the row index to 0. it should be dynamic.
Wrong
cmd2.Parameters.AddWithValue("@UseID", dgv.Rows[0].Cells[1].Value.ToString());Right
cmd2.Parameters.AddWithValue("@UseID", dgv.Rows[Index].Cells[1].Value.ToString());Also, You can use dataGridView_CellContentClick event to update the checkbox change value. please find below the code for the same.
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { var Value = (bool)dataGridView1.CurrentCell.Value; // Write code to update value to database //call rebind the grid }Rajanikant HawaldarRank #110 · 20.2K reputationPosted Sep 23, 2022, 12:29 AM EDT
1)cmd2.Parameters.AddWithValue("@UseID", Row.Cells[1].Value.ToString());
since your having foreach (DataGridViewRow Row in dgv.Rows) loop you should use Row obj
2) Share your code which you wrote for datagridview's checkbox column
Amit MohantyRank #112 · 20K reputationPosted Sep 23, 2022, 1:49 AM EDT
Hey, you have use foreach loop so your code should be
row.Cells[2].Value.ToString() not dgv.Rows[0].Cells[1].Value.ToString().
private void button4_Click(object sender, EventArgs e) { foreach (DataGridViewRow row in dataGridView1.Rows) { bool isSelected = Convert.ToBoolean(row.Cells["canShow"].Value); if (isSelected) { // your code for db operation } } }Please check the attached code, this may help you.
Sachin SinghRank #66 · 26.2K reputationPosted Sep 23, 2022, 2:29 AM EDT
use this
private void button4_Click(object sender, EventArgs e) { try { if (TXT_ID.Text != "") { foreach (DataGridViewRow Row in dgv.Rows) { { var conn2 = new SqlConnection(@"Data Source = MOHAMEDTHRWAT20\SQLEXPRESS; Initial Catalog = SharpControl; Integrated Security = True"); var cmd2 = new SqlCommand("", conn2); cmd2.CommandText = "update UserPr set UseID=@UseID,UseName=@UseName,ScreenId=@ScreenId,ScreenName=@ScreenName, CanShow =@CanShow, CanOpen = @CanOpen , CanEdit = @CanEdit ,CanDelete = @CanDelete , CanPrint = @CanPrint WHERE UseID='" + TXT_ID.Text + "'"; cmd2.Parameters.AddWithValue("@UseID", row.Cells[1].Value.ToString()); cmd2.Parameters.AddWithValue("@UseName", row.Cells[2].Value.ToString()); cmd2.Parameters.AddWithValue("@ScreenId", rowCells[3].Value.ToString()); cmd2.Parameters.AddWithValue("@ScreenName", row.Cells[4].Value.ToString()); cmd2.Parameters.AddWithValue("@CanShow", row.Cells[5].Value.Equals(true || false)); cmd2.Parameters.AddWithValue("@CanOpen", row.Cells[6].Value.Equals(true || false)); cmd2.Parameters.AddWithValue("@CanAdd", dgv.Rows[0].Cells[7].Value.Equals(true || false)); cmd2.Parameters.AddWithValue("@CanEdit", row.Cells[8].Value.Equals(true || false)); cmd2.Parameters.AddWithValue("@CanDelete", row.Cells[9].Value.Equals(true || false)); cmd2.Parameters.AddWithValue("@CanPrint", row.Cells[10].Value.Equals(true || false)); conn2.Open(); cmd2.ExecuteNonQuery(); access(); conn2.Close(); } } updateaut(); } MessageBox.Show("update sucess"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }