John Kinane
John Kinane

Reputation: 466

How to bind DropDownList in Gridview with data NOT from gridview

Half the battle of getting an answer is knowing how to ask the question. I am not certain I am doing a good job of that but this is my best shot.

I'm trying to bind a ddl with data inside a gridview that is NOT coming from the gridview itself. This is within the EditItemTemplate. The purpose for doing so is to give the user, to start, a selected value and a series of other values from a lookup stored procedure.

I'll mention here that I have done this successfully before but using an ObjectDataSource. I am trying to avoid that this time and do it entirely from the code behind for now then move it to a data layer later.

Here is what I have so far...

<asp:GridView ID="usersGrid" runat="server"                 
        DataKeyNames="userID" 
        AutoGenerateColumns="false" Width="580" 
        OnRowUpdating="usersGrid_RowUpdating"
        OnRowEditing="usersGrid_RowEditing" 
        OnRowCancelingEdit="usersGrid_RowCancelingEdit"                                   OnRowDeleting="usersGrid_RowDeleting" 
        >

...

<EditItemTemplate>
                <div class="gridName">
                    <asp:TextBox ID="txtFirstName" Text='<%#Eval("firstName") %>' runat="server" Width="95" />
                </div>
                <div class="gridName">
                    <asp:TextBox ID="txtLastName" Text='<%#Eval("lastName") %>' runat="server" Width="95" />
                </div>
                <div class="gridEmail">
                    <asp:TextBox ID="txtEmail" Text='<%#Eval("email") %>' runat="server" Width="245" />
                </div>
                <div class="gridName">
                    <asp:DropDownList ID="ddl_GetLists" 
                    DataSourceID="GetListData()"
                    AppendDataBoundItems="true"
                    DataValueField="listID"
                    DataTextField="listName"
                    SelectedValue='<%#Bind("listID") %>'
                    runat="server"
                    >
                    </asp:DropDownList>
                </div>
            </EditItemTemplate>

....

Protected Sub usersGrid_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
    usersGrid.EditIndex = e.NewEditIndex
    BindData()

End Sub

....

Private Sub BindData()
    Dim conn As New SqlConnection(connectionString)
    Dim ad As New SqlDataAdapter("MAINT_DIST_GET_USERS", conn)
    Dim ds As New DataSet()
    ad.Fill(ds)
    GetListData()
    usersGrid.DataSource = ds
    usersGrid.DataBind()

End Sub

I'm including last two as well as other approaches I've tried and failed.

...

    Protected Sub usersGrid_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowState = DataControlRowState.Edit Then
        Dim ddl As DropDownList = DirectCast(e.Row.FindControl("ddl_GetLists"), DropDownList)

        Dim conn As New SqlConnection(connectionString)
        Dim ad As New SqlDataAdapter("MAINT_DIST_GET_LISTS", conn)
        Dim ds As New DataSet()
        ad.Fill(ds)

        ddl.DataSource = ds
        ddl.DataBind()
    End If
End Sub

Public Function BindDropdown() As DataSet
    Dim conn As New SqlConnection(connectionString)
    Dim ad As New SqlDataAdapter("MAINT_DIST_GET_LISTS", conn)
    Dim ds As New DataSet()
    ad.Fill(ds)

    ddl_GetLists.DataSource = ds
    ddl_GetLists.DataBind()
End Function

I'll also ask why, in the final function, why is the control, ddl_GetLists, not recognized as well? Inside the grid it disappears from the designer but outside of the grid it reappears.

Thank you all for your help.

Upvotes: 7

Views: 39878

Answers (4)

Dinh Nam
Dinh Nam

Reputation: 1

You fix this:

  1. In the code page, you define a GetListData() public function (i think you did).

  2. Use DataSource property (if you want to call a function) :

                <asp:DropDownList ID="ddl_GetLists" 
                                  DataSource='<%# GetListData() %>'
                                  AppendDataBoundItems="true"
                                  DataValueField="listID"
                                  DataTextField="listName"
                                  SelectedValue='<%#Bind("listID") %>'
                                  runat="server" >
                </asp:DropDownList>
    

Upvotes: 0

Sooraj
Sooraj

Reputation: 21

You can simply create a global list of type that you want and set it as null if you are using linq then just put that source in the DropDownListObject.DataSource

DropDownListObject.DataSource=ObjListSource;
DropDownListObject.DataBind;

Hope it will be helpful.

Upvotes: 1

James Johnson
James Johnson

Reputation: 46067

You have a couple of options. You can use a datasource control, or you can bind the dropdowns in code-behind in the RowDataBound event of the GridView.

I noticed a couple of issues in your code too. In your example you're assigning the DataSourceID incorrectly. The DataSourceID should point to the ID of a datasource control on the page:

<asp:DropDownList ID="ddl_GetLists"     
    DataSourceID="SqlDataSource1"    
    AppendDataBoundItems="true"    
    DataValueField="listID"    
    DataTextField="listName"    
    SelectedValue='<%#Bind("listID") %>'    
    runat="server">    
</asp:DropDownList>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"                  
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"                  
    SelectCommand="SELECT listID, listName FROM SomeTable">                                             
</asp:SqlDataSource> 

If you want to do the binding in code-behind, you can do this through the RowDataBound event:

Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow AndAlso (e.Row.RowState And DataControlRowState.Edit) = DataControlRowState.Edit Then
        Dim ddl As DropDownList = TryCast(e.Row.FindControl("ddl_GetLists"), DropDownList)
        If ddl IsNot Nothing Then
            ddl.DataSource = RetrieveDataSource()
            ddl.DataBind()
        End If
    End If
End Sub

Upvotes: 12

Etch
Etch

Reputation: 3054

I see a couple issues.

Protected Sub usersGrid_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
    usersGrid.EditIndex = e.NewEditIndex
    BindData()

End Sub

Why are you doing this? Why would you bind the grid again on an event on your grid thats already been filled?

Upvotes: 0

Related Questions