How to access Datagrid in a datagrid

R

Raghuram

Hi,
i have a DataGrid by Name : datagrid1 and in that i had created one templete
Column and Added a new datagrid : datagrid2.
Now i want to access that datagrid2 how is it possiable. and populate the
same datawhich is preset in the datagrid1.

Raghuram
 
G

garethdjames

use the following code (sorry its in C#)

in Page_Load

DataGrid1.ItemCreated += new
DataGridItemEventHandler(DataGrid1_ItemCreated);
//bind your first grid
DataGrid1.DataSource = data;
DataGrid1.DataBind();

create the following method

private void DataGrid1_ItemCreated(object sender, DataGridItemEventArgs
e)
{
if(e.Item.ItemType == ListItemType.AlternatingItem
|| e.Item.ItemType == ListItemType.Item)
{
//find the control by referencing the col number
//if the template col is in the first col then
//use index 0
DataGrid DataGrid2 =
(DataGrid)e.Item.Cells[0].FindControl("DataGrid2");
//use whatever data source you need
DataGrid2.DataSource = new string[3]{"ab", "cd", "ef"};
DataGrid2.DataBind();
}
}

------ this is the html code

<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns=False>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:DataGrid ID="DataGrid2" Runat="server"></asp:DataGrid>
<br>
rest of template
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top