如何在数据表中添加新行 [英] how can i add new row in datatable

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

问题描述

我想在数据表中添加一个空白行,当我在数据表中添加时,我现在该怎么办?删除并替换为我的代码的新行。



I want to add a blank row in data table how can i do right now when i am adding in data table last one is remove and replace with new row my code is.

protected void btnAdd_Click(object sender, EventArgs e)
       {
           double a, b;
           a = double.Parse(txtqty.Text);
           b = a * double.Parse(LblRate1.Text);
           txtAmount.Text = b.ToString();

           DataTable dt;
           DataRow dr;
           if (GridView1.Rows.Count > 0)
           {
               dt = (DataTable)GridView1.DataSource;
           }
           else
           {
               dt = new DataTable();
           }

           dt.Columns.Add("Product_Name");
           dt.Columns.Add("Product_Quantity");
           dt.Columns.Add("Product_Amount");

           dr = dt.NewRow();

           dr["Product_Name"] = DropDownList1.SelectedItem.Text;
           dr["Product_Quantity"] = txtqty.Text;
           dr["Product_Amount"] = txtAmount.Text;
           dt.Rows.Add(dr);


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





添加新行的代码是什么。



what will be the code for adding new row.

推荐答案

您可以在数据表中添加一个新行而不会丢失最后一行。









You can add a new row in datatable without loosing the last row by following.




DataTable dt = new DataTable();
dt.Columns.Add("Column1");
dt.Columns.Add("Column2");
DataRow row = dt.NewRow();
row["Column1"] = "Hello";
row["Column2"] = "Hello2";

dt.Rows.Add(row);

DataRow row2 = dt.NewRow();
dt.Rows.Add(row2);







我测试了这个..它首先添加了一行包含适当的数据和而不是在下面添加一个空白行。



以下代码在datatable中成功添加两个同步行。






I have tested this.. it first adds a row with proper data and than add a blank row below.

and the following code is adding two simultaneous rows in datatable successfully.

DataTable dt = new DataTable();
    dt.Columns.Add("Column1");
    dt.Columns.Add("Column2");
    DataRow row = dt.NewRow();
    row["Column1"] = "Hello";
    row["Column2"] = "Hello2";

    dt.Rows.Add(row);

    DataRow row2 = dt.NewRow();
    row2[0] = "Row2-Col1";
    row2[1] = "Row2-Col2";
    dt.Rows.Add(row2);



希望它会有所帮助。 :)


Hope it will help. :)


请参阅 http://www.java2s。 com / Tutorial / CSharp / 0560__ADO.Net / UseDataTabletoinsertaRow.htm [ ^ ]


DataTable dt = new DataTable();
 DataRow dr=null;
protected void Page_Load(object sender, EventArgs e)
{
 dt.Columns.Add("Product_Name");
            dt.Columns.Add("Product_Quantity");
            dt.Columns.Add("Product_Amount");
}

protected void btnAdd_Click(object sender, EventArgs e)
{
if (ViewState["table"] != null)
{
dt = (DataTable)ViewState["table"];
}
double a, b;
a = double.Parse(txtqty.Text);
b = a * double.Parse(LblRate1.Text);
txtAmount.Text = b.ToString();
 
 if (ViewState["table"] != null)
            {
                dt = (DataTable)ViewState["table"];
            }
            dr = dt.NewRow();
            dr["Product_Name"] = DropDownList1.SelectedItem.Text;
            dr["Product_Quantity"] = txtqty.Text;
            dr["Product_Amount"] = txtAmount.Text;
            dt.Rows.Add(dr);
            ViewState["table"] = dt;

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





希望itz按照您的要求完美运行...每次我们必须在viewstate中分配数据表将数据存储在客户端,因为每个服务器请求它将为数据创建新对象,并支持new关键字。希望它有利...

by

Aswin(Swathi Group)



Hope itz works perfectly as per your requirement...everytime we have to assign the datatable in the viewstate to store the data in client side because every server request it will create new object for datatable with the support of 'new' keyword..Hope itz favourable...
by
Aswin(Swathi Group)


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

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