在datagridview中添加行 [英] adding rows in datagridview

查看:87
本文介绍了在datagridview中添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我在datagridview中创建了这样的列

 DataGridViewColumn newCol =  DataGridViewColumn(); // 在网格中添加一列

DataGridViewCell单元格=  DataGridViewTextBoxCell(); // 指定中的哪种单元格
newCol.CellTemplate =单元格;
newCol.HeaderText = " ;
 newCol.Name = " ;
 newCol.Visible =  true ;
newCol.Width =  40 ; 



现在我想为该列创建行.如果有人知道它对我有帮助.

解决方案

 有几种情况.我有两个想法,在这里讨论.如果您还有其他情况,请指定
//  1-如果您先创建一个dataTable,然后将bindingSource分配给GridView 
DataTable tbl =  DataTable(" ); // 创建表
tbl.columns.添加( DataColumn("  ColName")); // 创建列

DataRow dr = tbl.NewRow();
dr(" )= "  MyText";

DataGridView1.DataSource = tbl;

//  2-如果dataGrid是根据设计器形式设计的,并且包含1到n个单元格..

对象 [] arrObj =  对象 [ DataGridView1.Columns.Length]; // 获取列数
arrObj [ 0 ] = "  1 ] = " 跨度>;
.
.
.
obj [n] = " ;

DataGridView1.rows.添加(arrObj)


hi i created a column in datagridview like that

DataGridViewColumn newCol = new DataGridViewColumn(); // add a column to the grid

DataGridViewCell cell = new DataGridViewTextBoxCell(); //Specify which type of cell in 
newCol.CellTemplate = cell;
newCol.HeaderText = "Periods";
 newCol.Name = "Period";
 newCol.Visible = true;
newCol.Width = 40;



now i want to create rows for that column .if anyone knows about it help me.

解决方案

//There are several scenarios. i had two in mind and discussed here. if you have some other scenario..please specify
//1- In case you first create a dataTable and then assign bindingSource to GridView
DataTable tbl = new DataTable("TableName"); //Create Table
tbl.columns.add(new DataColumn("ColName")); //Create Column

DataRow dr = tbl.NewRow();
dr("ColName") = "MyText";

DataGridView1.DataSource = tbl;

//2-if dataGrid is designed from designer form and contain 1 to n number of cells..

object[] arrObj = new object[DataGridView1.Columns.Length]; //GetNumber of columns
arrObj[0] = "Value for column1";
arrObj[1] = "Value for Column2";
.
.
.
obj[n] = "Value for Column n";

DataGridView1.rows.add(arrObj)


这篇关于在datagridview中添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆