添加排在GridView C# [英] Add rows in a gridview c#

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

问题描述

林在asp.net中新。我想知道如何在一个GridView添加行编程。我能做到这一点,但它只是播放最新的加法。
这里是我的code:

  DataTable的DT =新的DataTable();
dt.Columns.Add(问题);
dt.Columns.Add(答);DataRow的博士= dt.NewRow();
博士[问题] = txtQuestion.Text;
博士[答案] = txtAnswer.Text;
dt.Rows.Add(DR);
dt.AcceptChanges();gvQnA.DataSource = DT;
gvQnA.DataBind();


解决方案

它,因为你正在创造新的表中的每个时间和与电网结合它

DO code作为下面可以解决您的问题...

在这里,我利用现有的数据源,并增加了两个排再结合它...

  DataTable的DT = gridView.DataSource的数据表;如果(DT!= NULL)
{  DataRow的博士= dt.NewRow();
  博士[问题] = txtQuestion.Text;
  博士[答案] = txtAnswer.Text;
  dt.Rows.Add(DR);
  dt.AcceptChanges();  gvQnA.DataSource = DT;
  gvQnA.DataBind();
}

Im new in asp.net. I want to know how to add a row in a gridview programatically. I was able to do it but it just displays the latest addition. Here is my code:

DataTable dt = new DataTable();
dt.Columns.Add("Question");
dt.Columns.Add("Answer");

DataRow dr = dt.NewRow();
dr["Question"] = txtQuestion.Text;
dr["Answer"] = txtAnswer.Text;
dt.Rows.Add(dr);
dt.AcceptChanges();

gvQnA.DataSource = dt;
gvQnA.DataBind();

解决方案

Its because you are creating new table each time and binding it with the grid

Do code as below may resolve your issue ...

here i am taking existing datasource and binding it again by adding two more row...

DataTable dt = gridView.DataSource as DataTable;

if (dt != null)
{

  DataRow dr = dt.NewRow();
  dr["Question"] = txtQuestion.Text;
  dr["Answer"] = txtAnswer.Text;
  dt.Rows.Add(dr);
  dt.AcceptChanges();

  gvQnA.DataSource = dt;
  gvQnA.DataBind();
}

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

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