Difference between revisions of "GridView"

From no name for this wiki
Jump to: navigation, search
(Sample 2)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 +
== Sample1 ==
 
Ein Sample über das GridView Webcontrol, das in vielen Büchern fehlt.  
 
Ein Sample über das GridView Webcontrol, das in vielen Büchern fehlt.  
  
Line 124: Line 125:
 
         </Columns>         
 
         </Columns>         
 
          
 
          
 +
        </asp:GridView>
 +
    </div>
 +
    </form>
 +
</body>
 +
</html>
 +
</source>
 +
 +
== Sample 2 ==
 +
Paging mit ''ObjectDataSource''. Nur die angezeigten Daten werden materialisiert!:
 +
<source lang="csharp">
 +
using System;
 +
using System.Data;
 +
 +
namespace Business
 +
{
 +
    public class Logic
 +
    {
 +
        public DataTable SelectMethod(int maximumRows, int startRowIndex)
 +
        {
 +
            return this.CreateTable(maximumRows, startRowIndex);
 +
        }
 +
 +
        public int Count()
 +
        {
 +
            return 100;
 +
        }
 +
 +
        private DataTable CreateTable(int maximumRows, int startRowIndex)
 +
        {
 +
            DataTable table = new DataTable();
 +
 +
            DataColumn column;
 +
            DataRow row;
 +
 +
            //First Column
 +
            column = new DataColumn();
 +
            column.DataType = typeof(Int32);
 +
            column.ColumnName = "id";
 +
            column.Unique = true;
 +
            table.Columns.Add(column);
 +
 +
            //Second Column
 +
            column = new DataColumn();
 +
            column.DataType = typeof(bool);
 +
            column.ColumnName = "BooleanColumn";
 +
            table.Columns.Add(column);
 +
 +
            // Make the ID column the primary key column.
 +
            DataColumn[] PrimaryKeyColumns = new DataColumn[1];
 +
            PrimaryKeyColumns[0] = table.Columns["id"];
 +
            table.PrimaryKey = PrimaryKeyColumns;
 +
 +
            for (int i = 0; i < maximumRows; i++)
 +
            {
 +
                row = table.NewRow();
 +
                row["id"] = i + startRowIndex + 1;
 +
                row["BooleanColumn"] = i % 2 == 0 ? true : false;
 +
                table.Rows.Add(row);
 +
            }
 +
            return table;
 +
        }
 +
    }
 +
}
 +
</source>
 +
 +
ASPX:
 +
<source lang="asp">
 +
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Pagination._Default" %>
 +
 +
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 +
 +
<html xmlns="http://www.w3.org/1999/xhtml" >
 +
<head runat="server">
 +
    <title></title>
 +
</head>
 +
<body>
 +
    <form id="form1" runat="server">
 +
    <div>
 +
        <asp:ObjectDataSource ID="ObjectDataSource1"
 +
                              runat="server"
 +
                              SelectMethod="SelectMethod"
 +
                              TypeName="Business.Logic"
 +
                              EnablePaging="True"
 +
                              SelectCountMethod="Count"/>
 +
        <asp:GridView ID="GridView1"
 +
                      runat="server"
 +
                      DataSourceID="ObjectDataSource1"
 +
                      AllowPaging="true"
 +
                      PageIndex="0"
 +
                      PageSize="10">
 
         </asp:GridView>
 
         </asp:GridView>
 
     </div>
 
     </div>

Latest revision as of 11:51, 5 April 2010

Sample1

Ein Sample über das GridView Webcontrol, das in vielen Büchern fehlt.

  • Templating ohne die hässliche Eval funktion.
  • funktionierendes Paging
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.GridView1.DataSource = this.CreateTable();
        this.GridView1.DataBind();
    }

    public void RowDataBound(Object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView row  = (DataRowView) e.Row.DataItem;
            bool value = (bool) row["BooleanColumn"];
            Label myLabel = (Label) e.Row.FindControl("GridViewLabel");
            if (value)
            {
                myLabel.Text = "Ja";
            }
            else
            {
                myLabel.Text = "Nein";
            }
        }
          
    }

    protected void PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        this.GridView1.DataBind();
    }


    private DataTable CreateTable()
    {
        DataTable table = new DataTable();

        DataColumn column;
        DataRow row;

        //First Column
        column = new DataColumn();
        column.DataType = typeof(Int32);
        column.ColumnName = "id";
        column.Unique = true;
        table.Columns.Add(column);

        //Second Column
        column = new DataColumn();
        column.DataType = typeof(bool);
        column.ColumnName = "BooleanColumn";                 
        table.Columns.Add(column);

        // Make the ID column the primary key column.
        DataColumn[] PrimaryKeyColumns = new DataColumn[1];
        PrimaryKeyColumns[0] = table.Columns["id"];
        table.PrimaryKey = PrimaryKeyColumns;

        for (int i = 0; i <= 200; i++)
        {
            row = table.NewRow();
            row["id"] = i;
            row["BooleanColumn"] = i%2 == 0 ? true : false;
            table.Rows.Add(row);
        }
        return table;
    }
}

und die Page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" 
                      runat="server"  
                      AutoGenerateColumns="False"
                      onrowdatabound="RowDataBound" 
                      AllowPaging="true" 
                      PageSize="10"
                      PageIndex="1"
                      OnPageIndexChanging="PageIndexChanging">
        
        <Columns>
  
            <asp:BoundField ReadOnly="true" HeaderText="The ominous id" DataField="id"/>
   
            <asp:TemplateField HeaderText="Ansehen" >
                <ItemTemplate>
                    <asp:Label ID="GridViewLabel" runat="server" Text="Label"/>                   
                </ItemTemplate>
            </asp:TemplateField>   
   
        </Columns>         
        
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Sample 2

Paging mit ObjectDataSource. Nur die angezeigten Daten werden materialisiert!:

using System;
using System.Data;

namespace Business
{
    public class Logic
    {
        public DataTable SelectMethod(int maximumRows, int startRowIndex)
        {
            return this.CreateTable(maximumRows, startRowIndex);
        }

        public int Count()
        {
            return 100;
        }

        private DataTable CreateTable(int maximumRows, int startRowIndex)
        {
            DataTable table = new DataTable();

            DataColumn column;
            DataRow row;

            //First Column
            column = new DataColumn();
            column.DataType = typeof(Int32);
            column.ColumnName = "id";
            column.Unique = true;
            table.Columns.Add(column);

            //Second Column
            column = new DataColumn();
            column.DataType = typeof(bool);
            column.ColumnName = "BooleanColumn";
            table.Columns.Add(column);

            // Make the ID column the primary key column.
            DataColumn[] PrimaryKeyColumns = new DataColumn[1];
            PrimaryKeyColumns[0] = table.Columns["id"];
            table.PrimaryKey = PrimaryKeyColumns;

            for (int i = 0; i < maximumRows; i++)
            {
                row = table.NewRow();
                row["id"] = i + startRowIndex + 1;
                row["BooleanColumn"] = i % 2 == 0 ? true : false;
                table.Rows.Add(row);
            }
            return table;
        }
    }
}

ASPX:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Pagination._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ObjectDataSource ID="ObjectDataSource1" 
                              runat="server"
                              SelectMethod="SelectMethod"
                              TypeName="Business.Logic"
                              EnablePaging="True"
                              SelectCountMethod="Count"/>
        <asp:GridView ID="GridView1" 
                      runat="server"
                      DataSourceID="ObjectDataSource1"
                      AllowPaging="true"
                      PageIndex="0"
                      PageSize="10">
        </asp:GridView>
    </div>
    </form>
</body>
</html>