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

A Sample GridView Example using ItemTemplate and BulletList


Posted Date: 16 Jun 2009    Resource Type: Code Snippets    Category: Databinding
Author: DotNetMatrixMember Level: Gold    
Rating: 1 out of 5Points: 8



This junk of code about to display populating data on the GridView

Here
I've taken GridView to display the data
SqlDataSource to connect the database
BulletList to populate Territories

The DataBase is NorthWind





< asp:GridView ID="employeesGridView" Runat="server" DataSourceID="employeesDataSource"
AutoGenerateColumns="False" BorderWidth="1px" BackColor="White" GridLines="Horizontal"
CellPadding="3" BorderStyle="None" BorderColor="#E7E7FF" OnRowDataBound="employeesGridView_RowDataBound">
< FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE">< /FooterStyle>
< PagerStyle ForeColor="#4A3C8C" HorizontalAlign="Right" BackColor="#E7E7FF">< /PagerStyle>
< HeaderStyle ForeColor="#F7F7F7" Font-Bold="True" BackColor="#4A3C8C">< /HeaderStyle>
< AlternatingRowStyle BackColor="#F7F7F7">< /AlternatingRowStyle>
< Columns>
< asp:BoundField HeaderText="LastName" DataField="LastName" SortExpression="LastName">< /asp:BoundField>
< asp:BoundField HeaderText="FirstName" DataField="FirstName" SortExpression="FirstName">< /asp:BoundField>
< asp:TemplateField HeaderText="Territories">< ItemTemplate>
< asp:BulletedList ID="bltTerritories" Runat="server" DataTextField="TerritoryDescription"
DataValueField="TerritoryDescription">
< /asp:BulletedList>
< /ItemTemplate>
< /asp:TemplateField>
< /Columns>
< SelectedRowStyle ForeColor="#F7F7F7" Font-Bold="True" BackColor="#738A9C">< /SelectedRowStyle>
< RowStyle ForeColor="#4A3C8C" BackColor="#E7E7FF">< /RowStyle>
< /asp:GridView>

< asp:SqlDataSource ID="employeesDataSource" Runat="server" SelectCommand="SELECT [EmployeeID], [LastName], [FirstName] FROM [Employees] ORDER BY [LastName], [FirstName]"
ConnectionString="< %$ ConnectionStrings:NWConnectionString %>">
< /asp:SqlDataSource>
< asp:SqlDataSource ID="territoriesDataSource" Runat="server" SelectCommand="SELECT dbo.EmployeeTerritories.EmployeeID, dbo.Territories.TerritoryDescription FROM dbo.Territories INNER JOIN dbo.EmployeeTerritories ON dbo.Territories.TerritoryID = dbo.EmployeeTerritories.TerritoryID"
ConnectionString="< %$ ConnectionStrings:NWConnectionString %>">
< /asp:SqlDataSource>

void employeesGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
// For each DataRow in the GridView, programmatically access the BulletedList, filter
// the DataView based on the GridView row's EmployeeID value and bind the filtered DataView
// to the BulletedList
if (e.Row.RowType == DataControlRowType.DataRow)
{
BulletedList bl = (BulletedList)e.Row.FindControl("bltTerritories");
territoryData.RowFilter = "EmployeeID = " + ((DataRowView) e.Row.DataItem)["EmployeeID"].ToString();
bl.DataSource = territoryData;
bl.DataBind();
}
}


DataView territoryData; // this DataView will hold all of the Territories, loaded at Page_Load

void Page_Load(object sender, EventArgs e)
{
// Load all of the territories into a DataView from the SqlDataSource
territoryData = (DataView)territoriesDataSource.Select(DataSourceSelectArguments.Empty);
}

--
Cheers
Enjoy the coding


For more details, visit http://dotnetpages.blogspot.com/search/label/GridView



Responses

Author: Miss Meetu Choudhary    18 Jun 2009Member Level: Diamond   Points : 1
Please Do Not Forgot to format your resources..

++
Thanks and Regards
Meetu Choudhary
Editor


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
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: How to bind Asp menucontrol from database
Previous Resource: C# - Sort the data table using Select Method
Return to Discussion Resource Index
Post New Resource
Category: Databinding


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use