Click here to Skip to main content
15,896,606 members
Articles / Web Development / ASP.NET

TableDataSource - Binding DataTable to Rich Data Controls

Rate me:
Please Sign up or sign in to vote.
5.00/5 (10 votes)
20 Jun 2007CPOL7 min read 76.6K   1.3K   30  
Binding DataTable to Rich Data Controls
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class TestTableDataSource : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		lblError.Text = "";

		if (!IsPostBack)
		{
			DataTable tabulka = new DataTable("MyTable");

			tabulka.Columns.Add("Id", typeof(int));
			tabulka.Columns.Add("Title", typeof(string));
			tabulka.Columns.Add("FirstName", typeof(string));
			tabulka.Columns.Add("LastName", typeof(string));
			tabulka.Columns.Add("Born", typeof(DateTime));
			tabulka.Columns.Add("Gender", typeof(Sample.Gender));
			tabulka.Columns.Add("Valid", typeof(bool));

			DataRow row = tabulka.NewRow();
			row["Id"] = 1;
			row["Title"] = "RNDr.";
			row["FirstName"] = "Jiri";
			row["LastName"] = "Soler";
			row["Born"] = DateTime.Parse("20.4.1947");
			row["Gender"] = Sample.Gender.Men;
			row["Valid"] = true;
			tabulka.Rows.Add(row);

			row = tabulka.NewRow();
			row["Id"] = 2;
			row["Title"] = "";
			row["FirstName"] = "Pavel";
			row["LastName"] = "Vodrazka";
			row["Born"] = DateTime.Parse("11.2.1989");
			row["Gender"] = Sample.Gender.Men;
			row["Valid"] = true;
			tabulka.Rows.Add(row);

			row = tabulka.NewRow();
			row["Id"] = 3;
			row["Title"] = "";
			row["FirstName"] = "Petr";
			row["LastName"] = "Jaros";
			row["Born"] = DateTime.Parse("3.6.1990");
			row["Gender"] = Sample.Gender.Men;
			row["Valid"] = true;
			tabulka.Rows.Add(row);

			row = tabulka.NewRow();
			row["Id"] = 4;
			row["Title"] = "";
			row["FirstName"] = "Jan";
			row["LastName"] = "Kulhavy";
			row["Born"] = DateTime.Parse("10.12.1989");
			row["Gender"] = Sample.Gender.Men;
			row["Valid"] = true;
			tabulka.Rows.Add(row);

			row = tabulka.NewRow();
			row["Id"] = 5;
			row["Title"] = "";
			row["FirstName"] = "Jana";
			row["LastName"] = "Kulhava";
			row["Born"] = DateTime.Parse("3.1.2000");
			row["Gender"] = Sample.Gender.Women;
			row["Valid"] = true;
			tabulka.Rows.Add(row);

			row = tabulka.NewRow();
			row["Id"] = 6;
			row["Title"] = "ing.";
			row["FirstName"] = "Tomas";
			row["LastName"] = "Kucera";
			row["Born"] = DateTime.Parse("3.8.1899");
			row["Gender"] = Sample.Gender.Men;
			row["Valid"] = true;
			tabulka.Rows.Add(row);

			row = tabulka.NewRow();
			row["Id"] = 7;
			row["Title"] = "MUDr.";
			row["FirstName"] = "Vaclav";
			row["LastName"] = "Moudry";
			row["Born"] = DateTime.Parse("13.2.2006");
			row["Gender"] = Sample.Gender.Men;
			row["Valid"] = true;
			tabulka.Rows.Add(row);

			row = tabulka.NewRow();
			row["Id"] = 8;
			row["Title"] = "maj.";
			row["FirstName"] = "Tereza";
			row["LastName"] = "Statecna";
			row["Born"] = DateTime.Parse("13.2.2006");
			row["Gender"] = Sample.Gender.Women;
			row["Valid"] = true;
			tabulka.Rows.Add(row);

			tabulka.PrimaryKey = new DataColumn[] {tabulka.Columns["Id"]};

			tabulka.AcceptChanges();
			tableDataSource.Table = tabulka;

			ViewState["tabulka"] = tabulka;
		} else {
			tableDataSource.Table = (DataTable)ViewState["tabulka"];
		}

	}
	protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
	{
		if (GridView1.EditIndex >= 0)
		{
			lblError.Text = "Cannot change page while editing";
			e.Cancel = true;
		}
	}
	protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
	{
		if (GridView1.EditIndex >= 0)
		{
			lblError.Text = "Cannot change edit row";
			e.NewEditIndex = GridView1.EditIndex;
		}
	}
	protected void insertButton_Click(object sender, EventArgs e)
	{
		DataTable table = tableDataSource.Table;
		DataRow row = table.NewRow();
		row["Gender"] = 0;
		table.Rows.InsertAt(row, table.Rows.Count);
		if (GridView1.AllowPaging)
		{
			GridView1.PageIndex = table.Rows.Count / GridView1.PageSize;
		}
		GridView1.EditIndex = (table.Rows.Count - 1) % GridView1.PageSize;
	}
	protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
	{
		DataRow row = tableDataSource.Table.Rows[GridView1.Rows[e.RowIndex].DataItemIndex];
		if (row.RowState == DataRowState.Added)
		{
			row.Delete();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Publisher
Czech Republic Czech Republic
I'm 63 years old, originally physicist, later programmer by profession, now pensioner.

Jirí Šoler

Comments and Discussions