熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#中對DatagridView的部分常用操作

2022-06-13   來源: .NET編程 

  (最基本的技巧)獲取某列中的某行(某單元格)中的內容

  

  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;
}


From:http://tw.wingwit.com/Article/program/net/201311/11879.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.