且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

添加排在GridView C#

更新时间:2023-12-06 16:06:10

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

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