DataGridView显示行号

DataGridView显示行号
private   void  DataGridView1_RowsAdded( object  sender, DataGridViewRowsAddedEventArgs e)
        {
            
for  ( int  i  =   0 ; i  <  e.RowCount; i ++ )
            {
                
this .DataGridView1.Rows[e.RowIndex  +  i].HeaderCell.Style.Alignment  =  DataGridViewContentAlignment.MiddleRight;
                
this .DataGridView1.Rows[e.RowIndex  +  i].HeaderCell.Value  =  (e.RowIndex  +  i  +   1 ).ToString();
            }
            
for  ( int  i  =  e.RowIndex  +  e.RowCount; i  <   this .DataGridView1.Rows.Count; i ++ )
            {
                
this .DataGridView1.Rows[i].HeaderCell.Style.Alignment  =  DataGridViewContentAlignment.MiddleRight;
                
this .DataGridView1.Rows[i].HeaderCell.Value  =  (i  +   1 ).ToString();
            }
        }

private   void  DataGridView1_RowsRemoved( object  sender, DataGridViewRowsRemovedEventArgs e)
        {
            
if  (DataGridView1.Rows.Count  !=   0 )
            {
                
for  ( int  i  =   0 ; i  <  e.RowCount; i ++ )
                {
                    
this .DataGridView1.Rows[e.RowIndex  +  i].HeaderCell.Style.Alignment  =  DataGridViewContentAlignment.MiddleRight;
                    
this .DataGridView1.Rows[e.RowIndex  +  i].HeaderCell.Value  =  (e.RowIndex  +  i  +   1 ).ToString();
                }

                
for  ( int  i  =  e.RowIndex  +  e.RowCount; i  <   this .DataGridView1.Rows.Count; i ++ )
                {
                    
this .DataGridView1.Rows[i].HeaderCell.Style.Alignment  =  DataGridViewContentAlignment.MiddleRight;
                    
this .DataGridView1.Rows[i].HeaderCell.Value  =  (i  +   1 ).ToString();
                }

            }
        }
 
如果显示的行号有被遮挡住,可将DataGridView的RowsHeaderWidthSizeMode属性设置为AutoSizeToAllHeaders或者AutoSizeToDisplayedHeaders。

 

你可能感兴趣的:(DataGridView显示行号)