vb.net 在 GridView 的 rowEditing 事件中填充下拉列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20496408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:05:27  来源:igfitidea点击:

Populating a dropdownlist in the rowEditing event of a GridView

asp.netvb.netgridview

提问by Jimsan

I have a GridView as below

我有一个 GridView 如下

<asp:GridView ID="gvChain" runat="server" Font-Size="Small" CellPadding="3" CellSpacing="1" GridLines="None"
                AutoGenerateColumns="False">
                <HeaderStyle BackColor="#CCCCCC" />
                <Columns>
                    <asp:BoundField DataField="Department" HeaderText="Department" />
                    <asp:TemplateField HeaderText="Manager Level 1">
                        <ItemTemplate>
                            <asp:Label ID="lblManager1" runat="server" Text=""></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="cbManager1" runat="server"></asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Manager Level 2">
                        <ItemTemplate>
                            <asp:Label ID="lblManager2" runat="server" Text=""></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="cbManager2" runat="server"></asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:CommandField ShowEditButton="True">
                    <ControlStyle ForeColor="#009EDD" />
                    </asp:CommandField>
                </Columns>
            </asp:GridView>

The BoundField 'Department' is being populated in the Page_Load as below which works fine and populates the GridView with a list of departments.

BoundField '部门' 被填充在 Page_Load 中,如下所示,它工作正常并用部门列表填充 GridView。

Dim dhandler As DepartmentHandler = New DepartmentHandler
    Dim depts As New List(Of Department)
    depts = dhandler.GetDepartmentList

    gvChain.DataSource = depts
    gvChain.DataBind()

I am then populating the ItemTemplates of the TemplateFields in the RowDAtaBound event as below. this is also working fine.

然后我在 RowDAtaBound 事件中填充 TemplateFields 的 ItemTemplates,如下所示。这也工作正常。

If e.Row.RowType = DataControlRowType.DataRow Then

        Dim lblManager1 As Label = DirectCast(e.Row.FindControl("lblManager1"), Label)
        Dim lblManager2 As Label = DirectCast(e.Row.FindControl("lblManager2"), Label)


        Dim eHandler As EmployeeHandler = New EmployeeHandler
        Dim deptCell As TableCell = e.Row.Cells(0)
        Dim dept As Department = New Department
        dept.Department = deptCell.Text
        Dim mgr1 As Manager = eHandler.getManager1(dept)
        Dim mgr2 As Manager = eHandler.getManager2(dept)

        lblManager1.Text = mgr1.Name
        lblManager2.Text = mgr2.Name
    End If

What I now want to achieve is when clicking on the 'Edit' field of a row on the GridView, populate cbManager1 and cbManager2 with a list of Managers and set the SelectedValue of each DDL to the same values as I am retrieving in the item template. I can get the data with the below code in the RowEditing event of the GridView:

我现在想要实现的是当单击 GridView 上一行的“编辑”字段时,使用管理器列表填充 cbManager1 和 cbManager2,并将每个 DDL 的 SelectedValue 设置为与我在项目模板中检索的值相同的值. 我可以在 GridView 的 RowEditing 事件中使用以下代码获取数据:

' Get the list of managers
    Dim mgrs As New List(Of Manager)
    mgrs = eHandler.GetManagerList

    ' Get the department name from the BoundField
    Dim deptCell As TableCell = gvChain.Rows(e.NewEditIndex).Cells(0)
    Dim dept As Department = New Department
    dept.Department = deptCell.Text

    ' Pass the department name to the getManager1/2 functions to return the correct manager for that department
    Dim mgr1 As Manager = eHandler.getManager1(dept)
    Dim mgr2 As Manager = eHandler.getManager2(dept)

I have set breakpoints to check the data is being returned from the functions and the data is there as expected but where I am stuck is getting a reference on the DDLs in the EditItemTemplate so I can databind them and set the SelectedValue. I have tried the below but this is giving a NullReference Exception:

我已经设置了断点来检查从函数返回的数据,并且数据按预期存在,但是我卡住的地方是在 EditItemTemplate 中获取对 DDL 的引用,以便我可以对它们进行数据绑定并设置 SelectedValue。我已经尝试了以下,但这给出了一个 NullReference 异常:

Dim cbManager1 As DropDownList = TryCast(gvChain.Rows(e.NewEditIndex).FindControl("cbManager1"), DropDownList)
    Dim cbManager2 As DropDownList = TryCast(gvChain.Rows(e.NewEditIndex).FindControl("cbManager2"), DropDownList)

Any ideas?

有任何想法吗?

回答by Jimsan

I got round this in the end. I added the following code to the RowEditing event

我最终解决了这个问题。我在 RowEditing 事件中添加了以下代码

gvChain.EditIndex = e.NewEditIndex
gvChain.DataBind()

I then did the following in the RowDataBound event

然后我在 RowDataBound 事件中做了以下事情

 If e.Row.RowState = DataControlRowState.Edit Then

            Dim cbManager1 As DropDownList = TryCast(e.Row.FindControl("cbManager1"), DropDownList)
            Dim cbManager2 As DropDownList = TryCast(e.Row.FindControl("cbManager2"), DropDownList)

            Dim eHandler As EmployeeHandler = New EmployeeHandler


            Dim mgrs As New List(Of Manager)
            mgrs = eHandler.GetManagerList
            cbManager1.DataSource = mgrs
            cbManager2.DataSource = mgrs
            cbManager1.DataValueField = "Name"
            cbManager1.DataTextField = "Name"
            cbManager2.DataValueField = "Name"
            cbManager2.DataValueField = "Name"
            cbManager1.DataBind()
            cbManager2.DataBind()

            ' Got rid of the bound field in the end and did it with a templatefield so needed to get the department from the label
            Dim lbl As Label = e.Row.Cells(0).FindControl("lblDepartment")
            Dim dept As Department = New Department
            dept.Department = lbl.Text


            Dim mgr1 As Manager = eHandler.getManager1(dept)
            Dim mgr2 As Manager = eHandler.getManager2(dept)


            Dim mgr1Name As String = mgr1.Name.ToUpper()
            Dim mgr2Name As String = mgr2.Name.ToUpper()


            cbManager1.SelectedValue = mgr1Name
            cbManager2.SelectedValue = mgr2Name

回答by conway

You could try something like this

你可以试试这样的

If e.Row.RowState = DataControlRowState.Edit Then

   Dim cbmanager As DropDownList = e.Row.FindControl("cbmanager1")

   If Not cbmanager Is Nothing Then cbmanager.Items.Add(New ListItem("Test1", "Test1"))


End If