hello_ i searched it in forums and google but i couldnt sokve it.
i have a datagrid view on my form.
when I select a row and update it, how do I keep focus on the selected row after update instead of it going to the first row .
i want when i click on every row _it remain in selected state after update
this is my show button:
private void show_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=MA-PC;Initial Catalog=bookstore;Integrated Security=True";
con.Open();
SqlCommand com = new SqlCommand();
com.CommandType = CommandType.Text;
com.Connection = con;
com.CommandText = "select * from book";
SqlDataAdapter da = new SqlDataAdapter(com);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
this is my code:(Update)
private void Update_click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=MA-PC;Initial Catalog=bookstore;Integrated Security=True";
con.Open();
SqlCommand com = new SqlCommand();
com.CommandType = CommandType.Text;
com.Connection = con;
com.CommandText = "update book set bookname=@bookname,publisher=@publisher where book_code=@book_code";
com.Parameters.AddWithValue("@bookname", txt1.Text);
com.Parameters.AddWithValue("@publisher", txt2.Text);
com.Parameters.AddWithValue("@book_code",Int32.Parse( dataGridView1["book_code",dataGridView1.CurrentRow.Index].Value.ToString()));
com.ExecuteNonQuery();
con.Close();
show_Click(null, null);
gotoselect();
}
this is a function:
public void gotoselect()
{
1 DataGridViewRow rowToSelect = this.dataGridView1.CurrentRow;
2 rowToSelect.Selected = true;
3 rowToSelect.Cells[0].Selected = true;
4 this.dataGridView1.CurrentCell = rowToSelect.Cells[0];
5 this.dataGridView1.BeginEdit(true);
}
I changed
selectionmode=cellselec
t and it answered, but I could not solve problem yet. Is it going to the first row again?
Please help.