Datagridview选中某行,点button触发问题。

2025-04-15 16:21:20
推荐回答(1个)
回答1:

抱歉前段时间没有登录“百度知道”


        private void Form1_Load(object sender, EventArgs e)
        {
            DataGridViewRow row1 = new DataGridViewRow();
            row1.CreateCells(dataGridView1);
            row1.SetValues("RN1", "LC1", "", "", "IT1", "", "");
            dataGridView1.Rows.Add(row1);

            DataGridViewRow row2 = new DataGridViewRow();
            row2.CreateCells(dataGridView1);
            row2.SetValues("RN2", "LC2", "", "", "IT2", "", "");
            dataGridView1.Rows.Add(row2);

            DataGridViewRow row3 = new DataGridViewRow();
            row3.CreateCells(dataGridView1);
            row3.SetValues("RN3", "LC3", "", "", "IT3", "", "");
            dataGridView1.Rows.Add(row3);

            DataGridViewRow row4 = new DataGridViewRow();
            row4.CreateCells(dataGridView1);
            row4.SetValues("RN4", "LC4", "", "", "IT4", "", "");
            dataGridView1.Rows.Add(row4);

        }
        private DataGridViewRow CurrentRow;
        private void button1_Click(object sender, EventArgs e)
        {
            if (CurrentRow != null)
                CurrentRow.Cells[2].Value = DateTime.Now.ToString();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (CurrentRow != null)
                CurrentRow.Cells[3].Value = DateTime.Now.ToString();
        }
        //方法一
        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int index = e.RowIndex;
            CurrentRow = (sender as DataGridView).Rows[index];
        }
        //方法二
        private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            //if (e.Row.Selected)
            //    CurrentRow = e.Row;
            //else
            //    CurrentRow = null;
        }

你试试不知是不是你要的效果