Click here to Skip to main content
16,019,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add product and quantity in data grid from drop down list and text box. But the problem i am facing is that when i am pressing link button only 1 row is being added and when i add another product it overwrites the previous one.

This is the Code of .aspx

HTML
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
     <ContentTemplate>
     
      <table align=center cellpadding="200px">
          
              <caption>
                  <label>
                  Select Regular Sheesha</label>
                  <tr>
                      <td>
                          <asp:DropDownList ID="Drop_nsheesha" runat="server" 
                              >
                          </asp:DropDownList>
                      </td>
                      <td>
                                
                          <asp:Label ID="Label3" runat="server" Text="Quantity"></asp:Label>
                              
                      </td>
                      <td>
                          <asp:TextBox ID="TextBox_SimpleSheesha" runat="server" width="30px" />
                      </td>
                      <caption>
                          <br />
                          <tr>
                              <td>
   <asp:LinkButton ID="LinkButton1" runat="server"       onclick="LinkButton1_Click">ADD</asp:LinkButton>
                              </td>
                          </tr>
                      </caption>
                  </tr>
              </caption>
         
          
     </table>
    
        <asp:GridView ID="GridView2" runat="server" >
        </asp:GridView>
      
        </ContentTemplate>
         </asp:UpdatePanel>


This is CS Code

C#
DataTable temp = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {

            temp.Columns.Add("Sheesha");
            temp.Columns.Add("Quantity");
         }

 protected void LinkButton1_Click(object sender, EventArgs e)
        {
            temp.Rows.Add();
            temp.Rows[temp.Rows.Count - 1]["Sheesha"] = Drop_nsheesha.SelectedItem;
            temp.Rows[temp.Rows.Count - 1]["Quantity"] = TextBox_SimpleSheesha.Text;
            GridView2.DataSource = temp;
            GridView2.DataBind();

        }
("Quantity");
}
Posted
Updated 28-Nov-14 2:02am
v3
Comments
Tushar sangani 10-Dec-14 6:55am    
Why Use Update Panel?

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