|
<p >0(最基本的技巧)、获取某列中的某行(某单元格)中的内容<p ><p >this.currentposition = this.dataGridView1.BindingContext<p >[this.dataGridView1.DataSource, this.dataGridView1.DataMember].Position;<p >bookContent = this.database.dataSet.Tables[0].Rows<p >[this.currentposition][21].ToString().Trim();<p >MessageBox.Show(bookContent); <p ><p >1、自定义列<p ><p >//定义列宽<p >this.dataGridView1.Columns[0].Width = 80;<p >this.dataGridView1.Columns[1].Width = 80;<p >this.dataGridView1.Columns[2].Width = 180;<p >this.dataGridView1.Columns[3].Width = 120;<p >this.dataGridView1.Columns[4].Width = 120;<p >Customize Cells and Columns in the Windows Forms <p >DataGridView Control by Extending TheirBehavior and <p >AppearanceHost Controls in Windows Forms DataGridView Cells <p ><p >继承 DataGridViewTextBoxCell 类生成新的Cell类,然后再继承 DataGridViewColumn 生成新的Column类,并指定<p >CellTemplate为新的Cell类。新生成的Column便可以增加到DataGridView中去。<p ><p >2、自动适应列宽<p ><p > rogrammatically Resize Cells to Fit Content in<p > the Windows Forms DataGridView ControlSamples:<p >DataGridView.AutoSizeColumns(DataGridViewAutoSizeColumnCriteria.HeaderAndDisplayedRows);<p >DataGridView.AutoSizeColumn(DataGridViewAutoSizeColumnCriteria.HeaderOnly,2, false);<p >DataGridView.AutoSizeRow(DataGridViewAutoSizeRowCriteria.Columns,2, false);<p >DataGridView.AutoSizeRows<p >(DataGridViewAutoSizeRowCriteria.HeaderAndColumns,0, dataGridView1.Rows.Count, false); <p ><p >3、可以绑定并显示对象<p ><p >Bind Objects to Windows Forms DataGridView Controls <p ><p >4、可以改变表格线条风格<p ><p ><p >Change the Border and Gridline Styles in <p >the Windows Forms DataGridView ControlSamples:<p >this.dataGridView1.GridColor = Color.BlueViolet;<p >this.dataGridView1.BorderStyle = BorderStyle.Fixed3D;<p >this.dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;<p >this.dataGridView1.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;<p >this.dataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single; <p ><p >5、动态改变列是否显示,和动态改变列的显示顺序<p ><p >Change the Order of the Columns in the Windows Forms DataGridView ControlSamples:<p >customersDataGridView.Columns["CustomerID"].Visible = false;<p >customersDataGridView.Columns["ContactName"].DisplayIndex = 0;<p >customersDataGridView.Columns["ContactTitle"].DisplayIndex = 1;<p >customersDataGridView.Columns["City"].DisplayIndex = 2;<p >customersDataGridView.Columns["Country"].DisplayIndex = 3;<p >customersDataGridView.Columns["CompanyName"].DisplayIndex = 4; <p ><p >6、可以在列中显示图像<p ><p >Display Images in Cells of the Windows Forms DataGridView ControlSamples:<p >Icon treeIcon = new Icon(this.GetType(), "tree.ico");<p >DataGridViewImageColumn iconColumn = new DataGridViewImageColumn ();<p >iconColumn.Image = treeIcon.ToBitmap();iconColumn.Name =<p > "Tree";iconColumn.HeaderText = "Nice tree";<p >dataGridView1.Columns.Insert(2, iconColumn); <p ><p >7、格式化显示内容:<p ><p >Format Data in the Windows Forms DataGridView ControlSamples:<p >this.dataGridView1.Columns["UnitPrice"].DefaultCellStyle.Format = "c";<p >this.dataGridView1.Columns["ShipDate"].DefaultCellStyle.Format = "d";<p >this.dataGridView1.DefaultCellStyle.NullValue = "no entry";<p >this.dataGridView1.DefaultCellStyle.WrapMode = DataGridViewWrapMode.Wrap;<p >this.dataGridView1.Columns["CustomerName"].<p >DefaultCellStyle.Alignment =DataGridViewContentAlignment.MiddleRight; <p ><p >8、在拖动列的滚动条时可以将指定的列冻结<p ><p >Freeze Columns in the Windows Forms DataGridView ControlSamples:<p >将指定列及以前的列固定不动this.dataGridView1.Columns["AddToCartButton"].<p >Frozen = true; <p ><p >9、获取选择的单元格,行,列<p ><p >Get the Selected Cells, Rows, <p >and Columns in the Windows Forms DataGridView ControlSamples: <p ><p >10、显示录入时出现的错误信息<p ><p >Handle Errors that Occur During Data Entry in the Windows<p > Forms DataGridView ControlSamples:<p >private void dataGridView1_DataError<p >(object sender,DataGridViewDataErrorEventArgs e){<p >// If the data source raises an exception when a cell value is<p >// commited, display an error message.<p >if<p > (e.Exception != null &&e.Context == DataGridViewDataErrorContext.Commit){<p >MessageBox.Show("CustomerID value must be unique.");<p >}<p >} <p ><p >11、大数据量显示采用Virtual Mode<p ><p >Implement Virtual Mode in the Windows Forms DataGridView Control <p ><p >12、设置指定的列只读<p ><p >Make Columns in the Windows Forms DataGridView Control Read-OnlySamples:<p >dataGridView1.Columns["CompanyName"].ReadOnly = true; <p ><p >13、移去自动生成的列<p ><p >Remove Autogenerated Columns from a Windows Forms DataGridView ControlSample:<p >dataGridView1.AutoGenerateColumns <p >= true;dataGridView1.DataSource <p >= customerDataSet;dataGridView1.Columns.Remove ("Fax");<p >或:dataGridView1.Columns["CustomerID"].Visible = false; <p align="center"><font color="FF0000" >1</font>2<span class="content01">下一页>></span></p></p> |
|