p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 3.5 Basics (http://p2p.wrox.com/forumdisplay.php?f=351)
-   -   Databind a gridview question. (http://p2p.wrox.com/showthread.php?t=73143)

chobo2 March 6th, 2009 07:40 PM

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

Lee Dumond March 6th, 2009 09:41 PM

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>


chobo2 March 8th, 2009 02:16 AM

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

Quote:

Originally Posted by Lee Dumond (Post 236395)
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>



Lee Dumond March 8th, 2009 02:28 AM

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.

chobo2 March 8th, 2009 04:38 PM

Quote:

Originally Posted by Lee Dumond (Post 236461)
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!


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

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