Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#4.0
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
 
   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));
                }
}
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
 
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
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 6-Feb-13 7:26am
Edited 6-Feb-13 8:48am
v4

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

try this
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
  Permalink  
Comments
shivani 2013 at 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 Moideen at 6-Feb-13 13:20pm
   
how many columns you added?
Jameel Moideen at 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 at 6-Feb-13 13:23pm
   
4
Jameel Moideen at 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
Jameel Moideen at 6-Feb-13 13:25pm
   
then remove i+1 from the loop.
shivani 2013 at 6-Feb-13 13:32pm
   
I need to add the data from textboxes to datagrid using datatable..........
in start, I entered data in respective textboxes and clicked "ädd" button .it entered into grid...now again i entered different data for respective textboxes...it should enter in to grid and should retain previous row data too.while doing this i am getting error told before
Jameel Moideen at 6-Feb-13 13:38pm
   
that is your mistake.Please make sure that the number of column should be equal to number of items in the loop. if it is not work please post your code you have tried
shivani 2013 at 6-Feb-13 13:43pm
   
ok i am posting my code.
i acknowledge you for my help....
shivani 2013 at 6-Feb-13 13:49pm
   
i have updated my code.pls see to it...
regards
Jameel Moideen at 6-Feb-13 13:55pm
   
change your loop like this for (int i = 0; i < grdDetails.Count; i++) also initialize dtDetails on the class level
shivani 2013 at 6-Feb-13 14:04pm
   
wat u wrote now i did initially same ...but got that same error........secondly i want to add items into grid depending upon products in dropdownlist so i delebrately did that so that my loop should start from 0 till dropdownlist count

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



Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 6 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100