[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 442
  • Last Modified:

Populate Dropdownlist in Gridview using function

I have a function that I use to populate dropdownlist on web forms with values from a SQL database.   I have a dropdown list inside of a gridview that is in a template column that i want to populate using that same function.  I think i have done this before but can figure it out.  Any help would be appreciated
0
Mike_Stevens
Asked:
Mike_Stevens
  • 2
1 Solution
 
CodeCruiserCommented:
Use the RowDataBound event

http://forums.asp.net/t/1212101.aspx
0
 
JosephEricDavisCommented:
You'll have to do this inside of the gridview.OnRowDataBound event, like this...

 
protected void gvYourGridView_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
	//Check to make sure the databound row is one of the data rows and not the header
	if (e.Row.RowType == DataControlRowType.DataRow)
	{
		DropDownList ddl = e.Row.FindControl("IdOfYourDropDownListControlInTheTemplateColumn") as DropDownList;
		//Run your data function on the ddl
	}
}

Open in new window

0
 
RickCommented:
Here's a basic example:
Sub PopulateGridview()

dim drp as dropdownlist = gridview1.rows(1).Cells(1).findcontrol("dropdownlist1")

Dim cnn As New SqlConnection(ConnString)
Dim reader As SqlDataReader

Dim cmd As SqlCommand = New SqlCommand("select column from table", cnn)


cnn.Open()
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

drp.items.add("")


While reader.Read 
 drp.Items.Add(reader.item("column")
End While

reader.close()
cmd.dispose()
cnn.dispose()

End Sub

Open in new window

0
 
RickCommented:
I forgot... you need to have that inside a for each loop:

   for each gridviewrow In gridview1.rows


Or use the gridview's RowDataBound event, like like CodeCruiser and JosephEricDaves mentioned.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now