Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 6th, 2009, 07:40 PM
Friend of Wrox
 
Join Date: Sep 2007
Location: , , .
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default 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
  #2 (permalink)  
Old March 6th, 2009, 09:41 PM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

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>
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
The Following User Says Thank You to Lee Dumond For This Useful Post:
chobo2 (March 8th, 2009)
  #3 (permalink)  
Old March 8th, 2009, 02:16 AM
Friend of Wrox
 
Join Date: Sep 2007
Location: , , .
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default

Ah thanks. So what is better the databind way or this way?

Quote:
Originally Posted by Lee Dumond View Post
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>
  #4 (permalink)  
Old March 8th, 2009, 02:28 AM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

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.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
The Following User Says Thank You to Lee Dumond For This Useful Post:
chobo2 (March 8th, 2009)
  #5 (permalink)  
Old March 8th, 2009, 04:38 PM
Friend of Wrox
 
Join Date: Sep 2007
Location: , , .
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default

Quote:
Originally Posted by Lee Dumond View Post
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:
Lee Dumond (March 8th, 2009)
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Databind DarkForce ASP.NET 2.0 Basics 5 January 25th, 2011 12:04 PM
About the Databind() kuuy BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 1 November 15th, 2006 12:56 PM
DataView will not DataBind with a GridView?! jason.l ASP.NET 2.0 Basics 0 October 13th, 2006 10:01 PM
HELP!!! (DropdownList in GridView question) Aaron Edwards ASP.NET 2.0 Basics 10 June 25th, 2006 05:08 AM
Gridview DataBind to DataTable not showing changes wirerider ASP.NET 2.0 Basics 1 March 4th, 2006 08:59 PM



All times are GMT -4. The time now is 07:17 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.