日期:2014-05-18  浏览次数:20921 次

datagridview选中多个单元格时触发什么事件呢?我想实现选中多个单元格时,得到第一行与最后一行的行号
datagridview选中多个单元格时触发什么事件呢?我想实现选中多个单元格时,得到第一行与最后一行的行号

我要实现,只要用户进行了选中多个单元格的操作,就在文本框中显示出,选择单元格第一行的行号,和最后一行的行号。
关键不知道,纸型选中多个单元格时,触发什么事件呢?


------解决方案--------------------
可以使用 SelectionChanged 事件
访问dataGridView1.SelectedRows或SelectedCells,找你要的东西
------解决方案--------------------
C# code

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            List<int> lstIndex = new List<int>();
            foreach (DataGridViewTextBoxCell cell in dataGridView1.SelectedCells)
            {
                if (!lstIndex.Contains(cell.RowIndex))
                {
                    lstIndex.Add(cell.RowIndex);
                }
            }
            if (lstIndex.Count > 0)
            {
                lstIndex.Sort();
                //第一行
                int firstRow = lstIndex[0];
                //最后行
                int lastRow = lstIndex[lstIndex.Count - 1];
            }
        }