C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ADO.NET »

DataGrid within DataGrid - Master/Detail view


Posted Date: 11 Mar 2007    Resource Type: Code Snippets    Category: ADO.NET
Author: Tony JohnMember Level: Diamond    
Rating: 1 out of 5Points: 10



This sample snippet shows how to use a data grid within a datagrid to display the master/detail relationship. Similar approach can be used to display DataGridView.

Place the following code in your ASP.NET page:


















Now, bind data to the parent grid:

SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=StudentDatabase;Persist Security Info=True;User ID=userid;Password=pass");
SqlCommand command = new SqlCommand("SELECT StudentId, StudentName FROM Students", connection);
connection.Open();
dgStudents.DataSource = command.ExecuteReader();
dgStudents.DataBind();
connection.Close();


The last thing to do is, on ItemDataBound event of the parent grid, bind the detail grid.

protected void dgStudents_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=StudentDatabase;Persist Security Info=True;User ID=userid;Password=pass");
SqlCommand command;

int studentId = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "StudentId"));

DataGrid dgExamsResults = (DataGrid)e.Item.FindControl("dgExamsResults");

command = new SqlCommand("SELECT ExamDate, Marks FROM ExamResults WHERE StudentId = " + studentId, connection);
connection.Open();

dgExamsResults.DataSource = command.ExecuteReader();
dgExamsResults.DataBind();
connection.Close();
}
}




Responses

Author: Sheeban    04 Aug 2009Member Level: Bronze   Points : 1
is there any way to get data on the click of hyperlink which is present in master grid


Author: Sheeban    04 Aug 2009Member Level: Bronze   Points : 1
is there any way to get data on the click of hyperlink which is present in master grid


Author: Abhay    13 Aug 2009Member Level: Diamond   Points : 1
Hi Tony john,

good one .

keep it up.

contribute more.

Thanks and Regards
Abhay


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

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: Using DefaultView to sort or filter a Table
Previous Resource: How to differentiate a Typed Dataset and Un-Typed dataset in your code?
Return to Discussion Resource Index
Post New Resource
Category: ADO.NET


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use