Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7035995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:22:15+00:00 2026-05-28T01:22:15+00:00

Update In editing the question I have spotted my own mistake (invalid cast to

  • 0

Update

In editing the question I have spotted my own mistake (invalid cast to a string.)

Many thanks to all, all suggestions most helpful.

Original Question

I have an .aspx page with a GridView, databound to an ObjectDataSource which is filled with a DataSet containing a DataTable.

When handling the RowDataBound callback on the GridView however, I am unable to reference columns in the underlying dataset by column name, they just return an object of type DBNull. So:

    protected void accountsGV_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // One way:
            // This works, but uses a hard coded column index which
            // I want to replace.
            if (e.Row.Cells[11].Text != " ")
            {
                // Apply certain styles to the row depending on
                // the contents of the cell.
            }


            // Another way. This doesn't work - "date" is always DBNull.
            DataRow row = (e.Row.DataItem as DataRowView).Row;
            DateTime date;

            if(!Convert.IsDBNull(row["date"]))
            {
                date = (DateTime)row["date"]);

                if (date < SomeConstantDate)
                {
                    // Apply certain styles to the row depending on
                    // the contents of the cell.
                }
            }
        }
    }

So specifically:

  • row["date"] returns an object of type DBNull.
  • row[11] returns a DateTime.
  • row.Table.Columns["date"] returns a valid DataColumn
  • row[row.Table.Columns["date"]] returns an object of type DBNull.

Can anyone suggest why this might be happening?

Setup

<asp:GridView ID="GV" runat="server" DataSourceID="ODS"
    AutoGenerateColumns="false"
    ... snip ...
    onrowdatabound="GV_RowDataBound" >
    <Columns> ... </Columns>
 </asp:GridView>


 <asp:ObjectDataSource ID="ODS" runat="server" 
    SelectMethod="getData"
    ... snip ...
</asp:ObjectDataSource>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:22:16+00:00Added an answer on May 28, 2026 at 1:22 am

    there are a few things to consider.

    1. e.Row.Cells is referring to the formatted output, not the object itself.
    2. null values are rendered as non-blanking white space hence &nbsp;
    3. column names are case sensative. “Date” != “date”
    4. the column name implies the type of the value is a DateTime object, not a string, so softly casting from a DateTime to string will return null, not the string value of the DateTime object.

    instead try this

    var data = (DataRowView)e.Row.DataItem;
    var date = data["Date"];
    if(date != null and date.ToString() == "...")
    {
       do something based on the value of date.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a gridview inside an update panel, that is doing inline editing. When
I'm using Vim for all program editing and I have a standard header I
update: I mistyped 2 variables...so embarrassing. thanks everyone for the effort! sorry i find
Update: I reported this as a bug to Apple and they fixed it! All
UPDATE 6/21/12 I have a form in rails that is working similar to an
Update : I found almost exact similar question , yet it has slightly different
I have a question regarding uitable view. I am implementing an app which is
I have been developing Android application, and I have one question - ListView uses
I have a basic question regarding timers. My timer is acting very strange. I
I have an application (CLI) which includes the feature of editing account information. It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.