欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# DatagridView常用操作汇总

程序员文章站 2024-02-21 13:07:46
本文汇总了c#中datagridview的常用操作,有助于读者加深对c# datagridview用法的理解,具体如下: 1、(最基本的技巧)、获取某列中的某行(某单元格...

本文汇总了c#中datagridview的常用操作,有助于读者加深对c# datagridview用法的理解,具体如下:

1、(最基本的技巧)、获取某列中的某行(某单元格)中的内容

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

2、自定义列

//定义列宽
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 their
behavior and appearance
host controls in windows forms datagridview cells

    继承 datagridviewtextboxcell 类生成新的cell类,然后再继承 datagridviewcolumn 生成新的column类,并指定
celltemplate为新的cell类。新生成的column便可以增加到datagridview中去。

3、自动适应列宽

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

4、可以绑定并显示对象

bind objects to windows forms datagridview controls

5、可以改变表格线条风格

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;

6、动态改变列是否显示,和动态改变列的显示顺序

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;

7、可以在列中显示图像

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

8、格式化显示内容:

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;

9、将指定列及以前的列固定不动

this.datagridview1.columns["addtocartbutton"].frozen = true;

10、显示录入时出现的错误信息

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、设置指定的列只读

datagridview1.columns["companyname"].readonly = true;

13、移去自动生成的列

datagridview1.autogeneratecolumns = true;
datagridview1.datasource = customerdataset;
datagridview1.columns.remove ("fax");

或:

datagridview1.columns["customerid"].visible = false;

14、自定义选择模式

this.datagridview1.selectionmode = datagridviewselectionmode.fullrowselect;
this.datagridview1.multiselect = false;

15、自定义设定光标进入单元格是否编辑模式(编辑模式)

this.datagridview1.editmode = datagridvieweditmode.editonenter;

16、新行指定默认值

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、数据验证

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