DataGridView重查后,返回原来所在行

 首先记录选中行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//查询前记录选中行
int _currentRow = 0;
//int _currentColumn = 0;
switch (modMark)
{
     case 1:
         if (dgvHis.CurrentCell != null )
         {
             _currentRow = this .dgvHis.CurrentCell.RowIndex;
             //_currentColumn = this.dgvHis.CurrentCell.ColumnIndex ;
         }
         break ;
     case 2:
         if (dgvBank.CurrentCell != null )
         {
             _currentRow = this .dgvBank.CurrentCell.RowIndex;
             //_currentColumn = this.dgvBank.CurrentCell.ColumnIndex;
         }
         break ;
}

设置选中行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
             //设置选中行
             switch (modMark)
             {
                 case 1:
                     if (dgvHis.Rows.Count > 0)
                     {
                         if (dgvHis.Rows.Count >= _currentRow)
                         {
                             dgvHis.Rows[_currentRow].Selected = true ;
                             dgvHis.FirstDisplayedScrollingRowIndex = _currentRow;
                         }
                         else
                         {
                             dgvHis.Rows[dgvHis.Rows.Count - 1].Selected = true ;
                             dgvHis.FirstDisplayedScrollingRowIndex = dgvHis.Rows.Count - 1;
                         }
 
                     }
//                     if (dgvHis.Columns.Count > _currentColumn)
//                     {
//                         dgvHis.Columns[_currentColumn].Selected = true;
//                     }
//                     else
//                     {
//                         dgvHis.Columns[dgvHis.Columns.Count - 1].Selected = true;
//                     }
                     break ;
                 case 2:
                     if (dgvBank.Rows.Count > 0)
                     {
                         if (dgvBank.Rows.Count >= _currentRow)
                         {
                             dgvBank.Rows[_currentRow].Selected = true ;
                             dgvBank.FirstDisplayedScrollingRowIndex = _currentRow;
                         }
                         else
                         {
                             dgvBank.Rows[dgvBank.Rows.Count - 1].Selected = true ;
                             dgvBank.FirstDisplayedScrollingRowIndex = dgvBank.Rows.Count - 1;
                         }
                     }
//                     if (dgvBank.Columns.Count > _currentColumn)
//                     {
//                         dgvBank.Columns[_currentColumn].Selected = true;
//                     }
//                     else
//                     {
//                         dgvBank.Columns[dgvBank.Columns.Count - 1].Selected = true;
//                     }
                     break ;
             }

你可能感兴趣的:(datagridview)