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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DropDownList inside DataGrid

Status
Not open for further replies.

rrhandle

Programmer
Dec 26, 2001
193
US
I have a DropDownList assigned to a column in a DataGrid, but cannot find a way to bind my collection to it.

I used the example from
They do it by putting code on the HTML page and assigning the DataSource to the DropDown. I am trying to keep my code in the CodeBehind page.

How do I get a reference to the DropDown from the CodeBehind page?




Here is the code for the DropDownList:
Code:
<asp:TemplateColumn HeaderText="Panel Type">
<ItemTemplate>
  <%# DataBinder.Eval(Container.DataItem, "DisplayInfo") %>
</ItemTemplate>
<EditItemTemplate>
 <asp:DropDownList runat="server" id="ddPanelTypes" 
                DataValueField="ConfigPanelGID"
                DataTextField="DispalyName"
                DataSource="???" />
/>
</EditItemTemplate>
</asp:TemplateColumn>

The only thing worse than being alone, is wishing you were.
 
Set your databinding properties of the dropdown in the properties window. Fill you dataset(s) and then bind the grid. The dropdown will also be bound at the same time the grid is bound.

Jim
 
There is no properties window for the dropdown--it does not exist on the page. I didn't drag-and-drop a control on the page; just followed the instruction in the example.

The only thing worse than being alone, is wishing you were.
 
How can it NOT exist if it exists in the HTML you posted? You have to right click on the grid and choose Edit Template and choose the column with your drop down in it. You will then see the drop down and be able to set the properties.
 
I'm not sure I see the problem here. As jbenson001 points out, the dropdownlist does exist as you have created it within your datagrid.

Regardless of whether a "Sub" is created within the HTML page (inline) as per the example on the 4guys site, or whether you create it in a code-behind file, you can still apply the same code as per the example (as they don't reference the DDL directly as it will be bound when the datagrid is bond - again, as jbenson001 pointed out).


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I must not be explaining me problem very well. With the code I have posted here, the DropDownList does show up in the DataGrid, but there is nothing in it. Someplace I need to bind the DropDownList to a collection of values I have, and I am attempting to do it within the CodeBehind page. Does that clearify things?

 
I am using the following code on the HTML page.

Code:
<EditItemTemplate>
<asp:DropDownList runat="server" id="ddPanelTypes" 
  DataValueField="ConfigPanelGID" 
  DataTextField="DisplayInfo" 
  [COLOR=red]DataSource="<%# PanelTypesBind() %>" [/color]/>
</EditItemTemplate>

In the code behind, I am using:

Code:
Public Function PanelTypesBind() As colConfigPanels
  Dim cConfigPanels As colConfigPanels = CType(Session ("ConfigPanels"), colConfigPanels)
  Return cConfigPanels
End Function

The current problem is the error: 'A valid data source must implement either IListSource or IEnumerable.'
I can't define the type of the function to .ItemList.
Any suggestions?


 
use this code in your code behind
ddPanelTypes.datasource = <your data source>
 
That was the first thing I tried. Error: "ddPanelTypes not defined."

This seems to work:

Code:
Public Function PanelTypesBind() As [COLOR=red]IEnumerable[/color]
  Dim cConfigPanels As colConfigPanels = CType(Session("ConfigPanels"), colConfigPanels)
  Return cConfigPanels.ItemList
End Function

 
Must have done a really bad job of explaining the problem. Sorry about that, and thanks for your help!

 
Last week I peppered this site with questions about a DropDownList inside a DataGrid, but got no place. Never have I been so stuck for so long. This weekend I found the problem, and it is so weird I answered my own post hoping to prevent/help anyone else who might run across this. I almost want to say it is a bug in .NET, but I don't know enough to make that statement. I'll let you be the judge. Maybe I slept through that day of .NET training.

You can view the answer at this post:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top