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 » DataGridView »

Creating a Nested Gridview Control


Posted Date: 05 Nov 2009    Resource Type: Code Snippets    Category: DataGridView
Author: PrajeeshMember Level: Gold    
Rating: 1 out of 5Points: 10



The following code snippet demonstrates a nested gridview control.

Step 1:
Create a gridview control named gvDepartments and add a Template Field to it.
Step 2:
Add another gridview called gvStudents inside the Template field's Item Template
Step 3:
Add following to gvStudents grid view

DataSource ='<%# GetStudentInfo( Convert.ToInt16(Eval("Department_Id")) )%>'

Where GetStudentInfo is the server side function which returns a datatable containing list of students based on department id.
Step 4
Create a method to bind gvDepartments (It must contain a column “Department_Id” as we would be passing this parameter to bind gvStudents).
Step 5
Create a method GetStudentInfo(int department_Id). It accepts Department_Id as parameter and returns datatable containing students list.

public DataTable GetStudentsByDepartment(int DepartmentId)

{

SqlConnection dbConnection = new SqlConnection(ConnectionString);
DataTable dtStudentList = new DataTable();
try

{
dbConnection.Open();
SqlDataAdapter daStudents = new SqlDataAdapter();
SqlCommand cmdSelect = new SqlCommand("SelectStudentByDep",dbConnection);
cmdSelect.CommandType = CommandType.StoredProcedure;
cmdSelect.Parameters.AddWithValue("@Dep", DepartmentId );
daStudents.SelectCommand = cmdSelect;
daStudents.Fill(dtStudentList);

}

catch (Exception objException)

{
HttpContext.Current.Response.Write(objException.Message);
}

finally

{
if (dbConnection != null && dbConnection.State == ConnectionState.Open)
{
dbConnection.Close();
}
}
return dtStudentList;
}




For more details, visit http://prajeeshkk.blogspot.com/2009/11/creating-nested-gridview-control.html



Responses

Author: Abhay    05 Nov 2009Member Level: Diamond   Points : 1
Hi Lajji ,

Please format your code and description and put it under code snippet.


Author: ABitSmart    05 Nov 2009Member Level: Diamond   Points : 0
Copied from http://prajeeshkk.blogspot.com/2009_11_01_archive.html


Author: Prajeesh    05 Nov 2009Member Level: Gold   Points : 1
Dear AbitSmart, that is my blog :), I have the right to copy content from my website, right?


Author: ABitSmart    05 Nov 2009Member Level: Diamond   Points : 0
Actually you don't. Search engine bots would not be able to detect this.


Author: ABitSmart    05 Nov 2009Member Level: Diamond   Points : 1
I have reframed your content this time. Next time onwards please change the content so that is does not match the blog exactly.

Hope I make sense !


Author: Prajeesh    05 Nov 2009Member Level: Gold   Points : 0
Thanks ABitsmart, i will follow your suggestions.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
NestedGridview  .  Gridview  .  

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: Select and Highlight an entire row in DataGridView
Return to Discussion Resource Index
Post New Resource
Category: DataGridView


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use