欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C# DatagridView常用操作匯總

 更新時間:2014年07月22日 15:34:18   投稿:shichen2014  
這篇文章主要介紹了C# DatagridView常用操作匯總,羅列了一些常用的用法與技巧,需要的朋友可以參考下

本文匯總了C#中DatagridView的常用操作,有助于讀者加深對C# DatagridView用法的理解,具體如下:

1、(最基本的技巧)、獲取某列中的某行(某單元格)中的內(nèi)容

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

2、自定義列

//定義列寬
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中去。

3、自動適應(yīng)列寬

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

4、可以綁定并顯示對象

Bind Objects to Windows Forms DataGridView Controls

5、可以改變表格線條風格

this.dataGridView1.GridColor = Color.BlueViolet;
this.dataGridView1.BorderStyle = BorderStyle.Fixed3D;
this.dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;
this.dataGridView1.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
this.dataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;

6、動態(tài)改變列是否顯示,和動態(tài)改變列的顯示順序

customersDataGridView.Columns["CustomerID"].Visible = false;
customersDataGridView.Columns["ContactName"].DisplayIndex = 0;
customersDataGridView.Columns["ContactTitle"].DisplayIndex = 1;
customersDataGridView.Columns["City"].DisplayIndex = 2;
customersDataGridView.Columns["Country"].DisplayIndex = 3;
customersDataGridView.Columns["CompanyName"].DisplayIndex = 4;

7、可以在列中顯示圖像

Icon treeIcon = new Icon(this.GetType(), "tree.ico");
DataGridViewImageColumn iconColumn = new DataGridViewImageColumn ();
iconColumn.Image = treeIcon.ToBitmap();
iconColumn.Name = "Tree";
iconColumn.HeaderText = "Nice tree";
dataGridView1.Columns.Insert(2, iconColumn);

8、格式化顯示內(nèi)容:

this.dataGridView1.Columns["UnitPrice"].DefaultCellStyle.Format = "c";
this.dataGridView1.Columns["ShipDate"].DefaultCellStyle.Format = "d";
this.dataGridView1.DefaultCellStyle.NullValue = "no entry";
this.dataGridView1.DefaultCellStyle.WrapMode = DataGridViewWrapMode.Wrap;
this.dataGridView1.Columns["CustomerName"].DefaultCellStyle.Alignment =DataGridViewContentAlignment.MiddleRight;

9、將指定列及以前的列固定不動

this.dataGridView1.Columns["AddToCartButton"].Frozen = true;

10、顯示錄入時出現(xiàn)的錯誤信息

private void dataGridView1_DataError(object sender,
DataGridViewDataErrorEventArgs e)
{
// If the data source raises an exception when a cell value is
// commited, display an error message.
if (e.Exception != null &&
e.Context == DataGridViewDataErrorContext.Commit)
{
MessageBox.Show("CustomerID value must be unique.");
}}

11、大數(shù)據(jù)量顯示采用Virtual Mode

Implement Virtual Mode in the Windows Forms DataGridView Control

12、設(shè)置指定的列只讀

dataGridView1.Columns["CompanyName"].ReadOnly = true;

13、移去自動生成的列

dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = customerDataSet;
dataGridView1.Columns.Remove ("Fax");

或:

dataGridView1.Columns["CustomerID"].Visible = false;

14、自定義選擇模式

this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
this.dataGridView1.MultiSelect = false;

15、自定義設(shè)定光標進入單元格是否編輯模式(編輯模式)

this.dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;

16、新行指定默認值

private void dataGridView1_DefaultValuesNeeded
(object sender, System.Windows.Forms.DataGridViewRowEventArgs e)
{
e.Row.Cells["Region"].Value = "WA";
e.Row.Cells["City"].Value = "Redmond";
e.Row.Cells["PostalCode"].Value = "98052-6399";
e.Row.Cells["Region"].Value = "NA";
e.Row.Cells["Country"].Value = "USA";
e.Row.Cells["CustomerID"].Value = NewCustomerId();
}

17、數(shù)據(jù)驗證

private void dataGridView1_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
// Validate the CompanyName entry by disallowing empty strings.
if (dataGridView1.Columns[e.ColumnIndex].Name == "CompanyName")
{
if (e.FormattedValue.ToString() == String.Empty)
{
dataGridView1.Rows[e.RowIndex].ErrorText =
"Company Name must not be empty";
e.Cancel = true;
}}}

18、數(shù)據(jù)提交到dataset中

DataSet ds = new DataSet("MyDataSet");
ds.Tables[biaom.Trim()].Rows.Clear();
try
{
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
DataTable dt = ds.Tables[biaom.Trim()];
DataRow myrow = ds.Tables[biaom.Trim()].NewRow();
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
myrow[j] = Convert.ToString(dataGridView1.Rows[i].Cells[j].Value);
}
ds.Tables[biaom.Trim()].Rows.Add(myrow);
}
}
catch (Exception)
{
MessageBox.Show("輸入類型錯誤!");
return;
} 

相關(guān)文章

  • C# 預處理器指令的用法

    C# 預處理器指令的用法

    本文主要介紹了C# 預處理器指令的用法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-04-04
  • 實例講解C#中的職責鏈模式

    實例講解C#中的職責鏈模式

    這篇文章主要介紹了C#中的職責鏈模式的相關(guān)資料,文中示例代碼非常詳細,幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-07-07
  • unity實現(xiàn)多點觸控代碼

    unity實現(xiàn)多點觸控代碼

    這篇文章主要介紹了unity實現(xiàn)多點觸控代碼,我最近在學習Unity游戲引擎。先從Unity平面開始,本章介紹Unity 平面上的多點觸摸。有需要的小伙伴參考下。
    2015-03-03
  • .NET實現(xiàn)父窗體關(guān)閉而不影響子窗體的方法

    .NET實現(xiàn)父窗體關(guān)閉而不影響子窗體的方法

    這篇文章主要介紹了.NET實現(xiàn)父窗體關(guān)閉而不影響子窗體的方法,很實用的功能,需要的朋友可以參考下
    2014-08-08
  • C#實現(xiàn)字符串與圖片的Base64編碼轉(zhuǎn)換操作示例

    C#實現(xiàn)字符串與圖片的Base64編碼轉(zhuǎn)換操作示例

    這篇文章主要介紹了C#實現(xiàn)字符串與圖片的Base64編碼轉(zhuǎn)換操作,結(jié)合實例形式分析了C#針對base64編碼與圖片的相互轉(zhuǎn)換操作技巧,需要的朋友可以參考下
    2017-06-06
  • C#向Word插入排版精良的TextBox

    C#向Word插入排版精良的TextBox

    這篇文章主要為大家詳細介紹了C#向Word插入排版精良的Text Box的相關(guān)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • C#關(guān)于Func和Action委托的介紹詳解

    C#關(guān)于Func和Action委托的介紹詳解

    委托是存有對某個方法的引用的一種引用類型變量,本文主要介紹了C#關(guān)于Func和Action委托的介紹,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Unity Shader實現(xiàn)黑幕過場效果

    Unity Shader實現(xiàn)黑幕過場效果

    這篇文章主要為大家詳細介紹了Unity Shader實現(xiàn)黑幕過場效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • C#多線程系列之線程池

    C#多線程系列之線程池

    本文詳細講解了C#多線程中的線程池,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-02-02
  • C# 游戲外掛實現(xiàn)核心代碼

    C# 游戲外掛實現(xiàn)核心代碼

    最近打算學習下游戲外掛,因為c#語言,感覺比較順,高手用delphi的多,不知道哪個最好。
    2009-01-01

最新評論