Click here to Skip to main content
15,907,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
DataTable dt;
    string str = ConfigurationManager.ConnectionStrings["conn"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(str);
        conn.Open();
        dt = new DataTable();
        DataColumn dc = new DataColumn();
        dc = new DataColumn("Medicine");
        dt.Columns.Add(dc);
        DataRow dr = dt.NewRow();
        dr["Medicine"] = this.medicine.SelectedValue;
        dt.Rows.Add(dr);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }




XML
<asp:DropDownList ID="medicine" runat="server" AutoPostBack="True"
            Height="22px" Width="262px">
            <asp:ListItem>agfdgd</asp:ListItem>
            <asp:ListItem>bgfdgd</asp:ListItem>
            <asp:ListItem>cgfdgd</asp:ListItem>
            <asp:ListItem>dgfdgd</asp:ListItem>
            <asp:ListItem>egfdgd</asp:ListItem>
            <asp:ListItem>fgfdgd</asp:ListItem>
            <asp:ListItem>ggfdgd</asp:ListItem>
        </asp:DropDownList>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            Height="59px" Width="404px">
            <Columns>
                <asp:BoundField DataField="Medicine" HeaderText="name" />
            </Columns>
        </asp:GridView>




i am able to add only one row when i select another value in the dropdwn i am loosing previous value how to add multiple rows without loosing previous values
Posted
Updated 17-Aug-11 21:29pm
v2

1 solution

As you are dynamically adding values, as the form posted back to server your data table contains only selected value of list box.

After adding try to save the collection some where either in session or database.
 
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