grid view combobox

2019-03-02 17:32发布

how to add items to a data grid view combo box

2条回答
祖国的老花朵
2楼-- · 2019-03-02 18:12

First add ad dropdownlist to your gridview with a template field like this Make sure you add an OnRowCreated Event to your gridview

<asp:GridView ID="GridView1" runat="server" OnRowCreated="GridView1_RowCreated">
<Columns>
                        <asp:TemplateField HeaderText="Prerequisite Course">
                            <ItemStyle HorizontalAlign="Center" />
                            <ItemTemplate>
                                <asp:DropDownList ID="ddlPrerequisiteCourseCode" runat="server">
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
</Columns>
  </asp:GridView>

Next in code behind Add a GridView1_RowCreated Event to your GridView

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {

                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    // Bind drop down to PrerequisiteCourseCodes
                    DropDownList ddl = (DropDownList)e.Row.FindControl("ddlPrerequisiteCourseCode");
                    ddl.DataSource = PrerequisiteCourseCodeList;
                    ddl.DataBind();
                }

        }
查看更多
Viruses.
3楼-- · 2019-03-02 18:25

You have a very good example here. Basically, the combobox is created and populated independently from the data binding.

This is a very generic question. If you're having more specific problems please let us know.

查看更多
登录 后发表回答