Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combo box in GridView VWD-2005 2

Status
Not open for further replies.

browneye

Programmer
Nov 21, 2000
82
US
Hi,
Any one know how to show combo box in GridView? Options for combo box are stored in one of field in same table
I am using Visual Web Developer 2005.

Thanks
 
browneye: one way to display a ddl in a datagrid is to use a template column, e.g.,
Code:
<asp:TemplateColumn HeaderText="Type">
  <EditItemTemplate>
   <asp:DropDownList id="ddType" runat="server">
     <asp:ListItem>Chemistry</asp:ListItem>
     <asp:ListItem>Bacteria</asp:ListItem>
     <asp:ListItem>Stream</asp:ListItem>
   </asp:DropDownList>
  </EditItemTemplate>
</asp:TemplateColumn>
..and for databinding from a database it may take on the following appearence:
Code:
<asp:TemplateColumn Headertext="Zip-City">
  <EditItemTemplate>
    <asp:DropDownList id="ddZip"/>
  </EditItemTemplate>
</asp:TemplateColumn> 
..and in the codebehind..you can use a Function for binding...
Public Function PopulateZip() As DataView
  'open database...
  Dim dsZip As New System.Data.DataSet
  daZip = New OleDbDataAdapter("SELECT Zip_Code, ZipCty FROM tblZipCodes", cnnZip)
  daZip.Fill(dsZip) 
  return dsZip.Tables(0).DefaultView
End Function
...and in the Grid's ItemDataBound Event...
Sub Grid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) 
 If e.Item.ItemType = ListItemType.EditItem Then
  Dim itemType As ListItemType = e.Item.ItemType
   If (itemType = ListItemType.EditItem) Then
    'set Zip dropdown...
    Dim ZipList As DropdownList = CType(e.Item.FindControl("ddZip"), DropDownList)               
    ZipList.DataTextField = "ZipCty"
    ZipList.DataValueField = "Zip_Code"
    ZipList.DataSource = PopulateZip()
    ZipList.DataBind() 
   End If
 End If
End Sub
..and finally add the ItemDataBound event to the <asp:DataGrid..> tag, q.v.,
OnItemDataBound="Grid_ItemDataBound"
..you can Try..Catch..Finally and error trapping, etc. This is one approach for populating a ddl within a Grid, there are other variations. The binding code will vary, e.g., if you're using SQL, etc.
 
Isadore,
I couldnt find event Sub Grid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
for GridView did you mean
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound ?
Thank you. Happy Easter!!
 
browneye: I'm still in Notepad but getting ready to open up the new Visual Studio and using SQL - so I'll have to catch up with the new GridView v. DataGrid code - I am sure that is the same; the code should be similar.
 
Hi Isadore,Jim
Thank you very much..I appriciate your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top