更新后,请继续关注datagrid的选定行 [英] keep focus on datagrid's selected row after update

查看:95
本文介绍了更新后,请继续关注datagrid的选定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hello_我在论坛和google中搜索过它但是我无法解决它。

i在我的表单上有一个数据网格视图。

当我选择一行并更新它时,如何在更新后将焦点保持在选定的行而不是第一行。

i当我点击每一行时_想要在更新后保持选定状态



这是我的显示按钮:

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);
}



我改变了 selectionmode = cellselec t并且它回答了,但我还是解决不了问题。是否会再次进入第一行?



请帮助。


I changed selectionmode=cellselect and it answered, but I could not solve problem yet. Is it going to the first row again?

Please help.

推荐答案

焦点的概念不适用于行,它始终表示键盘焦点,仅适用于整个控件。至于行,它们被选中。也就是说,现在很容易找到解决方案:

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.selected.aspx [ ^ ]。



-SA
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


这篇关于更新后,请继续关注datagrid的选定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆