Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Its feeling good to be on Codeproject after a long time.

My issue is about adding a row to gridview dinamically.

In past I have done like Gridview.rows.add(bla bla).

Now I have moved to VS 2010 & Framework 4.0.

Its strange that I am not able to find method Add() for adding new row.

The intellisence doesn't giving me Add() after "Gridview.Rows.".

Make sure I dont wont to bind gridview with any datasource.

Just want to add row to gridview.

Thanks,
Hemant Thakar
Posted

Ya,you are right.A row can't be added to gridview directly but if you want to add a row in gridview,you can add it in datasource i.e. datatable and then give it as a datasource to gridview.
 
Share this answer
 
Comments
Hemant Thaker 30-Dec-11 0:44am    
True.,
But what if i dont want to bind it with datasource.
That means after binding my gridview will be refreshed and data which was there previously will be lost.
Let me be clear.
I have only one column in gridview and that is template column.
I have put File Uploader control on that.
I want user to add rows dinamically, so that he has option to upload as many documents as he wants.
Supriya Srivastav 30-Dec-11 0:49am    
k,for that you can maintain session and put your datatable in that session variable.
Please use below code for adding a new row to grid

C#
DataTable _dtBands = ((DataTable)ViewState["Data"]).Clone();

        DataRow row;
        if (_type == "ADD")
        {
            row = _dtBands.NewRow();
            row["Item_ID"] = "";
            row["Item_Name"] = "";
            row["UOM"] = "";
            row["UOM_id"] = "";
            row["Quantity"] = "0";
            row["Tolrence"] = "0";
            row["markup"] = "0";
            row["markup_Fixed"] = "0";

            _dtBands.Rows.Add(row);
            for (int i = 0; i < grdMain.Rows.Count; i++)
            {
                row = _dtBands.NewRow();
                row["Item_ID"] = ((DropDownList)grdMain.Rows[i].FindControl("ddlItemName")).SelectedValue.ToString();
                row["Item_Name"] = ((DropDownList)grdMain.Rows[i].FindControl("ddlItemName")).SelectedItem.Text;
                row["UOM"] = ((Label)grdMain.Rows[i].FindControl("lblUOM")).Text;
                row["UOM_id"] = ((HiddenField)grdMain.Rows[i].FindControl("hdnuomid")).Value;
                row["Quantity"] = ((TextBox)grdMain.Rows[i].FindControl("txtqty")).Text;
                row["Tolrence"] = ((TextBox)grdMain.Rows[i].FindControl("txttolrence")).Text;
                row["markup"] = ((TextBox)grdMain.Rows[i].FindControl("txtmarkup")).Text;
                row["markup_Fixed"] = ((TextBox)grdMain.Rows[i].FindControl("txtmarkupFixed")).Text;
                _dtBands.Rows.Add(row);
            }            
        }


grdMain.DataSource = _dtBands ;
grdMain.DataBind();
 
Share this answer
 
v2
public partial class Admin_Invoicesl : UserControlDataAccess
{
    DataTable productsDt = new DataTable();
    DataTable GvDT=new DataTable();
    DataRow GVr = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        productsDt = DA.GetProducts(null, null, null, null, "2");
        if (!IsPostBack)
        {
            SetInitialRow();
        }
        
            
            
        

    }


    private void SetInitialRow()
    {
        GvDT.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        GvDT.Columns.Add(new DataColumn("Product", typeof(string)));
        GvDT.Columns.Add(new DataColumn("Qty", typeof(string)));
        GVr = GvDT.NewRow();
        GVr["RowNumber"] = 1;
        GVr["Product"] = string.Empty;
        GVr["Qty"] = string.Empty;
        GvDT.Rows.Add(GVr);
        Session["DT"] = GvDT;
        Session.Timeout = 60;
        Gridview1.DataSource = (DataTable)Session["DT"];
        Gridview1.DataBind();
    }

    private void AddNewRowToGrid()
    {
        setprevData();
        DataTable dt = (DataTable)Session["DT"];
        GVr = dt.NewRow();
        GVr["RowNumber"] = dt.Rows.Count+1;
        GVr["Product"] = string.Empty;
        GVr["Qty"] = string.Empty;
        dt.Rows.Add(GVr);
        Session["DT"] = dt;

    }
    protected void setprevData()
    {
        DataTable prevdata = (DataTable)Session["DT"];
        for (int i = 0; i < Gridview1.Rows.Count; i++)
        {
            prevdata.Rows[i]["Product"] = ((DropDownList)Gridview1.Rows[i].FindControl("DProducts")).SelectedValue.ToString();
            prevdata.Rows[i]["Qty"] = ((TextBox)Gridview1.Rows[i].FindControl("T_Quantity")).Text;



        }

    }


  

    protected void Button1_Click(object sender, EventArgs e)
    {

        AddNewRowToGrid();
        Gridview1.DataSource = (DataTable)Session["DT"];
        Gridview1.DataBind();
    }
 
Share this answer
 

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