Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello sir. if any body know the correct solution plz helpme. it is very urgent to me.

my requirement is i have a gridview with 15 boundfields. out side the grid and on the page i have 15 textboxes. and one submit button. when ever i am inserting some text in the textboxes and press submit button. these values should be displayed on the gridview. now i want to insert 100 rows in the gridview, but only one record is inserting. when ever i am entering second record it is overriding first record. if anybody know plz help me sir.

it is very urgent to me. i am struggled from 3 days.
plz plz help me. any one.
my code is helow.
XML
<asp:GridView ID="GVProductOrder" runat="server" AutoGenerateColumns="false"
                                               GridLines="Vertical" Height="200" Width="1050">
                                               <HeaderStyle CssClass="HeaderStyleGrid" />
                                               <Columns>
                                                   <asp:BoundField DataField="ItemCode" HeaderText="Item Code" HeaderStyle-Width="150px"/>

                                                   <asp:BoundField DataField="ItemName" HeaderText="Item Name" HeaderStyle-Width="500px"/>
                                                   <asp:BoundField DataField="EanCode" HeaderText="Ean Code" HeaderStyle-Width="100px"/>
                                                   <asp:BoundField DataField="Cost" HeaderText="Cost" HeaderStyle-Width="150px"/>
                                                   <asp:BoundField DataField="MRP" HeaderText="MRP" HeaderStyle-Width="150px"/>
                                                   <asp:BoundField DataField="SP" HeaderText="SP" HeaderStyle-Width="150px"/>
                                                   <asp:BoundField DataField="MBQ" HeaderText="MBQ" HeaderStyle-Width="100px"/>
                                                   <asp:BoundField DataField="Quantity" HeaderText="Quantity" HeaderStyle-Width="100px"/>
                                                   <asp:BoundField DataField="FreeQty" HeaderText="Free Qty" HeaderStyle-Width="100px"/>
                                                   <asp:BoundField DataField="FixedDisc" HeaderText="Fixed Disc" HeaderStyle-Width="100px"/>
                                                   <asp:BoundField DataField="Tax" HeaderText="Tax" HeaderStyle-Width="100px"/>
                                                   <asp:BoundField DataField="Value" HeaderText="Value" HeaderStyle-Width="100px"/>
                                                   <asp:BoundField DataField="OnOrder" HeaderText="On Order" HeaderStyle-Width="100px"/>
                                                   <asp:BoundField DataField="Remarks" HeaderText="Remarks" HeaderStyle-Width="500px"/>
                                                   <asp:BoundField DataField="Type" HeaderText="Type" HeaderStyle-Width="100px"/>

C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class PurchaseOrder1 : System.Web.UI.Page
{
    
    DataTable dt = new DataTable();
    protected void GVProductOrderBind()
    {
        ViewState["GVTable"]=dt;
        GVProductOrder.DataSource = dt;
        GVProductOrder.DataBind();
        
        
    }
    protected void InsertToGrid()
    {
        
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            DataRow dr = null;
            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");
            dr = dt.NewRow();
            dt.Rows.Add(dr);
            GVProductOrderBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        
    }
    protected void BtnClose_Click(object sender, EventArgs e)
    {
        Response .Redirect ("~/Welcome.aspx");
    }
    protected void BtnSave_Click(object sender, EventArgs e)
    {      
        try
        {
            if (ViewState["GVTable"] != null)
            {
                DataTable dtCurrentTable = (DataTable)ViewState["GVTable"];
                DataRow drCurrentRow = null;
                if (dtCurrentTable.Rows.Count > 0)
                {
                    for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                    {
                        drCurrentRow = dtCurrentTable.NewRow();
                       
                        drCurrentRow["ItemCode"] = TxtItemCode.Text;
                        drCurrentRow["ItemName"] = TxtItemDescription.Text;
                        drCurrentRow["EanCode"] = TxtEanCode.Text;
                        drCurrentRow["Cost"] = TxtCP.Text;
                        drCurrentRow["MRP"] = TxtMRP.Text;
                        drCurrentRow["SP"] = TxtSP.Text;
                        drCurrentRow["MBQ"] = TxtACP.Text;
                        drCurrentRow["Quantity"] = TxtQty.Text;
                        drCurrentRow["FreeQty"] = TxtFreeQty.Text;
                        drCurrentRow["FixedDisc"] = TxtDiscPercentage.Text;
                        drCurrentRow["Tax"] = TxtTaxPercentage.Text;
                        drCurrentRow["Value"] = TxtValue.Text;
                        drCurrentRow["OnOrder"] = TxtOnOrder.Text;
                        drCurrentRow["Remarks"] = TxtQOH.Text;
                        drCurrentRow["Type"] = TxtUOP.Text;
                    }
                    dtCurrentTable.Rows.Add(drCurrentRow);
                    //Store the current data to ViewState
                    ViewState["CurrentTable"] = dtCurrentTable;
                    //Rebind the Grid with the current data
                    GVProductOrder.DataSource = dtCurrentTable;
                    GVProductOrder.DataBind();
                }
            }
            else
            {
                Response.Write("ViewState is null");
            }
            SetPreviousData();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        
    }
    private void SetPreviousData()
    {
        //int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 1; i < dt.Rows.Count; i++)
                {
                    //TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    //TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    //TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
                    //box1.Text = dt.Rows[i]["ItemCode"].ToString();
                    //box2.Text = dt.Rows[i]["Column2"].ToString();
                    //box3.Text = dt.Rows[i]["Column3"].ToString();
                    DataRow drCurrentRow = dt.NewRow();
                    drCurrentRow["ItemCode"] = TxtItemCode.Text;
                    drCurrentRow["ItemName"] = TxtItemDescription.Text;
                    drCurrentRow["EanCode"] = TxtEanCode.Text;
                    drCurrentRow["Cost"] = TxtCP.Text;
                    drCurrentRow["MRP"] = TxtMRP.Text;
                    drCurrentRow["SP"] = TxtSP.Text;
                    drCurrentRow["MBQ"] = TxtACP.Text;
                    drCurrentRow["Quantity"] = TxtQty.Text;
                    drCurrentRow["FreeQty"] = TxtFreeQty.Text;
                    drCurrentRow["FixedDisc"] = TxtDiscPercentage.Text;
                    drCurrentRow["Tax"] = TxtTaxPercentage.Text;
                    drCurrentRow["Value"] = TxtValue.Text;
                    drCurrentRow["OnOrder"] = TxtOnOrder.Text;
                    drCurrentRow["Remarks"] = TxtQOH.Text;
                    drCurrentRow["Type"] = TxtUOP.Text;
                    //rowIndex++;
                }
            }
        }
    }
}
Posted
Updated 28-Nov-10 20:56pm
v2
Comments
Sunasara Imdadhusen 29-Nov-10 3:00am    
Don't use URGENT word in question!!

1 solution

Where are you calling "SetPreviousData"?
also "SetPreviousData" does not have the code to add the new row to dataTable.
And what is the correlation between ViewSTate["GVTable"] and ViewState["CurrentTable"]?
Debug the code giving special emphasis on regions where you are putting DataTable to viewstate and where you are setting dataSource for the grid.
 
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