C#DataGridView两个数据表同步滚动以及同步选中行

一、同步滚动
SumTable为表1,CycleTable为表2
两个表都添加Scroll滚动事件
        private void SumTable_Scroll(object sender, ScrollEventArgs e)//滚动同步
        {
            CycleTable.FirstDisplayedScrollingRowIndex = SumTable.FirstDisplayedScrollingRowIndex;//行滚动同步
            //CycleTable.FirstDisplayedScrollingColumnIndex = SumTable.FirstDisplayedScrollingColumnIndex;//列滚动同步
        }

        private void CycleTable_Scroll(object sender, ScrollEventArgs e)//滚动同步
        {
            SumTable.FirstDisplayedScrollingRowIndex = CycleTable.FirstDisplayedScrollingRowIndex;
            //SumTable.FirstDisplayedScrollingColumnIndex = CycleTable.FirstDisplayedScrollingColumnIndex;//列滚动同步
        }

 

 

二、同步选中

两个表都添加CellClick单元格点击事件
         private void SumTable_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (SumTable.SelectedRows.Count != 0 && e.RowIndex >= 0 && e.RowIndex < CycleTable.Rows.Count)
            {
                CycleTable.Rows[e.RowIndex].Selected = true;
            }
        }

        private void CycleTable_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (CycleTable.SelectedRows.Count != 0 && e.RowIndex >= 0 && e.RowIndex < SumTable.Rows.Count)
            {
                SumTable.Rows[e.RowIndex].Selected = true;
            }
        }

 

posted @ 2023-07-17 16:26  青丝·旅人  阅读(474)  评论(0编辑  收藏  举报