老鳥談ADO.NET DataGridView控件屬性
我們大家都知道ADO.NET訪問數(shù)據(jù)庫技術(shù),上網(wǎng)也收集里許多資料,在這里就ADO.NET DataGridView控件屬性給大家講述一番,大家都知道隨著一個技術(shù)的發(fā)展,它的各個功能也都隨著完善,在ADO.NET2.0提供一種強大而靈活的以表格形式顯示數(shù)據(jù)的方式。
#T#使用DataGridView控件來顯示少量數(shù)據(jù)的只讀視圖,也可以對其進行縮放以顯示特大數(shù)據(jù)集的可編輯視圖??梢杂煤芏喾绞綌U展DataGridView控件,以便將自定義行為內(nèi)置在應(yīng)用程序中。例如,可以采用編程方式指定自己的排序算法,以及創(chuàng)建自己的單元格類型。通過選擇一些屬性,可以輕松地自定義ADO.NET DataGridView控件屬性的外觀??梢詫⒃S多類型的數(shù)據(jù)存儲區(qū)用作數(shù)據(jù)源,也可以在沒有綁定數(shù)據(jù)源的情況下操作DataGridView控件。
常用的ADO.NET DataGridView控件屬性請參見表所示:
| 
 屬性  | 
 說明  | 
| 
 AllowUserToAddRows  | 
 獲取或設(shè)置一個值,該值指示是否向用戶顯示添加行的選項。  | 
| 
 AllowUserToDeleteRows  | 
 獲取或設(shè)置一個值,該值指示是否允許用戶從DataGridView中刪除行。  | 
| 
 AllowUserToOrderColumns  | 
 獲取或設(shè)置一個值,該值指示是否允許通過手動對列重新定位。  | 
| 
 AllowUserToResizeColumns  | 
 獲取或設(shè)置一個值,該值指示用戶是否可以調(diào)整列的大小。  | 
| 
 AllowUserToResizeRows  | 
 獲取或設(shè)置一個值,該值指示用戶是否可以調(diào)整行的大小。  | 
| 
 DataSource  | 
 獲取或設(shè)置DataGridView所顯示數(shù)據(jù)的數(shù)據(jù)源  | 
| 
 Columns  | 
 獲取一個包含控件中所有列的集合。  | 
詳細代碼:
- privatevoidInitializeDataGridView()
 - {
 - try
 - {
 - //建立DataGridView控件
 - dataGridView1.Dock=DockStyle.Fill;
 - //自動生成DataGridView列
 - dataGridView1.AutoGenerateColumns=true;
 - //建立數(shù)據(jù)源
 - bindingSource1.DataSource=GetData("Select*FromProducts");
 - dataGridView1.DataSource=bindingSource1;
 - //自動調(diào)整可視化行
 - dataGridView1.AutoSizeRowsMode=DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
 - //設(shè)置DataGridView控件的邊界
 - dataGridView1.BorderStyle=BorderStyle.Fixed3D;
 - //當(dāng)用戶使用DataGridView控件時,它會成為可編輯模式
 - dataGridView1.EditMode=DataGridViewEditMode.EditOnEnter;
 - }
 - catch(SqlExceptione)
 - {
 - MessageBox.Show("Torunthissamplereplaceconnection.ConnectionString"+"withavalidconnectionstringtoaNorthwind"+"databaseaccessibletoyoursystem.","ERROR",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
 - System.Threading.Thread.CurrentThread.Abort();
 - }
 - }
 - privatestaticDataTableGetData(stringsqlCommand)
 - {
 - stringconnectionString="IntegratedSecurity=SSPI;"+"PersistSecurityInfo=False;"+"InitialCatalog=Northwind;DataSource=localhost";
 - SqlConnectionnorthwindConnection=newSqlConnection(connectionString);
 - SqlCommandcommand=newSqlCommand(sqlCommand,northwindConnection);
 - SqlDataAdapteradapter=newSqlDataAdapter();
 - adapter.SelectCommand=command;
 - DataTabletable=newDataTable();
 - table.Locale=System.Globalization.CultureInfo.InvariantCulture;
 - adapter.Fill(table);
 - returntable;
 - }
 
代碼中dataGridView1.DataSource被設(shè)置為bindingSource1。















 
 
 
 
 
 
 