Click here to Skip to main content
15,885,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);   //for refresh
            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=cellselect and it answered, but I could not solve problem yet. Is it going to the first row again?

Please help.
Posted
Updated 6-May-13 11:17am
v9

1 solution

The notion of "focus" is not applicable to the rows, it always means "keyboard focus" and is only applicable to the whole controls. As to the rows, they are selected. That said, it's easy to find a solution now:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.selected.aspx[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900