ASP.NET DataGrid in Repeater

I have a table with two columns:

CommunityID
PersonID

And the “People” table, which has (among other things):

FirstName
LastName

I would like to display a different DataGrid for each community, each datagrid has only people who are part of this community. I would like to do this without using 4 separate SqlDataSources.

Repeater looks like a good way, with a DataGrid inside an ItemTemplate, but I can't get heads or tails to make it work with different values ​​for each repetition.

If anyone has suggestions on better ways to do this, I would be very grateful, as this is one of my first raids on the world for ASP.NET

Thanks,

Mike

+3
source share
1

DataGrid, , GridView ListView ( DataGrid , ). , , .

, , , :

<asp:Repeater runat="server" ID="myRepeater" 
              onitemdatabound="Repeater_ItemDataBound">
<ItemTemplate>
    <asp:DataGrid runat="server" ID="myDataGrid">
    </asp:DataGrid>
</ItemTemplate>
</asp:Repeater>

:

protected void Page_Load(object sender, EventArgs e)
{
    myRepeater.DataSource = new Object[0];
    myRepeater.DataBind();
}

protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    DataGrid dg = (DataGrid)e.Item.FindControl("myDataGrid");
    object o = e.Item.DataItem;// Cast the DataItem as whatever 
                               // your Repeater DataSource is

    // ...
    // Do whatever you need to get the 
    // data source for your DataGrid here
    // ...

    dg.DataSource = DataGridSourceObjectHere;
    dg.DataBind();
}

- Repeater ItemDataBound, , , . DataGrid. RepeaterItemEventArgs , .

+12

Source: https://habr.com/ru/post/1720747/


All Articles