Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
sir, i have a gridview with the property of autogenerate column true. i want to bind each column with dropdownlist.

My code is like :-

VB
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Gridview1.RowDataBound

 

     Dim dl As New DropDownList()
     cmd1.CommandText = "Select * from ClassRoom order by ClassRoom"
     Dim dr2 = cmd1.ExecuteReader
     dl.DataSource = dr2
     dl.AppendDataBoundItems = True
     dl.DataTextField = "ClassRoom"
     dl.DataValueField = "ClassRoom"
     dl.DataBind()
     dl.SelectedItem.Text = ""
     dr2.Close()


        
             For Each row As GridViewRow In Gridview1.Rows
                 row.Cells(2).Controls.Add(dl)
                 row.Cells(3).Controls.Add(dl)
             Next
        


 End Sub



this code add dropdownlist only cell(3) not in cell(2) how can i add dropdownlist in both cell.
Posted
Updated 12-Dec-14 20:13pm
v7

1 solution

Shortly: you need to create new DropDownList for each column (cell). ;)

VB
For Each row As GridViewRow In Gridview1.Rows
    For i = 1 to 2
        Dim dl As New DropDownList()
        cmd1.CommandText = "Select * from ClassRoom order by ClassRoom"
        Dim dr2 = cmd1.ExecuteReader
        dl.DataSource = dr2
        dl.AppendDataBoundItems = True
        dl.DataTextField = "ClassRoom"
        dl.DataValueField = "ClassRoom"
        dl.DataBind()
        dl.SelectedItem.Text = ""
        dr2.Close()
        row.Cells(i).Controls.Add(dl)
    Next i
Next


See similar question: Re: GridView DropDownList programmatically add items on RowDataBound[^]

I really do recommend to create DropDownList and set its properties on design time: Walkthrough: Displaying a Drop-Down List While Editing in the GridView Web Server Control[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900