I have a gridview with the following code:
<asp:TemplateField HeaderText="Column1">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Column1") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" Width="125px">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
I want to bind a sql statement to populate the dropdownlist...
select Column1 from Table1
would I do this through the code behind? any information is appreciated? Also based on the selection the user makes with this dropdownlist, I want to populate the next column (column2) with the corresponding data... Need assistance with that too...
I am unfamiliar with template fields, I can work with a gridview binding in code behind and through the html but templatefields is like another language... I appreciate the help!!
There are a few parts to the answer to your question and I'm making a few assumptions (you're using Visual Studio as your IDE, you can use VB as your code-behind language):
You can do this either in code-behind or through the Visual Studio GUI. While you may use a boundfield for the dropdownlist, the templatefield ultimately gives you more flexibility. I'd read up on the templatefield (here), as it will be your friend using the Gridview for anything beyond basic data display. Using the GUI, selecting the dropdownlist should give you a small arrow to the upper-right that will allow you to create a data connection and datasource to bind to your dropdownlist.
This is a little more complex, as you will need to trigger PostBack (using AutoPostBack) on the Dropdownlist, handle the Dropdownlist's SelectedIndexChanged event, find the control to be updated in your second column, and update that control based on the selectedindex/item/value from your Dropdownlist. There are several ways to do this but here's the quickest I've found (using asp.net winforms). I'm using the SQL Adventureworks database, populating a dropdown in a templatefield in column 1 with the employeeID and using the selected employeeID to populate a label in column 2 with that employeeID's managerID.
And the SelectedIndexChanged event from the code-behind:
And since working with the Gridview is sometimes an exercise in self-flagellation, I'd suggest having some good walkthrough tutorials at hand:
-J