Click here to Skip to main content
15,880,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one form in which 5 textboxes for product id ,qty,price,amount..when i enter the data and press "add" button it is getting placed in gridview.....but when i add another product with some other qty it addd it and remove the previous.it is showing one data which is currently.i don't want to loose previous it should add row wise...how can i achieve this.following is the coding

C#
   protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dtDetails = new DataTable();
        DataTable dt = new DataTable();
        dt = objprdt.getproducts();
        ddlprdcts.DataSource = dt;
        ddlprdcts.DataValueField = "Product_Id";
        ddlprdcts.DataTextField = "Product_Name";
        ddlprdcts.DataBind();
        ddlprdcts.Items.Add(new ListItem("- Please Select -", "-1"));
        ddlprdcts.SelectedValue = "-1";


         if (!this.IsPostBack)
            {
 dtDetails.Clear();
                if (dtDetails.Columns.Count == 0)
                {
                    dtDetails.Columns.Add("sl_no", typeof(int));
                    dtDetails.Columns.Add("productname", typeof(string));
                    dtDetails.Columns.Add("qty", typeof(double));
                    dtDetails.Columns.Add("price", typeof(decimal));
                    dtDetails.Columns.Add("amount", typeof(long));
                }
}

C#
public void filldtDetails()
       {
           dtDetails.Clear();
           if (dtDetails.Columns.Count == 0)
           {

                   //dtDetails.Columns.Add("sl_no", typeof(int));
                   dtDetails.Columns.Add("productname", typeof(string));
                   dtDetails.Columns.Add("qty", typeof(double));
                   dtDetails.Columns.Add("price", typeof(decimal));
                   dtDetails.Columns.Add("amount", typeof(double));


           }
           if (grdDetails.Rows.Count != 0)
           {
               for (int i = 0; i < Convert.ToInt32(ddlprdcts.Items.Count); i++)
               {
                   dtDetails.Rows.Add(grdDetails.Rows[i].Cells[1].Text, grdDetails.Rows[i].Cells[2].Text, grdDetails.Rows[i].Cells[3].Text, grdDetails.Rows[i].Cells[4].Text);

                   //dtDetails.Rows.Add(grdDetails.Rows[i].Cells[1].Text, Convert.ToDouble(grdDetails.Rows[i].Cells[2].Text), Convert.ToDecimal(grdDetails.Rows[i].Cells[3].Text), Convert.ToDouble(grdDetails.Rows[i].Cells[4].Text));

               }

           }
       }

on button click

C#
filldtDetails();
        DataRow dr1 = dtDetails.NewRow();
        //dr1[0] = TextBox2.Text;
        dr1[0] = TextBox2.Text;
        dr1[1] = TextBox4.Text;
        dr1[2] = TextBox3.Text;
        dr1[3] = TextBox5.Text;
        dtDetails.Rows.Add(dr1);
        grdDetails.DataSource = dtDetails;
        grdDetails.DataBind();


on source code
XML
Enter Email ID    <asp:TextBox ID="TextBox1" runat="server" 
        style="margin-bottom: 0px"></asp:TextBox>
 <br />
    <br />
Name                
    <asp:TextBox ID="TextBox2" runat="server" ontextchanged="TextBox2_TextChanged"></asp:TextBox>
                      
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Select" />
    <br />
 product            
    <asp:DropDownList ID="ddlprdcts" runat="server" 
        onselectedindexchanged="ddlprdcts_SelectedIndexChanged">    </asp:DropDownList>
                                    
    <asp:Button ID="select" runat="server" onclick="select_Click" Text="select" />
    <br />
    <br />
 product cost    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    <br />
    <br />
 Product qty      <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
    <br />
    <br />
 product total amount    <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
     
    <asp:Button ID="Button3" runat="server" onclick="Button3_Click" 
        Text="calculate" />
                     
    <asp:Button ID="Button2" runat="server" Height="22px" onclick="Button2_Click" 
        Text="Add to cart" Width="79px" />
    <br />
    <asp:GridView ID="grdDetails" runat="server" Height="139px" Width="507px">
        <Columns>
            <asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Edit" />
            <asp:ButtonField ButtonType="Button" CommandName="Delete" Text="remove" />
        </Columns>
    </asp:GridView>
    <br />
    <br />
    <asp:Button ID="Button4" runat="server" Height="26px" Text="total amount" />
     
    <asp:Label ID="Label1" runat="server"></asp:Label>



i don't want to add it into database.i just want to show it runtime only
pls help
Posted
Updated 6-Feb-13 7:48am
v4

1 solution

try this
C#
public class YourClass
{
 DataTable dtDetails = new DataTable();
  protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!this.IsPostBack)
            {
 dtDetails.Clear();
                if (dtDetails.Columns.Count == 0)
                {
                    dtDetails.Columns.Add("sl_no", typeof(int));
                    dtDetails.Columns.Add("productname", typeof(string));
                    dtDetails.Columns.Add("qty", typeof(double));
                    dtDetails.Columns.Add("price", typeof(decimal));
                    dtDetails.Columns.Add("amount", typeof(long));
                }
}
}
 public void filldtDetails()
        {
            dtDetails.Clear();
            if (dtDetails.Columns.Count == 0)
            {
               
                    dtDetails.Columns.Add("sl_no", typeof(int));
                    dtDetails.Columns.Add("productname", typeof(string));
                    dtDetails.Columns.Add("qty", typeof(double));
                    dtDetails.Columns.Add("price", typeof(decimal));
                    dtDetails.Columns.Add("amount", typeof(long));
               
         
            }
            if (grdDetails.Rows.Count != 0)
            {
                for (int i = 0; i < grdDetails.Rows.Count; i++)
                {
                    dtDetails.Rows.Add(i + 1, grdDetails.Rows[i].Cells[1].Text, grdDetails.Rows[i].Cells[2].Text, grdDetails.Rows[i].Cells[3].Text, grdDetails.Rows[i].Cells[4].Text);
                }

            }
        }
protected void Button2_Click(object sender, EventArgs e)
    { 
       filldtDetails();
        DataRow dr1 = dtDetails.NewRow();
        dr1[0] = TextBox2.Text;
        dr1[1] = TextBox2.Text;
        dr1[2] = TextBox4.Text;
        dr1[3] =TextBox3.Text;
        dr1[4] =TextBox5.Text;
        dtDetails.Rows.Add(dr1);
        GridView1.DataSource = dtDetails;
        GridView1.DataBind();
        
       
    }
}


Hope this helps
 
Share this answer
 
Comments
shivani 2013 6-Feb-13 13:15pm    
i am getting one error on dtDetails.Rows.Add(i + 1, grdDetails.Rows[i].Cells[1].Text, grdDetails.Rows[i].Cells[2].Text, grdDetails.Rows[i].Cells[3].Text, grdDetails.Rows[i].Cells[4].Text); when i click again add cart with another value
pls help
"Input array is longer than the number of columns in this table."
Jameel VM 6-Feb-13 13:20pm    
how many columns you added?
Jameel VM 6-Feb-13 13:22pm    
if you add four columns make sure that the same count should be in the loop.It's a simple error
shivani 2013 6-Feb-13 13:23pm    
4
Jameel VM 6-Feb-13 13:24pm    
i have added SI no column.please remove if you don't need.also remove i+1 from the loop

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