Click here to Skip to main content
15,891,621 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.

C#
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.
Posted
Updated 16-Sep-19 2:43am
v2
Comments
Hiren solanki 17-Sep-10 2:17am    
The way you have added new row is I think a answer itself

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



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. :)
 
Share this answer
 
v4
 
Share this answer
 
Comments
call to .net 17-Sep-10 2:12am    
Here I am not accessing records from database.
i am putting input from front end means from textbox and first i want to all these records come to datatable and then save to database.
Al-Farooque Shubho 17-Sep-10 6:47am    
Doesn't matter. All you need is to add a new row in the DataTable and then save the DataSet as shown in the given link.
That is:

DataRow row = EmployeeTable.NewRow();
row["FirstName"] = txtFirstName.Text;
row["LastName"] = txtLastName.Text;
EmployeeTable.Rows.Add(row);

// Update data adapter, this will actually insert a new row in the database
adapter.Update(ds, "Employee");

I would suggest you to learn the basics of DataSet usage and also hope to see you reconsidering the vote for the answer. The following link may help you:

http://msdn.microsoft.com/en-us/library/aa984437%28v=VS.71%29.aspx
C#
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();
} 


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)
 
Share this answer
 
v3
Comments
Tom Marvolo Riddle 30-Oct-13 3:46am    
The Thread is very old.Please Don't answer for old threads.
VICK 30-Oct-13 4:14am    
Oops, I did the same(posted a solution), just by seeing this one in active questions and without looking on the date. Well, Hope it gonna be helpful to some one.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900