C#: Common attributes of DataGridView control in Winiform form

We first compare the two versions of the DataGridView control:

version 1:

Version 2:

Through comparison, it can be found that version 2 is more user-friendly than version 1, adding line numbers and centering text information, while covering the entire form, which can give users a better experience. So how do you do this?

1. Make the DataGridView control cover the entire form:

  Change the AutoSizeColumnsMode property to Fill

  

2. Add automatic line number

//绘制单元格时发生
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
     dataGridView1.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
     if (e.ColumnIndex < 0 && e.RowIndex >= 0)
     {
       // 绘制 自动序号
       e.Paint(e.ClipBounds, DataGridViewPaintParts.All);
       Rectangle vRect = e.CellBounds;
       vRect.Inflate(-2, 2);
       TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), e.CellStyle.Font, vRect, e.CellStyle.ForeColor, TextFormatFlags.Right | TextFormatFlags.VerticalCenter);
       e.Handled = true;
      }
}
            

3. Cell style settings

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
      //单元格样式设置
      if (e.RowIndex % 2 == 0)
      { // 行序号为双数(含0)时                  
        e.CellStyle.BackColor = Color.White;
      }
      else
      {
         e.CellStyle.BackColor = Color.Honeydew;
      }
       // 选中单元格时,背景色
       e.CellStyle.SelectionBackColor = Color.Gray;
       //单位格内数据对齐方式        
       e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
 }

4. It is forbidden to use the mouse to drag the row width and column height

dataGridView1.AllowUserToResizeColumns = false; // 禁拖动列宽度  

dataGridView1.AllowUserToResizeRows = false;    // 禁拖动行高度

5. Disable the delete function of the Delete key

dataGridView1.AllowUserToDeleteRows = false;

//上述禁用,仅是将用户界面交互的自动新增行禁了,但还是可以通过代码:

dataGridView1.Rows.Remove(DataGridViewRow dataGridViewRow);

//或者 

dataGridView1.Rows.RemoveAt(int index);

//来删除指定行数据。

6. Make the cell non-editable

Change the ReadOnly property to True

7. Click to select the entire row and column

Reference blog: https://www.cnblogs.com/nimorl/p/9494452.html

dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
// 单击选中整行,枚举

SelectionMode is an enumerated type:

CellSelect   One or more cells can be selected
FullRowSelect Select the entire row by clicking on the header of the row or the cells contained in the row
FullColumnSelect Select the entire column by clicking on the header of the column or the cells contained in the column
RowHeaderSelect  Select this row by clicking on the header cell of the row. You can select this cell individually by clicking a cell.
ColumnHeaderSelect This column can be selected by clicking on the header cell of the column. You can select this cell individually by clicking a cell.

Guess you like

Origin blog.csdn.net/weixin_44690047/article/details/113618185