asp.net gridview行添加 [英] asp.net gridview row adding

查看:89
本文介绍了asp.net gridview行添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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();
    }









<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>





当我在dropdwn中选择另一个值时,我只能添加一行.我正在丢失先前的值,如何在不丢失先前的值的情况下添加多行





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

推荐答案

动态添加值,因为回发到服务器的表单中的数据表仅包含列表框的选定值.

添加后,尝试将集合保存在会话或数据库中的某些位置.
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.


这篇关于asp.net gridview行添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆