【问题标题】:Dropdownlist in Gridview valueGridview 值中的下拉列表
【发布时间】:2014-04-03 01:45:48
【问题描述】:

我正在向作为 Gridview 中的列之一的 dropDownlist 动态添加值。

问题是添加新行时下拉列表中选择的值没有保存或维护?知道我可能做错了什么。这是我的代码..

这是我的网格视图

   <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" OnDataBound="GridView1_DataBound">
                    <AlternatingRowStyle BackColor="White" />
                    <Columns>
                        <asp:boundfield datafield="ID" headertext="categoryid" />
<asp:boundfield datafield="univirsity" headertext="category name" />

                        <asp:templatefield headertext="products">
<itemtemplate>
<asp:dropdownlist id="dropdownlist1" runat="server">
</asp:dropdownlist>
</itemtemplate>
</asp:templatefield>
                    </Columns>

                </asp:GridView>

这是在单击按钮时将数据动态绑定到 Gridview 的代码。

if (!Page.IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("ID", System.Type.GetType("System.Int32"));
                dt.Columns.Add("univirsity", System.Type.GetType("System.Int32"));
                dt.Columns.Add("major", System.Type.GetType("System.Int32"));
                Session["MyDataTable"] = dt;
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            DataTable t = (DataTable)Session["MyDataTable"];
            DataRow row1 = t.NewRow();

            row1["ID"] = t.Rows.Count + 1;
            row1["univirsity"] = 3;
           // row1["major"] = 31;
            t.Rows.Add(row1);

            Session["MyDataTable"] = t;
            GridView1.DataSource = t;
            GridView1.DataBind(); 
        }

 protected void GridView1_DataBound(object sender, EventArgs e)
        {
            //addControl();

            ArrayList list = new ArrayList();
            list.Add("DMG");
            list.Add("SVC");

            foreach (GridViewRow row in GridView1.Rows)
            {
                // int key = (int)GridView1.DataKeys[row.RowIndex].Value;
                if (row.RowType == DataControlRowType.DataRow)
                {
                    DropDownList dl = (DropDownList)row.FindControl("dropdownlist1");

                    string sel = dl.SelectedValue;
                    if (!String.IsNullOrEmpty(sel))
                    {
                        dl.DataSource = list;
                        dl.DataBind();
                        dl.SelectedValue = sel;
                    }
                    else
                    {
                        dl.DataSource = list;
                        dl.DataBind();

                    }

                }
            }
        }

【问题讨论】:

    标签: c# asp.net gridview drop-down-menu datatable


    【解决方案1】:

    你并没有任何“错误”的事情,你只是在期待错误的事情。像gridviews这样的模板化控件就是这样工作的;如果您希望模板中控件的选定值保持不变,则必须自己完成。

    我建议不要在 Session 中存储大量数据,但我假设您知道这一点,并且您的方案不打算扩展到该数据表中有几行的数百万用户......每个用户。

    在您的情况下,我会在该数据表中添加另一列来存储“产品”值;将其与每个回发同步,并在重新绑定时确保从表值中设置下拉列表值:-

    protected override void OnLoad(EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("univirsity", typeof(int));
            dt.Columns.Add("major", typeof(int));
            // our new column
            dt.Columns.Add("selectedProduct", typeof(string));
            Session["MyDataTable"] = dt;
        }
        else
        {
            // on each postback, loop through the grid and update
            // the datatable
    
            // get our table AsEnumerable, so we can use LINQ expressions on it
            var table = ((DataTable)Session["MyDataTable"]).AsEnumerable();
    
            foreach (GridViewRow gridRow in GridView1.Rows)
            {
                // get this row's ID from the grid
                int id = Convert.ToInt32(gridRow.Cells[0].Text);
    
                // and get the matching datarow from the table
                var tableRow = table.Where(x => (int)x["ID"] == id)
                                    .SingleOrDefault();
    
                if (tableRow != null)
                {
                    var ddl = (DropDownList)gridRow.FindControl("dropdownlist1");
                    tableRow["selectedProduct"] = ddl.SelectedValue;
                }
            }
        }
    
        base.OnLoad(e);
    }
    
    void button1_Click(object sender, EventArgs e)
    {
        DataTable t = (DataTable)Session["MyDataTable"];
        DataRow   row1 = t.NewRow();
    
        row1["ID"] = t.Rows.Count + 1;
        row1["univirsity"] = 3;
        // set default value for new column
        row1["selectedProduct"] = "DMG";
    
        t.Rows.Add(row1);
    
        Session["MyDataTable"] = t;
        GridView1.DataSource = t;
        GridView1.DataBind();
    }
    
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        ArrayList list = new ArrayList();
        list.Add("DMG");
        list.Add("SVC");
    
        // get our table again
        var table = ((DataTable)Session["MyDataTable"]).AsEnumerable();
    
        foreach (GridViewRow gridRow in GridView1.Rows)
        {
            if (gridRow.RowType == DataControlRowType.DataRow)
            {
                // get our id and tablerow again
                int id = Convert.ToInt32(gridRow.Cells[0].Text);
                var tableRow = table.Where(x => (int)x["ID"] == id)
                                    .SingleOrDefault();
    
                // bind the list
                DropDownList dl = (DropDownList)gridRow.FindControl("dropdownlist1");
                dl.DataSource = list;
                dl.DataBind();
    
                // set value from tablerow.selectedProduct
                dl.SelectedValue = (string)tableRow["selectedProduct"];
            }
        }
    }
    

    【讨论】:

    • 非常感谢 sh1rts。你的建议成功了。
    【解决方案2】:

    当您在 gridview 中有新行时,您是否尝试使用循环重新加载此下拉列表?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多