程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> .Net基礎:C#中對DatagridView部分常用操作

.Net基礎:C#中對DatagridView部分常用操作

編輯:.NET實例教程

.

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

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


1、自定義列


//定義列寬

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

AppearanceHost Controls in Windows Forms DataGridVIEw Cells


繼承 DataGridViewTextBoxCell 類生成新的Cell類,然後再繼承 DataGridVIEwColumn 生成新的Column類,並指定

CellTemplate為新的Cell類。新生成的Column便可以增加到DataGridVIEw中去。


2、自動適應列寬


Programmatically Resize Cells to Fit Content in

the Windows Forms DataGridVIEw ControlSamples:

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


3、可以綁定並顯示對象


Bind Objects to Windows Forms DataGridVIEw Controls


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

 

Change the Border and Gridline Styles in

the Windows Forms DataGridVIEw ControlSamples:

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;


5、動態改變列是否顯示,和動態改變列的顯示順序


Change the Order of the Columns in the Windows Forms DataGridVIEw ControlSamples:

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;

6、可以在列中顯示圖像

Display Images in Cells of the Windows Forms DataGridVIEw ControlSamples:

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

7、格式化顯示內容:

Format Data in the Windows Forms DataGridVIEw ControlSamples:

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;

8、在拖動列的滾動條時可以將指定的列凍結

Freeze Columns in the Windows Forms DataGridVIEw ControlSamples:

將指定列及以前的列固定不動this.dataGridVIEw1.Columns["AddToCartButton"].

Frozen = true;

9、獲取選擇的單元格,行,列

Get the Selected Cells, Rows,

and Columns in the Windows Forms DataGridVIEw ControlSamples:

10、顯示錄入時出現的錯誤信息

Handle Errors that Occur During Data Entry in the Windows

Forms DataGridVIEw ControlSamples:

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、大數據量顯示采用Virtual Mode

Implement Virtual Mode in the Windows Forms DataGridVIEw Control

12、設置指定的列只讀

Make Columns in the Windows Forms DataGridVIEw Control Read-OnlySamples:

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

13、移去自動生成的列

Remove Autogenerated Columns from a Windows Forms DataGridVIEw ControlSample:

dataGridVIEw1.AutoGenerateColumns

= true;dataGridVIEw1.DataSource

= customerDataSet;dataGridVIEw1.Columns.Remove ("Fax");

或:dataGridVIEw1.Columns["CustomerID"].Visible = false;

.

 

14、自定義


選擇模式


Set the Selection Mode of the Windows Forms DataGridVIEw ControlSample:

this.dataGridVIEw1.SelectionMode

= DataGridVIEwSelectionMode.FullRowSelect;

this.dataGridVIEw1.MultiSelect = false;


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


Specify the Edit Mode for the Windows Forms DataGridVIEw

Controlthis.dataGridVIEw1.EditMode

= DataGridVIEwEditMode.EditOnEnter;


16、新行指定默認值


Specify Default Values for New Rows in the Windows

Forms DataGridVi
ew ControlSample:

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、數據驗證


Validate Data in the Windows Forms DataGridVIEw ControlSamples:

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、數據提交到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;

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved