C#中对DatagridView的部分常用操作
2009-05-21 08:27:46 来源:WEB开发网dbfC#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);
Tags:DatagridView 部分 常用
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接