如何根据来自代码隐藏或 DataGrid 中数据源的数据更改项目模板中的组合框

发布于 2024-12-04 15:50:39 字数 216 浏览 0 评论 0原文

我有一个数据网格,数据来自数据库,它在状态中有组合框,我希望它们根据各自的值,例如,有一列状态,它有一个组合框:打开和关闭,我希望它相对于数据库表列中的值进行更改,如果数据库表列中写入了“关闭”,则组合框应选择为“关闭”,如果它具有“打开”,则应选择为“打开”。请参阅所附图片。 在此处输入图像描述 提前感谢您的帮助。

I have a data grid, and data is coming from database, it has combo boxes in status and I want them to be according to their respective values, for example, there is a column of status, it has a combo box: Open and Close, I want it to be changed with respect to the value it has in database table-column, if it has Close written in database table column, combo box should be selected as Close, if it has Open then it should be selected as Open. Please see the image attached.
enter image description here
Thanks for Help in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青萝楚歌 2024-12-11 15:50:39

假设您使用的是模板列,您可以这样做:

<asp:TemplateColumn>
    <ItemTemplate>
        <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Eval("DropDownValueColumn") %>' />
    </ItemTemplate>
</asp:TemplateColumn>

如果您想在 ItemDataBound 事件中设置 SelectedValue,您可以这样做:

protected void DataGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    DropDownList ddl = e.Item.FindControl("DropDownList1") as DropDownList;
    if (ddl != null)
    {
        ddl.SelectedValue = DataBinder.Eval(e.Item.DataItem, "DropDownColumnValue").ToString();
    }
}

Assuming that you're using a template column, you can do this:

<asp:TemplateColumn>
    <ItemTemplate>
        <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Eval("DropDownValueColumn") %>' />
    </ItemTemplate>
</asp:TemplateColumn>

If you want to set the SelectedValue in the ItemDataBound event, you can do it like this:

protected void DataGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    DropDownList ddl = e.Item.FindControl("DropDownList1") as DropDownList;
    if (ddl != null)
    {
        ddl.SelectedValue = DataBinder.Eval(e.Item.DataItem, "DropDownColumnValue").ToString();
    }
}
把回忆走一遍 2024-12-11 15:50:39

首先将数据源保存在视图状态中。如果数据源是数据表则这样做。

protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{

        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            DropDownList ddlList = (DropDownList)e.Row.FindControl("Name_of_DPList");
            int i=e.Row.RowIndex;
            DataTable dtTable = (DataTable)ViewState["dtPurchaseOrder"];
            string str = dtTable.Rows[i]["Name_Of_column"].ToString();//Name of the column in data source that stores the status.
            ddlList.items.FindByText(str).Selected=true;
        }
    }

First of all save your data source in viewstate. If datasource is datatable then do like this.

protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{

        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            DropDownList ddlList = (DropDownList)e.Row.FindControl("Name_of_DPList");
            int i=e.Row.RowIndex;
            DataTable dtTable = (DataTable)ViewState["dtPurchaseOrder"];
            string str = dtTable.Rows[i]["Name_Of_column"].ToString();//Name of the column in data source that stores the status.
            ddlList.items.FindByText(str).Selected=true;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文