|
 |
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
|
 |
|
|

March 6th, 2009, 07:40 PM
|
Friend of Wrox
|
|
Join Date: Sep 2007
Location: , , .
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
|
|
Databind a gridview question.
Hi
In a book I have it has this
Code:
protected void gridEmployees_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Get the title of courtesy for the item that's being created.
string title = (string)
DataBinder.Eval(e.Row.DataItem, "TitleOfCourtesy");
// If the title of courtesy is "Ms.", "Mrs.", or "Mr.",
// change the item's colors.
if (title == "Ms." || title == "Mrs.")
{
e.Row.BackColor = System.Drawing.Color.LightPink;
e.Row.ForeColor = System.Drawing.Color.Maroon;
}
else if (title == "Mr.")
{
e.Row.BackColor = System.Drawing.Color.LightCyan;
e.Row.ForeColor = System.Drawing.Color.DarkBlue;
}
}
}
Then after it has this
Quote:
■Tip This example uses the DataBinder.Eval() method to retrieve a piece of information from the data item using
reflection. Alternatively, you could cast the e.Row.DataItem to the correct type (such as EmployeeDetails for the
ObjectDataSource), DataRowView (for the SqlDataSource in DataSet mode), or DbDataRecord (for the SqlDataSource in
DataReader mode). However, the DataBinder.Eval() approach works in all these scenarios (at the cost of being slightly
slower).
This
|
It never tells you how to actually to write the line of code of any of these. Like say for the SqlDataSource in a dataset.
I am not sure how the line would be I
tried to do this:
string title = (RowDataView)e.Row.DataItem.
I got stuck there since I did not know how to choose the header it should be looking for.
Thanks
|

March 6th, 2009, 09:41 PM
|
 |
Wrox Author
|
|
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
|
|
Here you go... blue for the boys, pink for the girls.
Code:
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Data" %>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Fun with Databinding</title>
<script runat="server">
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView rowView = (DataRowView)e.Row.DataItem;
string title = (string)rowView["TitleOfCourtesy"];
if (title == "Ms." || title == "Mrs.")
{
e.Row.BackColor = Color.LightPink;
e.Row.ForeColor = Color.Maroon;
}
else if (title == "Mr.")
{
e.Row.BackColor = Color.LightCyan;
e.Row.ForeColor = Color.DarkBlue;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="TitleOfCourtesy" HeaderText="TitleOfCourtesy"
SortExpression="TitleOfCourtesy" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [FirstName], [LastName], [TitleOfCourtesy] FROM [Employees]">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
|
The Following User Says Thank You to Lee Dumond For This Useful Post:
|
|

March 8th, 2009, 02:16 AM
|
Friend of Wrox
|
|
Join Date: Sep 2007
Location: , , .
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
|
|
Ah thanks. So what is better the databind way or this way?
Quote:
Originally Posted by Lee Dumond
Here you go... blue for the boys, pink for the girls.
Code:
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Data" %>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Fun with Databinding</title>
<script runat="server">
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView rowView = (DataRowView)e.Row.DataItem;
string title = (string)rowView["TitleOfCourtesy"];
if (title == "Ms." || title == "Mrs.")
{
e.Row.BackColor = Color.LightPink;
e.Row.ForeColor = Color.Maroon;
}
else if (title == "Mr.")
{
e.Row.BackColor = Color.LightCyan;
e.Row.ForeColor = Color.DarkBlue;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="TitleOfCourtesy" HeaderText="TitleOfCourtesy"
SortExpression="TitleOfCourtesy" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [FirstName], [LastName], [TitleOfCourtesy] FROM [Employees]">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
|
|

March 8th, 2009, 02:28 AM
|
 |
Wrox Author
|
|
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
|
|
You're welcome. Thanks button is on the right -->
The way I showed is better, because it takes advantage of the fact that SqlDataSource works with the DataView by default, and thus each DataItem can be safely cast to a DataRowView.
On the other hand, DataBinder.Eval uses reflection to determine whether the DataItem should be a DataRowView or a DbDataRecord. Reflection is a rather slow process, so it's worthwhile to avoid it if you can.
|
The Following User Says Thank You to Lee Dumond For This Useful Post:
|
|

March 8th, 2009, 04:38 PM
|
Friend of Wrox
|
|
Join Date: Sep 2007
Location: , , .
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
|
|
Quote:
Originally Posted by Lee Dumond
You're welcome. Thanks button is on the right -->
The way I showed is better, because it takes advantage of the fact that SqlDataSource works with the DataView by default, and thus each DataItem can be safely cast to a DataRowView.
On the other hand, DataBinder.Eval uses reflection to determine whether the DataItem should be a DataRowView or a DbDataRecord. Reflection is a rather slow process, so it's worthwhile to avoid it if you can.
|
Ya I just found out the thank you button exists!
|
The Following User Says Thank You to chobo2 For This Useful Post:
|
|
Thread Tools |
Search this Thread |
|
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |