DataGrid 属性解释:
AutoGenerateColumns="False" :是否为数据源的每一个字段自动创建对象。
AllowPaging="True" :是否允许分页。
PageSize="5" :分页的页数。
- <asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False" Width="600px"
- BorderWidth="0px" CellPadding="0" ShowHeader="true" AllowPaging="True" PageSize="5"
- Height="180px" GridLines="None" OnItemCommand="DataGrid1_ItemCommand">
- <Columns>
- <asp:TemplateColumn>
- <HeaderTemplate>
- <table width="400" border="1" bordercolor="#000000" cellspacing="0" cellpadding="0"
- style="border-collapse: collapse">
- <tr>
- <td width="100">
- 编号</td>
- <td width="100">
- 标题</td>
- <td width="100">
- 内容</td>
- <td width="100">
- 操作</td>
- </tr>
- </table>
- </HeaderTemplate>
- <ItemTemplate>
- <table width="400" border="1" bordercolor="#000000" cellspacing="0" cellpadding="0"
- style="border-collapse: collapse">
- <tr>
- <td width="100">
- <%# DataBinder.Eval(Container.DataItem,"Id") %>
- </td>
- <td width="100">
- <%# DataBinder.Eval(Container.DataItem,"Title") %>
- </td>
- <td width="100">
- <%# DataBinder.Eval(Container.DataItem,"LogContext") %>
- </td>
- <td width="100">
- <asp:Button ID="BtnDelete" runat="server" BackColor="#F3F3F3" Font-Size="14px" BorderStyle="None"
- Text="编号值" CommandName="index" ForeColor="#3399FF" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"Id")%>' />
- </td>
- </tr>
- </table>
- </ItemTemplate>
- </asp:TemplateColumn>
- </Columns>
- </asp:DataGrid>
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- this.DataGrid1.DataSource = DataGridSource();
- this.DataGrid1.DataBind();
- }
- }
- private List<Log> DataGridSource()
- {
- List<Log> logs = new List<Log>();
- for (int i = 1; i < 11; i++)
- {
- Log log = new Log();
- log.Id = i;
- log.Title = "标题" + i;
- log.LogContext = "内容" + i;
- logs.Add(log);
- }
- return logs;
- }
- public class Log
- {
- private int id;
- public int Id
- {
- get { return id; }
- set { id = value; }
- }
- private string title;
- public string Title
- {
- get { return title; }
- set { title = value; }
- }
- private string logContext;
- public string LogContext
- {
- get { return logContext; }
- set { logContext = value; }
- }
- }