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 7525773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:38:44+00:00 2026-05-30T03:38:44+00:00

I use to linq to sql to fill a gridview: var results = from

  • 0

I use to linq to sql to fill a gridview:

var results = from r in db.MyForm1_hosps
                      where r.recordId == recordId
                      orderby r.hospId
                      select new { r.hospId, r.which, r.description };

        if (results.Count() > 0)
        {
            Form_1_hosp.DataSource = results;
            Form_1_hosp.DataBind();
        }

later during OnRowDataBound, i call the following code to fill in the value of a radiobuttonlist

if (e.Row.RowType == DataControlRowType.DataRow)
        {
            RadioButtonList rbl = e.Row.FindControl("which") as RadioButtonList;
            if (rbl != null)
            {
                DataRowView rowView = (DataRowView)(e.Row.DataItem);
                LoadRadioButtonList(rowView["which"], rbl);
            }
        }

I get the following error:

Unable to cast object of type '<>f__AnonymousType1`3[System.Int32,System.Int16,System.String]' to type 'System.Data.DataRowView'.

I understand that an anonymous object cannot be cast to a datarowview, but what can I cast to in order to get the value of “which”

  • 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-30T03:38:46+00:00Added an answer on May 30, 2026 at 3:38 am

    You should define a proper class to describe your data, and then you will be able to cast to this class.

    // replace with proper names and types, as appropriate 
    class MyData 
    {
         public int HospId { get; set; }
         public string Which { get; set; }
         public string Description { get; set; }
    }
    

    Update your query’s select to utilize this class for the projection

    select new MyData
    {
        HospId = r.hospId, 
        Which = r.which, 
        Description = r.description 
    }; 
    

    And then use the type for the cast.

    MyData obj = (MyData)(e.Row.DataItem); 
    LoadRadioButtonList(obj.Which, rbl); 
    

    There are other techniques for dealing with this, such as using dynamic and letting the runtime figure it out, or using a CastByExample<T> method (you can look it up, but I consider it faily hack-ish), but this is in my opinion the cleanest thing to do.


    You could arguably also simply omit the projection and use the full object

    select r;
    

    At which point you would simply cast to the type of the elements in db.MyForm1_hosps, which is presumably MyForm1_hosp (you would have to verify). The counter against this approach would be if your UI container is auto-generating columns and this class contains more data than you wish to display, in which case, you would want to continue with the projection into a smaller construct.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use LINQ to SQL to select a few specific columns from
I use LINQ-SQL as my DAL, I then have a project called DB which
I have a linq-to-sql query that I use to fill an object that has
I'm trying to use LoadWith in LINQ to SQL to select another table at
I'm trying to use the following LINQ to SQL in my code: (from s
I use Linq-to-SQL which will generate classes for database tables dragged to its designer.
I use Linq to SQL in my project. I fetch data from a SQL
While trying to use LINQ to SQL I encountered several problems. I have table
I decided to use LINQ to SQL in my personal project after hearing lots
We decided to use Linq To SQL for our Data Layer on our most

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.