下拉列表中的GridView值(Dropdownlist in Gridview value)

2019-10-19 17:42发布

我动态添加值到处于GridView中柱之一DROPDOWNLIST。

问题是,在下拉列表中选择的值不会被保存或者在添加新行维持? 任何想法,我可能做错了。 这里是我的代码..

这里是我的GridView

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

                    }

                }
            }
        }

Answer 1:

你什么都没有 “错”正因为如此,你只是希望错误的事情。 如Gri​​dView的模板控件起作用这种方式; 如果你希望你的模板内的控制的设定值的坚持,你必须自己做。

我劝不存储大量的会话数据,但我会假设你是知道的,您的情况不打算扩展到数百万用户提供几行该数据表。每。

在你的情况,我将另一列添加到数据表来存储“产品”价值; 与每次回发它同步,并重新绑定确保您设置的表值的下拉列表值: -

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"];
        }
    }
}


Answer 2:

当你在gridview的新行,你有没有尝试重新加载此下拉列表与你的循环?



文章来源: Dropdownlist in Gridview value