C#中對DatagridVIEw部分常用操作
只列出技巧部分,後面會有補充
0(最基本的技巧). 獲取某列中的某行(某單元格)中的內容
this.currentposition = this.dataGridView1.BindingContext [this.dataGridView1.DataSource, this.dataGridVIEw1.DataMember].Position;
bookContent = this.database.dataSet.Tables[0].Rows [this.currentposition][21].ToString().Trim();
MessageBox.Show(bookContent);
1、自定義列
//定義列寬
this.dataGridVIEw1.Columns[0].Width = 80;
this.dataGridVIEw1.Columns[1].Width = 80;
this.dataGridVIEw1.Columns[2].Width = 180;
this.dataGridVIEw1.Columns[3].Width = 120;
this.dataGridVIEw1.Columns[4].Width = 120;
Customize Cells and Columns in the Windows Forms DataGridVIEw Control by Extending Their
Behavior and Appearance
Host Controls in Windows Forms DataGridVIEw Cells
繼承 DataGridViewTextBoxCell 類生成新的Cell類,然後再繼承 DataGridVIEwColumn 生成新的Column類,並指定
CellTemplate為新的Cell類。新生成的Column便可以增加到DataGridVIEw中去。
2、自動適應列寬
Programmatically Resize Cells to Fit Content in the Windows Forms DataGridVIEw Control
Samples:
DataGridVIEw.AutoSizeColumns(
DataGridVIEwAutoSizeColumnCriteria.HeaderAndDisplayedRows);
DataGridVIEw.AutoSizeColumn(
DataGridVIEwAutoSizeColumnCriteria.HeaderOnly,
2, false);
DataGridVIEw.AutoSizeRow(
DataGridVIEwAutoSizeRowCriteria.Columns,
2, false);
DataGridVIEw.AutoSizeRows(
DataGridVIEwAutoSizeRowCriteria.HeaderAndColumns,
0, dataGridVIEw1.Rows.Count, false);