locked
How can i bind an object to a GridControl ? RRS feed

  • Question

  • User1253338400 posted

    I have an object that returns a list as follows:

    var items = new List<Details>();

    and Details is as follows:

    public class Details
    {
     public string Id { get; set; }
     public string Name { get; set; }
     public string Status { get; set; }
    }

    the field Name is returned ad a semicolon delimnited for example 

    Bob;Builder

    Jim;Sanders

    How can i bind the data returned to a gridcontrol with Columns UserId, FirstName , LastName and SubscriptionStatus.

    For the name would i need to split it up first ?

    thanks

    Tuesday, March 10, 2020 1:55 AM

Answers

  • User-1330468790 posted

    Hi, robby32,

     

    You can directly use the list to bind the data to the grid view control as the data controls accepts the object since it has the access to the object's properties.

    The topic is related to "model binding" as below link if you have interest of.

    https://devblogs.microsoft.com/aspnet/tutorial-series-on-model-binding-with-asp-net-web-forms/

     

    However, if you just want to display it in the grid view control. You can refer to below code:

    .aspx Page:

    <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                    <Columns>
                        <asp:BoundField HeaderText="Id" DataField="Id"  />
                        <asp:TemplateField HeaderText="First Name" >
                            <ItemTemplate>
                                <asp:Label ID="Label1" Text='<%# Eval("Name").ToString().Split(";".ToCharArray())[0] %>' runat="server"></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Last Name" >
                            <ItemTemplate>
                                <asp:Label ID="Label1" Text='<%# Eval("Name").ToString().Split(";".ToCharArray())[1] %>' runat="server"></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField HeaderText="Status" DataField="Status"  />
                    </Columns>
                </asp:GridView>
            </div>
        </form>

     

    Code behind: Simulation of the data

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindGridView();
                }
            }
    
            protected void BindGridView()
            {
                List<Details> list = new List<Details>() 
                { 
                    new Details { Id = "1", Name = "Bob;Builder", Status = "Approved" } ,
                    new Details { Id = "2", Name = "Jim;Sanders", Status = "Failed" }
                };
                GridView1.DataSource = list;
                GridView1.DataBind();
    
    
            }
    
            public class Details
            {
                public string Id { get; set; }
                public string Name { get; set; }
                public string Status { get; set; }
            }

    Demo:

     

    Hope this can help you.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 10, 2020 8:53 AM

All replies

  • User-37275327 posted

    Try this, add 

    using System.Data;

    var items = new List<Details>();
                
    DataTable dt = new DataTable();
    dt.Columns.Add("id");
    dt.Columns.Add("FirstName");
    dt.Columns.Add("LastName");
    dt.Columns.Add("Status");
    
    foreach (var item in items)
    {
        dt.Rows.Add(item.Id, item.Name.Split(';')[0], item.Name.Split(';')[1], item.Status);
    }
    
    GridView1.DataSource = dt;
    GridView1.DataBind();

    Tuesday, March 10, 2020 7:35 AM
  • User-1330468790 posted

    Hi, robby32,

     

    You can directly use the list to bind the data to the grid view control as the data controls accepts the object since it has the access to the object's properties.

    The topic is related to "model binding" as below link if you have interest of.

    https://devblogs.microsoft.com/aspnet/tutorial-series-on-model-binding-with-asp-net-web-forms/

     

    However, if you just want to display it in the grid view control. You can refer to below code:

    .aspx Page:

    <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                    <Columns>
                        <asp:BoundField HeaderText="Id" DataField="Id"  />
                        <asp:TemplateField HeaderText="First Name" >
                            <ItemTemplate>
                                <asp:Label ID="Label1" Text='<%# Eval("Name").ToString().Split(";".ToCharArray())[0] %>' runat="server"></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Last Name" >
                            <ItemTemplate>
                                <asp:Label ID="Label1" Text='<%# Eval("Name").ToString().Split(";".ToCharArray())[1] %>' runat="server"></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField HeaderText="Status" DataField="Status"  />
                    </Columns>
                </asp:GridView>
            </div>
        </form>

     

    Code behind: Simulation of the data

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindGridView();
                }
            }
    
            protected void BindGridView()
            {
                List<Details> list = new List<Details>() 
                { 
                    new Details { Id = "1", Name = "Bob;Builder", Status = "Approved" } ,
                    new Details { Id = "2", Name = "Jim;Sanders", Status = "Failed" }
                };
                GridView1.DataSource = list;
                GridView1.DataBind();
    
    
            }
    
            public class Details
            {
                public string Id { get; set; }
                public string Name { get; set; }
                public string Status { get; set; }
            }

    Demo:

     

    Hope this can help you.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 10, 2020 8:53 AM