3

I have a GridView which I assign the DataSource and Bind like this:

protected void Search(object sender, EventArgs e)
{
    using(SqlConnection con = new SqlConnection(constring))
    using(SqlCommand com = new SqlCommand("XXX", con))
    {
        com.CommandType = CommandType.StoredProcedure;

        // parameter declarations are here

        con.Open();

        SqlDataReader rdr = com.ExecuteReader();
        gvResults.DataSource = rdr;
        gvResults.DataBind();

    }
}

There is then an OnRowBound method which looks like this:

protected void gvResults_OnRowBound(object sender, GridViewRowEventArgs e)
{   
    GridViewRow row = e.Row;

    if (row.RowType != DataControlRowType.DataRow) return;

    DataRowView rowdata = (DataRowView)row.DataItem;

    // fancy stuff will happen here

}

When then line that attempts to cast the row's DataItem to DataRowView is reached an error is thrown which reads:

Unable to cast object of type 'System.Data.Common.DataRecordInternal' to type 'System.Data.DataRowView'

I understand that I'm obviously not casting this DataItem to the right kind of class, but my question is what is the right class to be casting to when the DataSource is a SqlDataReader?

Or am I on the wrong track completely?

1 Answer 1

7

The correct type would be IDataRecord

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.