C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




OPERATIONS IN DATAGRID IN ASP.NET 2.0 WITH C#


Posted Date: 08 Aug 2006    Resource Type: Articles    Category: Web Applications
Author: Yamuna Devi RMember Level: Silver    
Rating: Points: 10



Introduction



OPERATIONS IN DATAGRID IN ASP.NET 2.0 WITH C#

Paragraph Heading 1



This article talks about how to use the data grid in web application.

Step1: Design the DataGrid

àDrag datagrid from the toolbox and place it in our web page.Goto the property window and select property builder.

àNow we can see the DataGrid properties.Select Columns option in the left hand side.Expand the “Button column”.We can see the Select,Edit and Delete buttons.

àMove these three buttons from Available columns to selected columns by clicking “>”.
Check button type should be Link Button.Then press OK.

àNow you can see these buttons present in each row of the datagrid.

àThen you need to write bind_data() procedure where you should bind the table to the datasource to the datagrid.

àExample

private void bind_data()
{
dataSet11.Clear();
oleDbDataAdapter1.Fill(dataSet11);
dataView1.Table=dataSet11.yamu;
DataGrid1.DataSource=dataView1;
try
{
DataGrid1.DataBind();
}
catch
{
DataGrid1.CurrentPageIndex = 0;
bind_data();
}
}

Step2: Writing Select Event

àSelect the properties window for the data grid and expand the selected item style property. Here you can specify the selected items property like back color, font color etc.

àGo to the Events property and double click the “SelectedIndexChanged”.It will open the event property in the code behind window.

àExample

private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
Label1.Text=DataGrid1.SelectedItem.Cells[3].Text;
}


Cell[0]àSelect Command Button.
Cell[1]àEdit Command Button.
Cell[2]àDelete Command Button.
Our datacolumn starts from 3.

Inside this you can specify whatever operations you want to perform on the selected Item.


Step2: Writing Edit Event

àGo to the Events property and double click the “EditCommand”.It will open the event property in the code behind window.

àExample

private void DataGrid1_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=e.Item.ItemIndex;
bind_data();
}


After click this edit button you can see the update and cancel button instead of edit button.You can also see the data column for that row has been changed into textbox.
In that text box we can edit the values.


Step2: Writing Update Event

àGo to the Events property and double click the “UpdateCommand”.It will open the event property in the code behind window.

àTable Yamu has 2 columns “Name”, ”ID”.

àExample

private void DataGrid1_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{

System.Web.UI.WebControls.TextBox cName = new
System.Web.UI.WebControls.TextBox();
System.Web.UI.WebControls.TextBox cID = new
System.Web.UI.WebControls.TextBox();
cName = (System.Web.UI.WebControls.TextBox) e.Item.Cells[4].Controls[0];
cID = (System.Web.UI.WebControls.TextBox) e.Item.Cells[3].Controls[0];
String ss="update yamu set name='"+cName.Text.ToString()+"' where id='"+cID.Text.ToString()+"'";
oleDbCommand3.CommandText=ss;
oleDbConnection1.Open();
oleDbCommand3.ExecuteNonQuery();
oleDbConnection1.Close();
DataGrid1.EditItemIndex=-1;
bind_data();
}


Step2: Writing Cancel Event

àGo to the Events property and double click the “CancelCommand”.It will open the event property in the code behind window.

àExample

private void DataGrid1_CancelCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=-1;
bind_data();
}


Step2: Writing Delete Event

àGo to the Events property and double click the “DeleteCommand”.It will open the event property in the code behind window.

àExample

private void DataGrid1_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
oleDbConnection1.Open();
String ss="delete from yamu where id='"+Int32.Parse(DataGrid1.Items[e.Item.ItemIndex].Cells[3].Text.ToString())+"' and name='"+DataGrid1.Items[e.Item.ItemIndex].Cells[4].Text.ToString()+"'";
oleDbCommand3.CommandText=ss;
oleDbCommand3.ExecuteNonQuery();
oleDbConnection1.Close();
bind_data();
}




Paragraph Heading N





Summary







Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
OPERATIONS  .  DATAGRID  .  ASP.NET 2.0  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: State Management in ASP.Net
Previous Resource: Difference Between usercontrol and custom control in .net
Return to Discussion Resource Index
Post New Resource
Category: Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use