简体   繁体   English

如何相对于来自DataGrid背后的代码或数据源的数据更改项目模板中的组合框

[英]How to change combobox in item template with respect to the data coming from code behind or data source in DataGrid

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. 请参阅所附图片。 在此处输入图片说明 Thanks for Help in advance. 预先感谢您的帮助。

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: 如果要在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();
    }
}

First of all save your data source in viewstate. 首先,将数据源保存在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;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从WPF DataGrid中的数据模板获取组合框 - Get the combobox from data template in WPF DataGrid 数据源更改时如何更改datagridview中添加的Combobox项目? - how to change added Combobox item in datagridview when Data Source changes? 如何从后面的代码绑定并在我的数据网格中添加数据? - How to bind and add data in my datagrid from code behind? 如何在SQL Server返回的DataGrid中显示数据(没有代码) - How to display data in DataGrid returned from SQL Server (no code behind) 如何将 Windows 社区模板数据网格的数据项行上的集合用作同一行上 ComboBox 列的项源? - How can I use a collection on the data item row of my Windows Community Template datagrid as the Itemsource for a ComboBox column on the same row? 如何从WPF后面的代码更改Data Grid Header的字体大小 - How to change the fontsize of Data Grid Header from code behind wpf 如何从WPF中的datagrid获取组合框中的数据? - How to get data from combobox from datagrid in WPF? 如何显示来自 c# 数据库的 combobox 中的特定数据? - How to display specific data in combobox coming from database in c#? 将DataGrid * Column绑定到后台代码中的数据 - Binding a DataGrid*Column to data in code-behind 如何在后面的代码中更改DataGrid列的标题? - How to change the header of a DataGrid column in code behind?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM