Data binding for the DataGrid control using a custom data source

Source: Internet
Author: User
Tags eval
Datagrid|datagrid Control | data | Data from defined collection classes
<summary>
Summary description of the Collection.
</summary>
public class Collection:System.Collections.CollectionBase
{
Public Collection ()
{
for (int i = 0;i < 10;i++)
{
Base. Innerlist.add (New Element (i,string). Format ("a[{0}]", i));
}
}
}

Collection Element class
public class Element
{
private string name;
public string ValueName
{
Get{return name;}
}
private int valu;
public int Value
{
Get{return Valu;}
}
Public Element (int val,string nam)
{
name = Nam;
Valu = val;
}
}

ASPX's post code
<summary>
Summary description of the WebForm1.
</summary>
public class WebForm1:System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;

private void Page_Load (object sender, System.EventArgs e)
{
DataGrid1.DataSource = new Collection ();
Datagrid1.databind ();
}

Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Load + = new System.EventHandler (this. Page_Load);

}
#endregion
}

HTML code for ASPX pages
<body ms_positioning= "FlowLayout" >
<form id= "Form1" method= "POST" runat= "Server" >
<asp:datagrid id= "DATAGRID1" runat= "Server" autogeneratecolumns= "False" width= "224px" >
<Columns>
<asp:templatecolumn headertext= "Name" >
<ItemTemplate>
<asp:label runat= "Server" text= ' <%# DataBinder.Eval (Container, "Dataitem.valuename")%> ' >
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:textbox runat= "Server" text= ' <%# DataBinder.Eval (Container, "DataItem")%> ' >
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:templatecolumn headertext= "Digital" >
<ItemTemplate>
<asp:label runat= "Server" text= ' <%# DataBinder.Eval (Container, "Dataitem.value")%> ' >
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:textbox runat= "Server" text= ' <%# DataBinder.Eval (Container, "DataItem")%> ' >
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</HTML>






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.