C#中對DatagridView部分常用操作

黄舟
發布: 2016-12-21 14:50:41
原創
1703 人瀏覽過

this.currentposition = this.dataGridView1.BindingContext 

[this.dataGridView1.DataSource, this.dataGridView1.DataMember].Position; 

bookContent = this.data.ember].Position; 

bookCont ][21].ToString().Trim(); 

MessageBox.Show(bookContent); 


1、自訂列 


//定義列寬
View

= 80; 

this.dataGridView1.Columns[1].Width = 80; 

this.dataGridView1.Columns[2].Width = 180; 
Grids this.dataGridView1.Columns[4].Width = 120; 

Customize Cells and Columns in the Windows Forms 

DataGrid Control by Extending TheirBehavior and ell 類別產生新的Cell類,然後繼承DataGridViewColumn 產生新的Column類,並指定 

CellTemplate為新的Cell類別。新產生的Column便可以增加到DataGridView中去。


2、自動適應列寬 


PRogrammatically Resize Cells to Fit Content in 

the Windows Forms DataGridView ControlSamples: 


the Windows Forms DataGridView.

DataGridView.AutoSizeColumn(DataGridViewAutoSizeColumnCriteria.HeaderOnly,2, false); 

DataGridView.AutoSizeRow(DataGridViewAutoSizeRowCriteria.Columns,2, false); 

DataGridView.AutoSizeRows 

(Data


3、可以綁定並顯示器物件 


Bind Objects to Windows Forms DataGridView Controls 


4、可變更表格線條風格 

Change the Border and Gridline Styles BlueViolet ; 

this.dataGridView1.BorderStyle = BorderStyle.Fixed3D; 

this.dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;
this.dataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single; 


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


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

customersData customersDataGridView.Columns["ContactName"].DisplayIndex = 0; 

customersDataGridView.Columns["ContactTitle"].DisplayIndex = 1; 

customersDataViewView. umns[" Country"].DisplayIndex = 3; 

customersDataGridView.Columns["CompanyName"].DisplayIndex = 4; 


6、可在欄位中顯示映像Icon treeIcon = new Icon(this.GetType(), "tree.ico"); 

DataGridViewImageColumn iconColumn = new DataGridViewImageColumn (); 

iconColumn.Image = treeI.Toemage ;iconColumn.HeaderText = "Nice tree"; 

dataGridView1.Columns.Insert(2, iconColumn); 


7、格式化顯示內容: 
.Columns["UnitPrice"].DefaultCellStyle.Format = "c"; 

this.dataGridView1.Columns["ShipDate"].DefaultCellStyle.Format = "d"; 

this.ApryStyle. "; 

this.dataGridView1.DefaultCellStyle.WrapMode = DataGridViewWrapMode.Wrap; 

this.dataGridView1.Columns["CustomerName"]. 、在拖曳列的滾動條時可以將指定的欄位凍結 


Freeze Columns in the Windows Forms DataGridView ControlSamples: 

將指定欄位及先前的欄位固定不動this.dataGridView1.Columns["AddToCartButton"].

9、取得選取的儲存格,行,列 


Get the Selected Cells, Rows, 

and Columns in the Windows Forms DataGridView ControlSamples: 

and Columns in the Windows Forms DataGridView ControlSamples: 

and Columns in the Windows Forms DataGridView ControlSamples: 

Errors that Occur During Data Entry in the Windows 

Forms DataGridView ControlSamples: 

private void dataGridView1_DataError 

(object sender,DataGrid方格DataErrorEventArgs e){ 
(object sender,DataGrid配置ErrorEventArgs e){ 
/
////////////////////////////////////////////////////////-付付端行為提交訊息。

if 

(e.Exception != null &&e.Context == DataGridViewDataErrorContext.Commit){ 

MessageBox.Show("CustomerID"); 、大數據量顯示採用虛擬模式 


在Windows 窗體DataGridView 控制項中實作虛擬模式 


12、設定指定的列互動 


在Windows 窗體中製作欄位Data Columns["CompanyName"].ReadOnly = true; 


13、移去自動產生的列


從Windows 窗體DataGridView ControlSample 中刪除自動產生的列:
DataSource

= customerDataSet;dataGridView1.Columns.Remove ("Fax 」); 

或:dataGridView1.Columns["CustomerID"].Visible = false 

14、自訂Windows Forms DataGridView ControlSample: 

this.dataGridView1.SelectionMode 

= DataGridViewSelectionMode.FullRowSelect; 

this.dataGrids
指定Windows 窗體DataGridView 的編輯模式

Controlthis.dataGridView1.EditMode 

= DataGridViewEditMode.EditOnEnter; 


16、新行指定private void dataGridView1_DefaultValuesNeeded(object sender, 

System.Windows.Forms.DataGridViewRowEventArgs e){ 

e.Row.Cells["Region"].Value. .Value = "雷蒙"; 

e.Row.Cells["郵遞區號"].Value = "98052-6399"; 

e.Row.Cells["Region"].Value = "NA";

e.Row.Cells["國家"].Value = "美國"; 

e.Row.Cells["CustomerID"].Value = NewCustomerId(); 




17
驗證Windows 窗體DataGridView ControlSamples 中的資料: 

private void dataGridView1_CellValidating 

(object sender,DataGridViewCellValidatingEventArgs e){

if (dataGridView1.Columns[e.ColumnIndex].Name == "CompanyName"){

if (e.FormattedValue.ToString() == String.Empty){ 

dataView1.Rowsx]. ErrorText 

="公司名稱不能為空"; 

e.Cancel = true; 






Data 18、設定資料集到資料集中); 

ds.Tables[biaom.Trim()].Rows.Clear(); 

try{for (int i = 0; i
DataTable ds.Tables[biaom.Trim()]; 

DataRow myrow = ds.Tables[biaom.Trim()].NewRow(); 

for (int j = 0; j
myrow[j] = Convert.ToString(dataGridView1.Rows[i].Cells[j].Value); 



ds.Tables[biaom.Trim()].Rows.Add(myrow);





catch(異常){ 

MessageBox.Show("輸入類型錯誤!"); 

相關內容請關注PHP中文網(www.php.cn)! 



來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板