I have trouble with conversion of a gridview example to datagrid, please help



In this example there is an array of states with sub-arrays of cities. The
problem is when I try to bind a specific sub-array to a dropdownlist within
a template column of the datagrid I am unable to translate the gridview
code to something that works with datagrids.

I have not found a way to get the Row so that I can then perform
FindControl("ddlCities") and bind the dropdownlist.

I have found in the documentation that Item is a reference to a 'row' in the
DataGrid, but the rest of the code that needs translation to DataGrid
eludes me

The HTML is as follows:
<code>
<asp:datagrid id="dgStates" runat="server" AutoGenerateColumns="False"
OnItemCreated="dgStates_ItemCreated">
<Columns>
<asp:BoundColumn DataField="Name" HeaderText="State"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Cities">
<ItemTemplate>
<asp:DropDownList ID="ddlCities" AutoPostBack="True"
Runat="server"
OnSelectedIndexChanged="Cities_SelectedIndexChanged"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid><asp:label id="lblCity" runat="server"
Text="Label"></asp:label></form>
</code>

The codebehind:

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
ArrayList states=new ArrayList();

string[] cities=new string[
{"PortLand","Salem","Eugene"};
State state = new State("OR",cities);
states.Add(state);

cities=new string[]{"Seatle","Tacoma","Olympia"};
state = new State("WA",cities);
states.Add(state);

cities=new string[]{"Agoura","Los Angeles","San
Diego"};
state = new State("CA",cities);
states.Add(state);

this.dgStates.DataSource = states;
this.dgStates.DataBind();
}
}
protected void dgStates_ItemCreated(object sender,
DataGridItemEventArgs e){
if(!IsPostBack){
if(e.Item.RowType == DataControlRowType.DataRow)
{
DropDownList ddl =
(DropDownList)e.Item.FindControl("ddlCities");
ddl.DataSource = ((State)e.Item.DataItem).Cities;
ddl.DataBind();
}
}
}
end of snippet

The code that I have been unable to translate if in the dgStates_ItemCreated
method:none of e.Item.RowType or e.Item.DataItem, or e.Item.FindControl
exist and give me compile errors.

Thanks

.



Relevant Pages

  • Re: Error: No default member found for type MyClass
    ... Return CType, clsAgentPostalCode) ... Dim oAgentPostalCodeCollection As New clsAgentPostalCodeCollection ... > You try to make from your dataset a collection to bind that to a datagrid. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: ASP.NET Remoting problem
    ... > I have added a web reference to this service. ... > But now I cannot bind an array of this object to a DataGrid because ... > properties to be able to bind with data grid. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: ASP.NET Remoting problem
    ... > I have added a web reference to this service. ... > But now I cannot bind an array of this object to a DataGrid because ... > properties to be able to bind with data grid. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: DataGrid.Binding()
    ... strings to which to bind. ... Bindable controls can bind to any object that has ... You cannot bind a DataTable with a custom object. ... A Datagrid can be bound TO ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Error: No default member found for type MyClass
    ... Return CType, clsAgentPostalCode) ... Dim oAgentPostalCodeCollection As New clsAgentPostalCodeCollection ... > You try to make from your dataset a collection to bind that to a datagrid. ...
    (microsoft.public.dotnet.languages.csharp)