show-notice
hide-notice

Monday, 19 August 2013

DataTable event in asp.net


Introduction:

Here I will explain datatable event in asp.net  and c#

Description:

the DataTable exposes a set of that can be captured and handled. It can be very useful to handle the DataTable event, for case in point, we can use the events to update the user interface, or to validate edits, updates, or deletes before they are dedicated.

ASPX:

<%@ Page Inherits="DataTableExample" src="DataTableExample.cs" %>
<HTML>
<BODY>
<FORM runat="server" ID="Form2">
<TABLE CellPadding="4" CellSpacing="0" Brider="0">
<TR>
<TD VALIGN="TOP">
<H3>
Products Table
</H3>
<asp:DataGrid runat="server" id="productGrid" CellPadding="4" CellSpacing="0" BorderWidth="1" Gridlines="Horizontal" Font-Names="Verdana, Arial, sans-serif" Font-Size="x-small"
HeaderStyle-Font-Bold="True" OnEditCommand="DataGrid_OnEditCommand" OnCancelCommand="DataGrid_OnCancelCommand" OnUpdateCommand="DataGrid_OnUpdateCommand" OnDeleteCommand="DataGrid_OnDeleteCommand">
<Columns>
<asp:ButtonColumn Text="Delete" CommandName="Delete" />
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel"
UpdateText="Update" />
</Columns>
</asp:DataGrid>
</TD>
<TD VALIGN="TOP">
<H3>
DataTable Events List
</H3><asp:Label runat="server" id="EventsList" Font-Names="Verdana, Arial, sans-serif"
Font-Size="x-small" />
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>

CS:
using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public class DataTableExample : Page
{
//Declare the DataTable object at the class level
protected DataTable myDataTable;
//Map the Web Form server controls
protected DataGrid productGrid, categoryGrid;
protected Label EventsList;
private void MakeData()
{
myDataTable = (DataTable)Cache.Get("myDataTable");
//If myDataTable is not in the cache, create it
if(myDataTable == null)
{
myDataTable = new DataTable("Products");
//Build the Products schema
myDataTable.Columns.Add("ID",
Type.GetType("System.Int32"));
myDataTable.Columns.Add("Name",
Type.GetType("System.String"));
myDataTable.Columns.Add("Category",
Type.GetType("System.Int32"));
//Set up the ID column as the PrimaryKey
DataColumn[] pk = new DataColumn[1];
pk[0] = myDataTable.Columns["ID"];
myDataTable.PrimaryKey = pk;
myDataTable.Columns["ID"].AutoIncrement = true;
myDataTable.Columns["ID"].AutoIncrementSeed = 1;
myDataTable.Columns["ID"].ReadOnly = true;
DataRow tempRow;//Populate the Products table with 10 cars
for(int i = 0; i < 10; i++)
{
//Make every other car a Caterham Seven de Dion
if(Math.IEEERemainder(i,2) == 0)
{
tempRow = myDataTable.NewRow();
tempRow["Name"] = "Caterham Seven de Dion #" +
i.ToString();
tempRow["Category"] = 1;
myDataTable.Rows.Add(tempRow);
}else{
tempRow = myDataTable.NewRow();
tempRow["Name"] = "Dodge Viper #" + i.ToString();
empRow["Category"] = 2;
myDataTable.Rows.Add(tempRow);
}
}
Cache.Insert("myDataTable", myDataTable);
}
}
private void BindData()
{
//Get the DataSet
MakeData();
//Set the DataGrid.DataSource properties
productGrid.DataSource = myDataTable;
//Bind the DataGrid
productGrid.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
//Start with a fresh DataTable
Cache.Remove("myDataTable");
}
//Create a new DataSet by calling the MakeData method
MakeData();
myDataTable.ColumnChanging +=
new DataColumnChangeEventHandler(ColumnChangingHandler);
myDataTable.ColumnChanged +=
new DataColumnChangeEventHandler(ColumnChangedHandler);
//Add RowChanging and RowChanged Event Handlers
myDataTable.RowChanging +=
new DataRowChangeEventHandler(RowChangingHandler);
myDataTable.RowChanged +=
new DataRowChangeEventHandler(RowChangedHandler);
//Add RowDeleting and RowDeleted Event Handlers
myDataTable.RowDeleting +=
new DataRowChangeEventHandler(RowDeletingHandler);
myDataTable.RowDeleted +=
new DataRowChangeEventHandler(RowDeletedHandler);
if(!Page.IsPostBack)
BindData();
}
}
//*********************************************************************//
//********************** DataColumn Event Handlers ********************//
//*********************************************************************//
private void ColumnChangingHandler(object sender,
DataColumnChangeEventArgs e)
{
EventsList.Text += String.Format(
"ColumnChanging Handler
" +
"  Column: {0}
",
e.Column.ColumnName);
string propValue = e.ProposedValue.ToString().ToLower();
if((e.Column.ColumnName == "Name")
&& (propValue.IndexOf("pinto") > -1))
{
throw(new System.Exception(
"Pintos are not allowed on this list."));
}
else
{
EventsList.Text += String.Format(
"  Changing {0} " +
"to {1}
",
e.Row[e.Column.ColumnName],
e.ProposedValue);
}
}
private void ColumnChangedHandler(object sender,
DataColumnChangeEventArgs e)
{
EventsList.Text += String.Format(
"" +
"ColumnChanged Handler
" +
"  Column: {0}
",
e.Column.ColumnName);
EventsList.Text += String.Format(
"  New Value: {0}
",
e.ProposedValue);
}
//*********************************************************************//
//****************************DataRow Event Handlers **********************//
//*********************************************************************//
private void RowChangingHandler(object sender,
DataRowChangeEventArgs e)
{
EventsList.Text += String.Format(
"RowChanging Handler
" +
"  Row: {0}
",
e.Row["ID"]);
EventsList.Text += String.Format(
"  Action: {0}
",
e.Action);
}
private void RowChangedHandler(object sender,
DataRowChangeEventArgs e)
{
EventsList.Text += String.Format(
"" +"RowChanged Handler
" +

"Row: {0}
",
e.Row["ID"]);
EventsList.Text += String.Format(
"  Action: {0}
",
e.Action);
}
private void RowDeletingHandler(object sender,
DataRowChangeEventArgs e)
{
EventsList.Text += String.Format(
"RowDeleting Handler
" +
"Row: {0}
",
e.Row["ID"]);

EventsList.Text += String.Format(
"  Action: {0}
",
e.Action);}
private void RowDeletedHandler(object sender,
DataRowChangeEventArgs e)
{
EventsList.Text += "" +
"RowDeleted Handler
";
EventsList.Text += String.Format("  Action: {0}
",
e.Action);
}
//*********************************************************************//
//********************** DataGrid Event Handlers **********************//
//*********************************************************************//
protected void DataGrid_OnEditCommand(object sender,
DataGridCommandEventArgs e)
{
EventsList.Text = "";
((DataGrid)sender).EditItemIndex = e.Item.ItemIndex;
BindData();
}
protected void DataGrid_OnCancelCommand(object sender,
DataGridCommandEventArgs e)
{
((DataGrid)sender).EditItemIndex = -1;
BindData();
}
protected void DataGrid_OnUpdateCommand(object sender,
DataGridCommandEventArgs e)
{EventsList.Text = "";
//Cast an object as the source DataGrid
DataGrid senderGrid = (DataGrid)sender;
//Invoke MakeData() to create the myDataTable object
MakeData();
//Get the edited item values
TextBox Name = (TextBox)e.Item.Cells[3].Controls[0];
TextBox Category = (TextBox)e.Item.Cells[4].Controls[0];
//Get the PrimaryKey column text
string item = e.Item.Cells[2].Text;
//Get the DataRow from myDataTable
DataRow dr = myDataTable.Rows.Find(Int32.Parse(item));
//Change the DataRow values
//This will raise the ColumnChanging event
try
{
dr[1] = Name.Text;
dr[2] = Int32.Parse(Category.Text);
//Commit the changes to the DataRow
//This will raise the ColumnChanged event
dr.AcceptChanges();
}
catch(Exception ex)
{
EventsList.Text += "Error: " +
ex.Message +
"
"; } //Recache the DataTable Cache.Insert("myDataTable", myDataTable); //Bind the DataGrid senderGrid.EditItemIndex = -1; BindData(); } protected void DataGrid_OnDeleteCommand(object sender, DataGridCommandEventArgs e) { EventsList.Text = ""; //Cast an object as the source DataGrid DataGrid senderGrid = (DataGrid)sender; //Get the Data and create a DataView to filter MakeData(); //Get the PrimaryKey column text string item = e.Item.Cells[2].Text; //Get the DataRow from myDataTable DataRow dr = myDataTable.Rows.Find(Int32.Parse(item)); //Use the Remove() method to delete the row myDataTable.Rows.Remove(dr); //Recache the DataSet Cache.Insert("myDataTable", myDataTable); //Bind the DataGrid senderGrid.EditItemIndex = -1; BindData(); } }

SHARE THIS POST   

0 comments :

Post a Comment

Design by Gohilinfotech | www.gohilinfotech.blogspot.com