Friday, March 13, 2009

Overview of DataGrid

1) What is Datagrid?
1) Datagrid is a used to display formatted data from database
2) Data Grid is the standard control for viewing data in .Net environment. A data grid control is represented in .Net by System.Windows.Forms.DataGrid class. The data grid control can display the data from a number of data sources e.g. data table, dataset, data view and array.
3) Datagrid having types of columns that will help you to display formatted data and also you can able to edit & update that data within datagrid

2) Datagrid Columns
1) Bound Column - Bound Column used to bound database value directly to that column
2) Button Column - There are three types of buttons in datagrid
1) Select Button - used to select particular row of datagrid - Datagrid_SelectCommand () is associated with this functionality
2) Edit/Update/Cancel Button - used to edit/update/cancel record within Datagrid - No need to use external coding for edit and update record. - Datagrid_EditCommand function associated with this functionality
3) Delete Button - Used to delete particular row/record from datagrid - Datagrid_DeleteCommand function associated with this functionality.

IMP NOTE: If you want to use any external button or image in datagrid and you want to perform one of the above datagrid button functionality than just set the command property of that button to particular command e.g. for Edit Records - use edit , for Delete Records - use Delete etc.

3) Template Column - This column is very useful when you want to use another server control within Datagrid

Template Column Creation Steps:

1) Right click on datagrid and click on Property builder option it will open new small property window.
2) Now select Column tab than Add new Template Column and give header text to it.
3) close that window and again right click on datagrid and select Edit Template Columns option
4) Select your added column you will find Template Column with ITEM TEMPLATE heading
5) Drag & Drop one Server Control in ITEM TEMPLATE e.g. DropDownList
6) Now your Template column is ready to use.

Coding for Template Column:

1) Go to Datagrid Properties and click on Datagrid_ItemDatabound () Event.
2) Why this ItemDatabound () function? Let me Explain
Explanation: After Successfully Bind each row of datagrid this function will fired every time so its called ItemDataBound not ItemDataBinding.
3) Now we are going to bind our DropDownList in this function. Please refer following code for that
Code Language: C#

private void Datagrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
// We need to find DropDownList from our ItemTemplate Column and so there is no need to check
// header, footer and Pager Columns so this loop done that job

if ( e.Item.ItemType == ListItemType.AlternatingItem
|| e.Item.ItemType == ListItemType.Item
|| e.Item.ItemType == ListItemType.SelectedItem )
{
DropDownList drp = (DropDownList)e.Item.FindControl("DropDownList1"); //Type Casting
// Another way to write above code
DropDownList drp = e.Item.FindControl("DropDownList1") as DropDownList; //Type Casting

// Now Bind or fill that DropDownList
Dataset ds = new Dataset();
// your select query
// Fill Dataset using Adapter
// Suppose you bind Employee Table that having a Employeeid(PK) and EmployeeName Columns

DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "EmployeeName"; // to be displayed in dropdownlist
DropDownList1.DataValueField = "Employeeid" ;//Hidden Value
DropDownList1.DataBind();

// This code successfully bound dropdownlist control and using this code you are able to use any server control in Datagrid. You can also able to create Nested Datagrid means Datagrid inside Datagrid using same functionality.

}
}



4) Hyperlink Column
Hyperlink column very easy to use. You just need to set properties like NavigateURL, Querystring etc. so I think there is no need to explain this column functionality in details

3) Datagrid Events List:
1) Datagrid_ItemDatabound() – Explain above in detail
2) Datagrid_ItemCommand() – If you are using any external image in button column and give that button command property like remove than whenever that button click you need to check in this function and do whatever you want.
e.g.

private Datagrid_ItemCommand()
{
if(e.CommandName == “remove”)
{
// Do your code
}
}
Generally we are using this functionality to develop ADD TO CART button in E-Commerce application.
3) Datagrid_EditCommand() – already explained above
4) Datagrid_DeleteCommand() – already explained above
5) Datagrid_SelectCommand() – already explained above
6) Datagrid_PageIndexChanged() – Use to create Paging functionality in datagrid
7) Datagrid_SortCommand() – Use to create Sort functionality in datagrid. For this you need to assign SortExpression property of every columns.
8) Datagrid_UpdateCommand() – Use to create inbuilt functionality of Update records from datagrid.

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home