亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C# DatagridView常用操作匯總

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

本文匯總了C#中DatagridView的常用操作,有助于讀者加深對(duì)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、自動(dòng)適應(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、可以綁定并顯示對(duì)象

Bind Objects to Windows Forms DataGridView Controls

5、可以改變表格線條風(fēng)格

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、動(dòng)態(tài)改變列是否顯示,和動(dòng)態(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、將指定列及以前的列固定不動(dòng)

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

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

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、移去自動(dòng)生成的列

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è)定光標(biāo)進(jìn)入單元格是否編輯模式(編輯模式)

this.dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;

16、新行指定默認(rèn)值

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ù)驗(yàn)證

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("輸入類型錯(cuò)誤!");
return;
} 

相關(guān)文章

  • C# 預(yù)處理器指令的用法

    C# 預(yù)處理器指令的用法

    本文主要介紹了C# 預(yù)處理器指令的用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • 實(shí)例講解C#中的職責(zé)鏈模式

    實(shí)例講解C#中的職責(zé)鏈模式

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

    unity實(shí)現(xiàn)多點(diǎn)觸控代碼

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

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

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

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

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

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

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

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

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

    Unity Shader實(shí)現(xiàn)黑幕過(guò)場(chǎng)效果

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

    C#多線程系列之線程池

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

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

    最近打算學(xué)習(xí)下游戲外掛,因?yàn)閏#語(yǔ)言,感覺(jué)比較順,高手用delphi的多,不知道哪個(gè)最好。
    2009-01-01

最新評(píng)論