Click here to Skip to main content
14,732,999 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello sir.
i have a gridview with 15 boundfields. i want fill gridview with textboxes with submit button. i filled one row. but i want to create a new row and add that new row to gridview below first row. to do that how can i write the code.
plz see my code and help me.
thanking you.

DataTable dt = new DataTable();
    protected void GVProductOrderBind()
    {
        GVProductOrder.DataSource = dt;
        GVProductOrder.DataBind();
        Session["GVTable"] = dt;
        
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            int i = GVProductOrder.Rows.Count;
            if (i == 0)
            {
                //dt.Columns.Add("ItemCode", System.Type.GetType("System.String"));
                DataRow dr = dt.NewRow();
                dt.Columns.Add("ItemCode");
                dt.Columns.Add("ItemName");
                dt.Columns.Add("EanCode");
                dt.Columns.Add("Cost");
                dt.Columns.Add("MRP");
                dt.Columns.Add("SP");
                dt.Columns.Add("MBQ");
                dt.Columns.Add("Quantity");
                dt.Columns.Add("FreeQty");
                dt.Columns.Add("FixedDisc");
                dt.Columns.Add("Tax");
                dt.Columns.Add("Value");
                dt.Columns.Add("OnOrder");
                dt.Columns.Add("Remarks");
                dt.Columns.Add("Type");

                dt.Rows.Add(dr);
                GVProductOrderBind();

                //Session["tab"] = dt;
            }
            else
            {
                //dt.Rows.Add(row);               
                if (Session["GVTable"] != null)
                {
                    ((DataTable)Session["GVTable"]).Merge(dt);
                }

                //for (int j = 0; j <= i + 1; j++)
                //{
                //    if (j == (i + 1))
                //    {
                //        DataRow dr1;
                //        dr1 = dt.NewRow();
                //        dr1["slno"] = j;
                //        dt.Rows.Add(dr1);
                //        GVProductOrder.DataSource = dt;
                //        GVProductOrder.DataBind();
                //    }

                //}
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        
        //DataSet ds = new DataSet();
        //ds = BusinessLogic.SP_GridDetails();
        //GVProductOrder.DataSource = ds;
        //GVProductOrder.DataBind();
    }
    protected void BtnClose_Click(object sender, EventArgs e)
    {
        Response .Redirect ("~/Welcome.aspx");
    }
    protected void BtnSave_Click(object sender, EventArgs e)
    {
        try
        {
            
                DataRow drNew = dt.NewRow();
                
                drNew["ItemCode"] = TxtItemCode.Text;
                drNew["ItemName"] = TxtItemDescription.Text;
                drNew["EanCode"] = TxtEanCode.Text;
                drNew["Cost"] = TxtCP.Text;
                drNew["MRP"] = TxtMRP.Text;
                drNew["SP"] = TxtSP.Text;
                drNew["MBQ"] = TxtACP.Text;
                drNew["Quantity"] = TxtQty.Text;
                drNew["FreeQty"] = TxtFreeQty.Text;
                drNew["FixedDisc"] = TxtDiscPercentage.Text;
                drNew["Tax"] = TxtTaxPercentage.Text;
                drNew["Value"] = TxtValue.Text;
                drNew["OnOrder"] = TxtOnOrder.Text;
                drNew["Remarks"] = TxtQOH.Text;
                drNew["Type"] = TxtUOP.Text;
               
                dt.Rows.Add(drNew);
                GVProductOrderBind();
            }
           
        
        catch (Exception ex)
        {
            throw ex;
        }
Posted
Updated 26-Nov-10 21:07pm
v2
Comments
raju melveetilpurayil 27-Nov-10 2:08am
   
[Edit] edited for more readability [/Edit]

1 solution

ON page Load Event Create new data table and Add columns to DataTable.


dt = new DataTable();
dt.Columns.Add("ItemCode");
dt.Columns.Add("ItemName");
dt.Columns.Add("EanCode");
dt.Columns.Add("Cost");
dt.Columns.Add("MRP&dt.Columns.Add("SP");
dt.Columns.Add("MBQ");
dt.Columns.Add("Quantity");
dt.Columns.Add("FreeQty");
dt.Columns.Add("FixedDisc");
dt.Columns.Add("Tax");
dt.Columns.Add("Value");
dt.Columns.Add("OnOrder");
dt.Columns.Add("Remarks");
dt.Columns.Add("Type");


and On add button click event declare new Row

DataRow drNew = dt.NewRow();
drNew["ItemCode"] = TxtItemCode.Text;
drNew["ItemName"] = TxtItemDescription.Text;
drNew["EanCode"] = TxtEanCode.Text;
drNew["Cost"] = TxtCP.Text;
drNew["MRP"] = TxtMRP.Text;
drNew["SP"] = TxtSP.Text;
drNew["MBQ"] = TxtACP.Text;
drNew["Quantity"] = TxtQty.Text;
drNew["FreeQty"] = TxtFreeQty.Text;
drNew["FixedDisc"] = TxtDiscPercentage.Text;
drNew["Tax"] = TxtTaxPercentage.Text;
drNew["Value"] = TxtValue.Text;
drNew["OnOrder"] = TxtOnOrder.Text;
drNew["Remarks"] = TxtQOH.Text;
drNew["Type"] = TxtUOP.Text;
dt.Rows.Add(drNew);

After that bind table to datagrid view.
datagridview.datasource=datatabel name.
   
v2
Comments
Ankur\m/ 27-Nov-10 5:08am
   
[moved from answer]
sathish.jampala wrote:
thanks for you quick reply.
now i need to insert another row. now i want to display two records in the gridview. so how can i create a new row here.
plz helpme to know this sir.
thanking you.

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