DataGridview拾遗

1. 绑定数据(DataSource)时,不自动生成列 AutoGenerateColumns = false;windows

2. 添加按钮列(可视化编辑) ColumnType设置为DataGridViewButtonColumn,设置Text(按钮上的文字),UseColumnTextForButtonValue设置为truecode

3. 单元格颜色orm

  • 设置整个表格 gridView.DefaultCellStyle.BackColor = Color.Yellow;
  • 设置某行/列的颜色 gridView.Rows[0].DefaultCellStyle.BackColor = Color.Red;
  • 设置单元格颜色 gridView[0, 0].Style.BackColor = Color.Pink;
  • 奇偶行颜色
    gridView.RowsDefaultCellStyle.BackColor = Color.Aqua //全部行 
    gridView.AlternatingRowsDefaultCellStyle.BackColor = Color.Yellow //偶数行

     

  •   更多颜色 CellFormating中 if(somcondition) e.CellStyle.ForeColor = Color.Red;
  • 注:CellFormating还能够实现根据枚举值,显示对应文字,如1 表示 男 2 表示女
  •  

4. 经常使用事件索引

DataGridView.CellClick 事件 表示click到Cell自己事件

DataGridView.CellContentClick 事件 表示Click到 Cell 內包含的控件get

5. 取消第一列 RowHeadersVisible为falseit

6. 选择一行而不是单元格 SelectionMode为FullRowSelectio

7. 选择多行 MultiSelect为trueform

8.获取选中行/列class

  • gridview.CurrentCell.RowIndex;是当前活动的单元格的行的索引
  • gridview.SelectedRows 是选中行的集合
  • gridview.SelectedColumns 是选中列的集合
  • gridview.SelectedCells 是选中单元格的集合 

9. 

相关文章
相关标签/搜索