Tuesday, October 19, 2010

Unable to cast object of type System.Data.DataRowView to type System.IConvertible

Wow...

Long time.. A lot of distractions. I have been interested in a lot new things. With Windows Phone, Robotics coming into picture, the hobbies and pass-times all changed around.

But to get back on the track.. Here is a very small piece of information that I am sure would help a lot of people.

I encountered the above error while working with a Combobox in a Windows Application.

This would come up every time the form loads and the the SelectedValue of the combobox was selected.

This normally would occur to comboboxes, which are databound.

So here is the problem and the solution.

This is how we usually bind the combobox to data

cboBox.DataSource = data_set.Tables["table_name"].DefaultView;

cboBox.DisplayMember = "field_name";

cboBox.ValueMember = "field_id";


Instead, to resolve this issue all we need to do is first specify the DisplayMember and ValueMember and then Bind.


cboBox.DisplayMember = "field_name";

cboBox.ValueMember = "field_id";

cboBox.DataSource = data_set.Tables["table_name"].DefaultView;


6 comments:

  1. Anonymous10:27

    Thank u very much

    ReplyDelete
  2. Anonymous15:39

    thank u soooo ver much man!!!!
    but y does this happen!?

    ReplyDelete
  3. hi,

    But I have one Question, If first combo box have 8 value then 8 times fire selectedindexchanged event. Why?

    ReplyDelete
  4. Anonymous21:48

    thanks alooooooooooooooooooooooooooooooooooooooooooooooooot dear

    ReplyDelete
  5. Anonymous16:33

    You gave me amazing solution thanks a lot!!!

    ReplyDelete
  6. Thanks alot.......

    ReplyDelete