在C#中向datagridView添加新行 [英] add new row in to datagridView in C#

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

问题描述





我正在做一个小小的步骤,在c#中将任意选择的MDb表放到我的datagridview中。



我需要的是当我点击Add按钮时它应该在datagridview中返回一个带有空单元格的行,它应该反映在数据库中。



P.Rupesh

Hi,

I'm doing a small trail to get Any Selected MDb tables in to my datagridview in c#.

What I need is when I click on Add button it should return a row with empty cells in the datagridview and it should reflect in database.

P.Rupesh

推荐答案

Hey...
To add new row do like this....

DataSet ds = (DataSet)GV.DataSource;

DataRow dr = ds.Tables[0].NewRow();
ds.Tables[0].Rows.Add(dr);

GV.DataSource = ds;
GV.DataBind();

check it out..............:thumbsup:


添加新的行使用下面的代码,



For add a new row use the below code,

DataTable dttable = new DataTable();
DataColumn column;
DataRow row;

column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "Empid";
dttable.Columns.Add(column);

column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName = "EmpName";
dttable.Columns.Add(column);

row = table.NewRow();
row["id"] = 1;
row["item"] = "Albert";
dttable.Rows.Add(row);

Gridview1.DataSource = dttable;



现在你尝试下一步更新到db。


now you try next step for update to db.


DataRow dr = dt.Tables [0]。 NewRow();

dt.Tables [0] .Rows.Add(dr);
DataRow dr=dt.Tables[0].NewRow();
dt.Tables[0].Rows.Add(dr);


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

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