getting data from gridview to dropdown

learning 1 Reputation point
2021-06-10T17:53:52.693+00:00

I am trying to get the data from gridview to the form. I am able get all the values except dropdownlist.
when i debug it is getting the value right. but doesn't display in the form?

how to check back this forum to see my questions and answers.

Thanks

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,494 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Albert Kallal 5,251 Reputation points
    2021-06-12T01:05:54.563+00:00

    Ok, I’m going to take a whack at this.

    It would help a lot if this combo box was a single column combo, or a “often” used two column combo (stores the “ID”, but displays some description.

    I’m going with the 2nd example

    So, assume we have a list of customers and they have one column that is the Hotel they are booked to.

    So, our data table is quite simple. It looks like this:

    104928-image.png

    Ok, now our gridview. (tip: I build the gridview using the wizards – and THEN delete the data source setting in the combo, and remove the data source from the markup. That way I was able to cook up this grid view without having to type much – it took me LESS time to build the grid then to write this far into this post!!!

    So we have 3 regular data rows (First,Last,City)

    And then we dropped in a dropdown list. (in a templatefield).

    And then we dropped in a button (in a template field).

    So, we have this markup now:

    104945-image.png

    Ok, NOTE VERY close in above. That button uses a special “grid view” option – this MUST be “select”. This will cause the gridview “row command” to fire, and ALSO the gridview “selected index to fire”.

    We will NOT use the row command event (we could, but the gridview “index change” will not have occurred, and our code is thus “easy” by using the index changed event.

    So, first up, the code to load this grid up:

       Dim MyTable As New DataTable  
        Dim tblCity As New DataTable  
      
      
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
      
            If Not IsPostBack Then  
                LoadGrid()  
                ViewState("MyTable") = MyTable  
                ViewState("tblCity") = tblCity  
            Else  
                MyTable = ViewState("MyTable")  
                tblCity = ViewState("tblCity")  
            End If  
      
        End Sub  
      
        Sub LoadGrid()  
      
            Using cmdSQL As New SqlCommand("SELECT * from People", New SqlConnection(My.Settings.TEST4))  
      
                cmdSQL.Connection.Open()  
                MyTable.Load(cmdSQL.ExecuteReader)  
      
                cmdSQL.CommandText = "SELECT ID, HotelName from tblHotels ORDER BY HotelName"  
                tblCity.Load(cmdSQL.ExecuteReader)  
      
                GridView1.DataSource = MyTable  
                GridView1.DataBind()  
      
            End Using  
      
        End Sub  
      
      
        Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound  
      
            If e.Row.RowType.ToString = "DataRow" Then  
      
                Dim MyRow As DataRowView = e.Row.DataItem  
                Dim HotelID As Integer = MyRow("Hotel_ID")  
      
                Dim myDrop As DropDownList = e.Row.FindControl("cboHotel")  
                myDrop.DataSource = tblCity  
                myDrop.DataBind()  
                myDrop.Items.FindByValue(HotelID).Selected = True  
      
            End If  
      
        End Sub  
    

    So not much code. The resulting grid is now this:

    104938-image.png

    Ok, so far that was easy.

    Now, we need to get the "row" information. And for fun, lets add a repeater view to this page for display of the one row. I don't know if you wanted to jump to a new page, or just display that row choice on this page. Lets for now go with the current page. So lets create a cute little box with the column data, and a button to go back to the grid.

    So, now, our mark for this box looks like this:

    104954-image.png

    And the results look like this:

    104973-image.png

    So, now we only need the code to "transfer" the grid row to that cute little edit box.

    The code to do this is thus:

        Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged  
      
            Dim strFilter As String  
            strFilter = "ID = " & GridView1.DataKeys(GridView1.SelectedIndex).Item(0)  
      
            Dim MyViewRow As New DataView(MyTable, strFilter, "", DataViewRowState.CurrentRows)  
      
            Repeater1.DataSource = MyViewRow  
            Repeater1.DataBind()  
      
            GridView1.Style("Display") = "none"  
            MyViewer.Style("display") = "normal"  
      
        End Sub  
      
      
      
        Protected Sub Repeater1_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles Repeater1.ItemDataBound  
            Debug.Print(e.Item.ItemType.ToString)  
      
            Select Case e.Item.ItemType.ToString  
      
                Case "Item", "AlternatingItem"  
      
                    Dim MyRow As DataRowView = e.Item.DataItem  
                    Dim HotelID As Integer = MyRow("Hotel_ID")  
      
                    Dim myDrop As DropDownList = e.Item.FindControl("cboHotelR")  
      
                    myDrop.DataSource = tblCity  
                    myDrop.DataBind()  
                    myDrop.Items.FindByValue(HotelID).Selected = True  
      
            End Select  
      
        End Sub  
        Protected Sub cmdViewDone_Click(sender As Object, e As EventArgs) Handles cmdViewDone.Click  
      
            GridView1.Style("display") = "normal"  
            MyViewer.Style("display") = "none"  
      
        End Sub  
      
      
    

    So the view button triggers the selected item index. We then take that data table, create a data 'view" on that table of one row, and shove that into the Repeater.

    All in all it is not much code. You can of course loop the grid view rows and pull out each row, and pull out the combo box (you have to use find control).

    On the other hand, just getting the whole row of data, and binding it to the repeater eleminates all that code and work.

    And we could add a "save" data button to the details.

    Note that the markup once working should have a display "none" to hide the repeater. And you can see how our view button loads the repeater, hides the grid. And the back button simple hides the repeater and re-shows the grid.

    Edit:
    If you want a c# version - I'm happy to post the above code as c#

    Regards,
    Albert D. Kallal (Access MVP 2003-2017)
    Edmonton, Alberta Canada

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.