c#

位置:IT落伍者 >> c# >> 浏览文章

C#中对DatagridView部分常用操作


发布日期:2019年02月19日
 
C#中对DatagridView部分常用操作

(最基本的技巧)获取某列中的某行(某单元格)中的内容

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 TheirBehavior and

AppearanceHost 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 ControlSamples

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 ControlSamples:

thisdataGridViewGridColor = ColorBlueViolet;

thisdataGridViewBorderStyle = BorderStyleFixedD;

thisdataGridViewCellBorderStyle = DataGridViewCellBorderStyleNone;

thisdataGridViewRowHeadersBorderStyle = DataGridViewHeaderBorderStyleSingle;

thisdataGridViewColumnHeadersBorderStyle = DataGridViewHeaderBorderStyleSingle;

动态改变列是否显示和动态改变列的显示顺序

Change the Order of the Columns in the Windows Forms DataGridView ControlSamples

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 ControlSamples

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 ControlSamples:

thisdataGridViewColumns[UnitPrice]DefaultCellStyleFormat = c;

thisdataGridViewColumns[ShipDate]DefaultCellStyleFormat = d;

thisdataGridViewDefaultCellStyleNullValue = no entry;

thisdataGridViewDefaultCellStyleWrapMode = DataGridViewWrapModeWrap;

thisdataGridViewColumns[CustomerName]

DefaultCellStyleAlignment =DataGridViewContentAlignmentMiddleRight;

在拖动列的滚动条时可以将指定的列冻结

Freeze Columns in the Windows Forms DataGridView ControlSamples

将指定列及以前的列固定不动thisdataGridViewColumns[AddToCartButton]

Frozen = true;

获取选择的单元格

Get the Selected Cells Rows

and Columns in the Windows Forms DataGridView ControlSamples:

显示录入时出现的错误信息

Handle Errors that Occur During Data Entry in the Windows

Forms DataGridView ControlSamples

private void dataGridView_DataError

(object senderDataGridViewDataErrorEventArgs 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 ReadOnlySamples

dataGridViewColumns[CompanyName]ReadOnly = true;

移去自动生成的列

Remove Autogenerated Columns from a Windows Forms DataGridView ControlSample:

dataGridViewAutoGenerateColumns

= true;dataGridViewDataSource

= customerDataSet;dataGridViewColumnsRemove (Fax);

dataGridViewColumns[CustomerID]Visible = false;

自定义

选择模式

Set the Selection Mode of the Windows Forms DataGridView ControlSample

thisdataGridViewSelectionMode

= DataGridViewSelectionModeFullRowSelect;

thisdataGridViewMultiSelect = false;

自定义设定光标进入单元格是否编辑模式(编辑模式)

Specify the Edit Mode for the Windows Forms DataGridView

ControlthisdataGridViewEditMode

= DataGridViewEditModeEditOnEnter;

新行指定默认值

Specify Default Values for New Rows in the Windows

Forms DataGridView ControlSample

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 ControlSamples

private void dataGridView_CellValidating

(object senderDataGridViewCellValidatingEventArgs 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;

}

               

上一篇:c#二叉树遍历vs2008实现

下一篇:趣味理解ADO.NET对象模型