Link to home
Start Free TrialLog in
Avatar of groovbox303
groovbox303

asked on

Drop down Template inside Gridview bind to different fieldnames

Hi Experts,
              I am having a hard time figuring this out. I have a gridview that Im binding to an object Datasource, and a drop down template inside it that I am binding to a seperate object Data source. My problem is that depending on a Field Type name picked from a drop down (ddlFields) outside of the Gridview the data will be pulled from different tables for the drop down template. So I cannot give it specific field names to bind to. How can I accomplish this. I use a button to populate both the object data source for the Grid and the drop down template. If I do the following it populates the dropdown templates the second time I push the BtnShowMappings button but the first time it just displays System.Data.datarowView as all items. I have commented the different object sources for both. Is there another way to give the drop downs the different datavalue field names and value fields?

Also once they are populated with the correct items I would like to go through each row and pre select the item with the value = e.row.cell(4).Text  
 
 Many thanks in advance for any help I can get!  

Protected Sub BtnShowMappings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnShowMappings.Click
        HdHospID.Value = HospSelector1.SelectedValue
        FormatNETGrid(GridShowMappings)
        Dim Field As String = ddlFields.SelectedValue
 
        'The Object Data Source for the Gridview
        ObjectDataSource1.DataBind()
 
        For Each oRow As GridViewRow In GridShowMappings.Rows
     Dim ddl As DropDownList = CType(oRow.FindControl("DropDownList1"), DropDownList)
            If (Field = "HospPatType") Then
                ddl.DataTextField = "StdPatTypeDesc"
                ddl.DataValueField = "StdPatType"
            ElseIf (Field = "HospDschgCode") Then
                ddl.DataTextField = "StdDschgDesc"
                ddl.DataValueField = "StdDschgCode"
            End If
 
        Next
        'The object Data source for the Drop down template 
        DDLODS.DataBind()
     
    End Sub

Open in new window

Avatar of crazyman
crazyman
Flag of United Kingdom of Great Britain and Northern Ireland image

Create 2 dropdowns and hide/show the appropriate one?
Avatar of groovbox303
groovbox303

ASKER

 Hi Crazyman, actually those two drop downs for only for an example, there is going to be a dropdown for every row thats why I mentioned I created a drop down template inside the gridview. The problem is using object datasource, you have to mention the datavalue field and datatextfield for the template in EditItemTemplate but these fields in this situation change, based on an option called Fields which is selected from a drop down outside of the grid. Different fields will get data from different tables so I cannot explicitly give those names before hand, I need to dynamically assign them on the button click event as I am populating the gridview somehow.
ASKER CERTIFIED SOLUTION
Avatar of crazyman
crazyman
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
k that helped me get it to work, I changed it to Ddl.databind for each row and it binds it just fine now on the button click event. But now I have another problem. I was mistaken I had to put the drop down in an item template and not an Edititemtemplate because doing that I could not find the drop downs by the names using FindControl("DropDownList1") for each row. Now when I hit Edit for any row, the dropdowns lose thier Items and just show " System.data.DatarowView" . I tried doing the following in the Row Editing and Row Created events but it does not work. Does anyone know what event should I put the below logic for it to work?
Protected Sub GridShowMappings_RowEditing(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs)
 
        For Each oRow As GridViewRow In GridShowMappings.Rows
     
        Dim ddl As DropDownList = CType(oRow.FindControl("DropDownList1"), DropDownList)
            Dim Field As String = ddlFields.SelectedValue
            GridShowMappings.EditIndex = e.NewEditIndex
 
            If (Field = "HospPatType") Then
                ddl.DataTextField = "StdPatTypeDesc"
                ddl.DataValueField = "StdPatType"
            ElseIf (Field = "HospDschgCode") Then
                ddl.DataTextField = "StdDschgDesc"
                ddl.DataValueField = "StdDschgCode"
            End If
            ddl.DataBind()
            ddl.SelectedValue = oRow.Cells(4).Text
        Next
    End Sub

Open in new window