开发者

ASP.NET DataGrid - Display nested array data

开发者 https://www.devze.com 2023-04-06 05:52 出处:网络
I have a class like so: public class ApplicationInformation { public string ApplicationName { get; set; }

I have a class like so:

public class ApplicationInformation
{
    public string ApplicationName { get; set; }
    public List<UserInformation> Users { get; set; }
}

I want to display a List<App开发者_如何学GolicationInformation> in a DataGrid. So I tried this:

.aspx:
<asp:DataGrid ID="tbl" runat="server">
</asp:DataGrid>

.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
    tbl.DataSource = Global.Data;
    tbl.DataBind();
}

But that table only has a column titled ApplicationName. I want to have a column for each property in ApplicationInformation and UserInformation, and a separate row for every UserInformation in the list in the List<ApplicationInformation>.

I hope that made sense...

I looked into templates a bit, but couldn't figure out how to do this. I suppose I could always just loop through everything and create a separate array with all the data I need, but I'd have to make another class, and it just doesn't seem like that's the best way.

EDIT

For example, if I have a List<ApplicationInformation> like so:

{
    { ApplicationInformation
        ApplicationName = "App 1"
        { UserInformation
            UserName = "John Smith"
            ApplicationHost = "JOHN-PC"
        }
        { UserInformation
            UserName = "Mindy from the network"
            ApplicationHost = "MINDY-PC"
        }
    }
    { ApplicationInformation
        ApplicationName = "App 2"
        { UserInformation
            UserName = "John Smith"
            ApplicationHost = "JOHN-PC"
        }
        { UserInformation
            UserName = "Bob Jones"
            ApplicationHost = "BOB-PC"
        }
    }
}

Then I want the table to appear like this:

  Application Name  |  User Name  |  Application Host
-------------------------------------------------------
  App 1             | John Smith  |  JOHN-PC
  App 1             | Mindy fro...|  MINDY-PC
  App 2             | John Smmith |  JOHN-PC
  App 2             | Bob Jones   |  BOB-PC


You can either handle ItemDataBound event or use linq to create an anonymous object and bind it to DataGrid


You need to handle RowDataBound as Junaid suggested but you need to have a template column which in itself could be another Datagrid or a List of some sort to Bind List<UserInformation> to it.

For example (Untested code):

void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.DataRow)
    {

      //find nested grid
      GridView nestedGrid = e.FindControl("NestedGridID") as GridView;
      nestedGrid.DataSource = DataBinder.Eval(e.DataItem,"Users");
      nestedGrid.DataBind();

    }
 }


You may construct anonymous or typed list via Linq:

if (!IsPostBack)
        {
            List<ApplicationInformation> app = new List<ApplicationInformation>()
            {
                    new ApplicationInformation()
                    {
                         ApplicationName="App 1",
                         Users=new List<UserInformation>() 
                         {
                             new UserInformation()
                             {
                                  UserName = "John Smith",
                                  ApplicationHost = "JOHN-PC"
                             },
                           new UserInformation()
                             {
                                 UserName = "Mindy from the network",
                                 ApplicationHost = "MINDY-PC"
                             }
                         }
                    },
                    new ApplicationInformation()
                    {
                         ApplicationName="App 2",
                         Users=new List<UserInformation>() 
                         {
                             new UserInformation()
                             {
                                  UserName = "John Smith",
                                  ApplicationHost = "JOHN-PC"
                             },
                           new UserInformation()
                             {
                                 UserName = "Bob",
                                 ApplicationHost = "BOB-PC"
                             }
                         }
                    }
            };

            var result = from ele in app
                          from user in ele.Users 
                         select new
                         {
                              ApplicationName=ele.ApplicationName,
                              UserName=user.UserName ,
                              ApplicationHost=user.ApplicationHost 
                         };

            GridView1.DataSource = result.ToList();
            GridView1.DataBind();
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消