最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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、可以改變表格線條風(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、動態(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è)定光標(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ù)驗證

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#多態(tài)

    深入理解c#多態(tài)

    這篇文章主要介紹了c#多態(tài)的相關(guān)知識,文中代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • C# 多線程對資源讀寫時如何控制的方法

    C# 多線程對資源讀寫時如何控制的方法

    這篇文章主要介紹了C# 多線程對資源讀寫時如何控制的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • C#使用Selenium+PhantomJS抓取數(shù)據(jù)

    C#使用Selenium+PhantomJS抓取數(shù)據(jù)

    本文主要介紹了C#使用Selenium+PhantomJS抓取數(shù)據(jù)的方法步驟,具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • C#中的委托Delegate

    C#中的委托Delegate

    這篇文章介紹了C#中的委托Delegate,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • 使用設(shè)計模式中的工廠方法模式進(jìn)行C#編程的示例講解

    使用設(shè)計模式中的工廠方法模式進(jìn)行C#編程的示例講解

    這篇文章主要介紹了使用設(shè)計模式中的工廠方法模式進(jìn)行C#編程的示例講解,工廠方法模式可以看作是對簡單工廠模式的進(jìn)一步擴展,需要的朋友可以參考下
    2016-02-02
  • C#實現(xiàn)TFTP客戶端的項目實踐

    C#實現(xiàn)TFTP客戶端的項目實踐

    TFTP不僅有斷點續(xù)傳,多用戶級別限制等功能,本文主要介紹了C#實現(xiàn)TFTP客戶端的項目實踐,具有一定的參考價值,感興趣的可以了解一下
    2024-04-04
  • C#中Json的簡單處理方法

    C#中Json的簡單處理方法

    這篇文章主要介紹了C#中Json的簡單處理方法的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • C#中的composite模式示例詳解

    C#中的composite模式示例詳解

    Composite組合模式屬于設(shè)計模式中比較熱門的一個,相信大家對它一定不像對訪問者模式那么陌生,這篇文章主要介紹了C#中的composite模式,需要的朋友可以參考下
    2022-06-06
  • C#解碼base64編碼二進(jìn)制數(shù)據(jù)的方法

    C#解碼base64編碼二進(jìn)制數(shù)據(jù)的方法

    這篇文章主要介紹了C#解碼base64編碼二進(jìn)制數(shù)據(jù)的方法,涉及C#中Convert類的靜態(tài)方法Convert.FromBase64String使用技巧,需要的朋友可以參考下
    2015-04-04
  • C#多線程處理多個隊列數(shù)據(jù)的方法

    C#多線程處理多個隊列數(shù)據(jù)的方法

    這篇文章主要介紹了C#多線程處理多個隊列數(shù)據(jù)的方法,涉及C#線程與隊列的相關(guān)操作技巧,需要的朋友可以參考下
    2015-07-07

最新評論

湖北省| 咸丰县| 高密市| 渭南市| 嫩江县| 玉龙| 唐海县| 永清县| 陵川县| 平南县| 孝昌县| 宝清县| 玛曲县| 岑巩县| 慈利县| 黎城县| 杂多县| 开原市| 高陵县| 巴林右旗| 梨树县| 婺源县| 忻城县| 和顺县| 定远县| 五指山市| 香格里拉县| 夏邑县| 贵港市| 呈贡县| 曲阳县| 静安区| 乐陵市| 龙川县| 周宁县| 卫辉市| 华安县| 蚌埠市| 巢湖市| 松江区| 成安县|