I have trouble with conversion of a gridview example to datagrid, please help
- From: intrader <intrader@xxxxxxx>
- Date: Mon, 15 May 2006 16:34:13 GMT
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
.
- Follow-Ups:
- Prev by Date: RegEx validator control not validating properly
- Next by Date: RE: Web site templates
- Previous by thread: RegEx validator control not validating properly
- Next by thread: Re: I have trouble with conversion of a gridview example to datagrid, please help
- Index(es):
Relevant Pages
|