0
点赞
收藏
分享

微信扫一扫

C#中中模拟DataGridView的CellClick事件_dataGridView1_CellClick


/// <summary>
    /// Refresh 回到首行并且执行时间
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button2_Click(object sender, EventArgs e)
    {
            dataGridView1_CellClick(dataGridView1, new DataGridViewCellEventArgs(0,0));
     }


让某一行选中并且执行事件。




1. //假设dgv是一个DataGridView, 我要把第三行设置为当前行。  
2. //index是有0开始的。  
3. //这里只是把那行highlight。可有可没  
4. dgv.Rows[2].Selected = true;  
5. //这是主要的地方。这行后,CurrentRow就是第三行了。  
6. dgv.CurrentCell = dgv.Rows[2].Cells[0];

选中行


1.  private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.CurrentRow.Index != 0)
            {
                DataGridView dgv = sender as DataGridView;


                if (e != null)
                {
                    dataGridView1.Rows[e.RowIndex].Selected = true;
                }

  1.    

1.  public partial class Form1 : Form
    {
        private readonly string _connectionString;
        private int selectedRowIndex = 0; //声明一个成员变量作为行号

  1. 获取数据的时候绑定一下


            dataGridView1.Rows[selectedRowIndex].Selected = true;

  1. 赋值是这时候

4.  private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                DataGridView dgv = sender as DataGridView;
                selectedRowIndex = dgv.CurrentCell.RowIndex;
                if (e != null)
                {
                    dataGridView1.Rows[e.RowIndex].Selected = true;
                }


举报

相关推荐

0 条评论