Difference between revisions of "GridView"
(New page: Das Sample, das in jedem Buch fehlt: <source lang="csharp"> </source>) |
|||
Line 1: | Line 1: | ||
− | Das Sample, das in jedem Buch fehlt | + | Das Sample, das in jedem Buch fehlt. |
<source lang="csharp"> | <source lang="csharp"> | ||
+ | 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; | ||
+ | } | ||
+ | } | ||
</source> | </source> |
Revision as of 18:28, 11 November 2009
Das Sample, das in jedem Buch fehlt.
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;
}
}