(最基本的技巧)获取某列中的某行(某单元格)中的内容
thiscurrentposition = thisdataGridViewBindingContext
[thisdataGridViewDataSource thisdataGridViewDataMember]Position;
bookContent = thisdatabasedataSetTables[]Rows
[thiscurrentposition][]ToString()Trim();
MessageBoxShow(bookContent);
//定义列宽
thisdataGridViewColumns[]Width = ;
thisdataGridViewColumns[]Width = ;
thisdataGridViewColumns[]Width = ;
thisdataGridViewColumns[]Width = ;
thisdataGridViewColumns[]Width = ;
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中去
自动适应列宽
Programmatically Resize Cells to Fit Content in the Windows Forms DataGridView Control
Samples
DataGridViewAutoSizeColumns(
DataGridViewAutoSizeColumnCriteriaHeaderAndDisplayedRows);
DataGridViewAutoSizeColumn(
DataGridViewAutoSizeColumnCriteriaHeaderOnly
false);
DataGridViewAutoSizeRow(
DataGridViewAutoSizeRowCriteriaColumns
false);
DataGridViewAutoSizeRows(
DataGridViewAutoSizeRowCriteriaHeaderAndColumns
dataGridViewRowsCount false);
可以绑定并显示对象
Bind Objects to Windows Forms DataGridView Controls
可以改变表格线条风格
Change the Border and Gridline Styles in the Windows Forms DataGridView Control
Samples:
thisdataGridViewGridColor = ColorBlueViolet;
thisdataGridViewBorderStyle = BorderStyleFixedD;
thisdataGridViewCellBorderStyle = DataGridViewCellBorderStyleNone;
thisdataGridViewRowHeadersBorderStyle = DataGridViewHeaderBorderStyleSingle;
thisdataGridViewColumnHeadersBorderStyle = DataGridViewHeaderBorderStyleSingle;
动态改变列是否显示和动态改变列的显示顺序
Change the Order of the Columns in the Windows Forms DataGridView Control
Samples
customersDataGridViewColumns[CustomerID]Visible = false;
customersDataGridViewColumns[ContactName]DisplayIndex = ;
customersDataGridViewColumns[ContactTitle]DisplayIndex = ;
customersDataGridViewColumns[City]DisplayIndex = ;
customersDataGridViewColumns[Country]DisplayIndex = ;
customersDataGridViewColumns[CompanyName]DisplayIndex = ;
可以在列中显示图像
Display Images in Cells of the Windows Forms DataGridView Control
Samples
Icon treeIcon = new Icon(thisGetType() treeico);
DataGridViewImageColumn iconColumn = new DataGridViewImageColumn ();
iconColumnImage = treeIconToBitmap();
iconColumnName = Tree;
iconColumnHeaderText = Nice tree;
dataGridViewColumnsInsert( iconColumn);
格式化显示内容:
Format Data in the Windows Forms DataGridView Control
Samples:
thisdataGridViewColumns[UnitPrice]DefaultCellStyleFormat = c;
thisdataGridViewColumns[ShipDate]DefaultCellStyleFormat = d;
thisdataGridViewDefaultCellStyleNullValue = no entry;
thisdataGridViewDefaultCellStyleWrapMode = DataGridViewWrapModeWrap;
thisdataGridViewColumns[CustomerName]DefaultCellStyleAlignment =
DataGridViewContentAlignmentMiddleRight;
在拖动列的滚动条时可以将指定的列冻结
Freeze Columns in the Windows Forms DataGridView Control
Samples将指定列及以前的列固定不动
thisdataGridViewColumns[AddToCartButton]Frozen = true;
获取选择的单元格行列
Get the Selected Cells Rows and Columns in the Windows Forms DataGridView Control
Samples:
显示录入时出现的错误信息
Handle Errors that Occur During Data Entry in the Windows Forms DataGridView Control
Samples
private void dataGridView_DataError(object sender
DataGridViewDataErrorEventArgs e)
{
// If the data source raises an exception when a cell value is
// commited display an error message
if (eException != null &&
eContext == DataGridViewDataErrorContextCommit)
{
MessageBoxShow(CustomerID value must be unique);
}}
大数据量显示采用Virtual Mode
Implement Virtual Mode in the Windows Forms DataGridView Control
设置指定的列只读
Make Columns in the Windows Forms DataGridView Control ReadOnly
Samples
dataGridViewColumns[CompanyName]ReadOnly = true;
移去自动生成的
列
Remove Autogenerated Columns from a Windows Forms DataGridView Control
Sample:
dataGridViewAutoGenerateColumns = true;
dataGridViewDataSource = customerDataSet;
dataGridViewColumnsRemove (Fax);
或
dataGridViewColumns[CustomerID]Visible = false;
自定义选择模式
Set the Selection Mode of the Windows Forms DataGridView Control
Sample
thisdataGridViewSelectionMode = DataGridViewSelectionModeFullRowSelect;
thisdataGridViewMultiSelect = false;
自定义设定光标进入单元格是否编辑模式(编辑模式)
Specify the Edit Mode for the Windows Forms DataGridView Control
thisdataGridViewEditMode = DataGridViewEditModeEditOnEnter;
新行指定默认值
Specify Default Values for New Rows in the Windows Forms DataGridView Control
Sample
private void dataGridView_DefaultValuesNeeded
(object sender SystemWindowsFormsDataGridViewRowEventArgs e)
{
eRowCells[Region]Value = WA;
eRowCells[City]Value = Redmond;
eRowCells[PostalCode]Value = ;
eRowCells[Region]Value = NA;
eRowCells[Country]Value = USA;
eRowCells[CustomerID]Value = NewCustomerId();
}
数据验证
Validate Data in the Windows Forms DataGridView Control
Samples
private void dataGridView_CellValidating(object sender
DataGridViewCellValidatingEventArgs e)
{
// Validate the CompanyName entry by disallowing empty strings
if (dataGridViewColumns[eColumnIndex]Name == CompanyName)
{
if (eFormattedValueToString() == StringEmpty)
{
dataGridViewRows[eRowIndex]ErrorText =
Company Name must not be empty;
eCancel = true;
}}}
数据提交到dataset中
DataSet ds = new DataSet(MyDataSet);
dsTables[biaomTrim()]RowsClear();
try
{
for (int i = ; i < dataGridViewRowsCount ; i++)
{
DataTable dt = dsTables[biaomTrim()];
DataRow myrow = dsTables[biaomTrim()]NewRow();
for (int j = ; j < dataGridViewColumnsCount; j++)
{
myrow[j] = ConvertToString(dataGridViewRows[i]Cells[j]Value);
}
dsTables[biaomTrim()]RowsAdd(myrow);
}
}
catch (Exception)
{
MessageBoxShow(输入类型错误!);
return;
}