知识大全 C#中对DatagridView的部分常用操作
Posted 内容
篇首语:实践是知识的母亲,知识是生活的明灯。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 C#中对DatagridView的部分常用操作相关的知识,希望对你有一定的参考价值。
C#中对DatagridView的部分常用操作 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
(最基本的技巧) 获取某列中的某行(某单元格)中的内容
this currentposition = this dataGridView BindingContext
[this dataGridView DataSource this dataGridView DataMember] Position;bookContent = this database dataSet Tables[ ] Rows
[this currentposition][ ] ToString() Trim();MessageBox Show(bookContent);
//定义列宽this dataGridView Columns[ ] Width = ;this dataGridView Columns[ ] Width = ;this dataGridView Columns[ ] Width = ;this dataGridView Columns[ ] Width = ;this dataGridView Columns[ ] Width = ;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中去
自动适应列宽
Programmatically Resize Cells to Fit Content in the Windows Forms DataGridView ControlSamples DataGridView AutoSizeColumns(DataGridViewAutoSizeColumnCriteria HeaderAndDisplayedRows);DataGridView AutoSizeColumn(DataGridViewAutoSizeColumnCriteria HeaderOnly false);DataGridView AutoSizeRow(DataGridViewAutoSizeRowCriteria Columns false);DataGridView AutoSizeRows(DataGridViewAutoSizeRowCriteria HeaderAndColumns dataGridView Rows Count false);
可以绑定并显示对象
Bind Objects to Windows Forms DataGridView Controls
可以改变表格线条风格
Change the Border and Gridline Styles in the Windows Forms DataGridView ControlSamples:this dataGridView GridColor = Color BlueViolet;this dataGridView BorderStyle = BorderStyle Fixed D;this dataGridView CellBorderStyle = DataGridViewCellBorderStyle None;this dataGridView RowHeadersBorderStyle = DataGridViewHeaderBorderStyle Single;this dataGridView ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle Single;
动态改变列是否显示 和动态改变列的显示顺序
Change the Order of the Columns in the Windows Forms DataGridView ControlSamples customersDataGridView Columns[ CustomerID ] Visible = false;customersDataGridView Columns[ ContactName ] DisplayIndex = ;customersDataGridView Columns[ ContactTitle ] DisplayIndex = ;customersDataGridView Columns[ City ] DisplayIndex = ;customersDataGridView Columns[ Country ] DisplayIndex = ;customersDataGridView Columns[ CompanyName ] DisplayIndex = ;
可以在列中显示图像
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 ;dataGridView Columns Insert( iconColumn);
格式化显示内容:
Format Data in the Windows Forms DataGridView ControlSamples:this dataGridView Columns[ UnitPrice ] DefaultCellStyle Format = c ;this dataGridView Columns[ ShipDate ] DefaultCellStyle Format = d ;this dataGridView DefaultCellStyle NullValue = no entry ;this dataGridView DefaultCellStyle WrapMode = DataGridViewWrapMode Wrap;this dataGridView Columns[ CustomerName ] DefaultCellStyle Alignment =DataGridViewContentAlignment MiddleRight;
在拖动列的滚动条时可以将指定的列冻结
Freeze Columns in the Windows Forms DataGridView ControlSamples 将指定列及以前的列固定不动this dataGridView Columns[ AddToCartButton ] Frozen = true;
获取选择的单元格 行 列
Get the Selected Cells Rows and Columns in the Windows Forms DataGridView ControlSamples:
显示录入时出现的错误信息
Handle Errors that Occur During Data Entry in the Windows Forms DataGridView ControlSamples private void dataGridView _DataError(object sender DataGridViewDataErrorEventArgs e)// If the data source raises an exception when a cell value is// mited display an error message if (e Exception != null &&e Context == DataGridViewDataErrorContext Commit)MessageBox Show( CustomerID value must be unique );
大数据量显示采用Virtual Mode
Implement Virtual Mode in the Windows Forms DataGridView Control
设置指定的列只读
Make Columns in the Windows Forms DataGridView Control Read OnlySamples dataGridView Columns[ CompanyName ] ReadOnly = true;
移去自动生成的
列
Remove Autogenerated Columns from a Windows Forms DataGridView ControlSample:dataGridView AutoGenerateColumns = true;dataGridView DataSource = customerDataSet;dataGridView Columns Remove ( Fax );或 dataGridView Columns[ CustomerID ] Visible = false;
自定义选择模式
Set the Selection Mode of the Windows Forms DataGridView ControlSample this dataGridView SelectionMode = DataGridViewSelectionMode FullRowSelect;this dataGridView MultiSelect = false;
自定义设定光标进入单元格是否编辑模式(编辑模式)
Specify the Edit Mode for the Windows Forms DataGridView Controlthis dataGridView EditMode = DataGridViewEditMode EditOnEnter;
新行指定默认值
Specify Default Values for New Rows in the Windows Forms DataGridView ControlSample private void dataGridView _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 = ;e Row Cells[ Region ] Value = NA ;e Row Cells[ Country ] Value = USA ;e Row Cells[ CustomerID ] Value = NewCustomerId();
数据验证
Validate Data in the Windows Forms DataGridView ControlSamples private void dataGridView _CellValidating(object sender DataGridViewCellValidatingEventArgs e)// Validate the CompanyName entry by disallowing empty strings if (dataGridView Columns[e ColumnIndex] Name == CompanyName )if (e FormattedValue ToString() == String Empty)dataGridView Rows[e RowIndex] ErrorText = Company Name must not be empty ;e Cancel = true;
数据提交到dataset中
DataSet ds = new DataSet( MyDataSet );ds Tables[biaom Trim()] Rows Clear();tryfor (int i = ; i < dataGridView Rows Count ; i++)DataTable dt = ds Tables[biaom Trim()];DataRow myrow = ds Tables[biaom Trim()] NewRow();for (int j = ; j < dataGridView Columns Count; j++)myrow[j] = Convert ToString(dataGridView Rows[i] Cells[j] Value);ds Tables[biaom Trim()] Rows Add(myrow);catch (Exception)MessageBox Show( 输入类型错误! );return;
cha138/Article/program/net/201311/11879相关参考
c#DataGridView数据绑定编程与显示 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! &
知识大全 C# WinForm下DataGridView单元按钮列
C#WinForm下DataGridView单元按钮列 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧
DataGridView的常用用法 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! DataGri
C#封装Word常用操作类 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! usingSystem
C#封装Excel常用操作类 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! usingS
知识大全 asp.net中DataGridView的常用用法
asp.net中DataGridView的常用用法 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
DataGridView操作技巧 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 通常会设置的Da
C#文件操作全收录 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 本文收集了目前最为常用的C#经
Oracle中对时间操作的一些总结 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! Oracle中
Java中对文件的读写操作之比较 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! Java对文件进