dotnet0824
asked on
Winform comboBox returns dataRowView
Hi,
I have a dataset bound to comboBox1 with records.
In a certain scenario I have to reload the dataset with some other data if combo2 is selected.
So When combo2 is selected I set the bindings off for the comboBox1 and reload a fresh dataset again.
In the selected change event I try to get the student_id when a name is selected. but it returns system.data.RowView
Its bound for the first time like this
this.combobox1.DataSource = _ds.Tables[0];
this.combobox1.DisplayMemb er = "Student_name";
this.combobox1.ValueMember = "student_id";
this.combobox1.SelectedInd ex = -1;
this.combobox1.SelectedInd exChanged += new System.EventHandlerthis.co mbobox1_Se lectedInde xChanged);
this.combobox1.SelectedInd ex = 0;
Now when Combo2 is selected and combobox1 is clicked IN CLICK EVENT OF of comboBox1
I set the bindings off and bind again in the above way to another dataset as It has to be done on fly (ie when combo2 is selected Combo1 has to load some other data)
In the selected change event I try to get the student_ID but it returns data.row.dataviewrow
private void Combobo1_SelectedIndexChan ged(object sender, EventArgs e)
{
if ( combobox1.SelectedIndex > 0 || combobox1.selectedindex !=-1)
{
// this.txtStudentID.Text = combobox1.SelectedItem.ToS tring();
MessageBox.Show(combobox1. SelectedIt em.ToStrin g());
I have a dataset bound to comboBox1 with records.
In a certain scenario I have to reload the dataset with some other data if combo2 is selected.
So When combo2 is selected I set the bindings off for the comboBox1 and reload a fresh dataset again.
In the selected change event I try to get the student_id when a name is selected. but it returns system.data.RowView
Its bound for the first time like this
this.combobox1.DataSource = _ds.Tables[0];
this.combobox1.DisplayMemb
this.combobox1.ValueMember
this.combobox1.SelectedInd
this.combobox1.SelectedInd
this.combobox1.SelectedInd
Now when Combo2 is selected and combobox1 is clicked IN CLICK EVENT OF of comboBox1
I set the bindings off and bind again in the above way to another dataset as It has to be done on fly (ie when combo2 is selected Combo1 has to load some other data)
In the selected change event I try to get the student_ID but it returns data.row.dataviewrow
private void Combobo1_SelectedIndexChan
{
if ( combobox1.SelectedIndex > 0 || combobox1.selectedindex !=-1)
{
// this.txtStudentID.Text = combobox1.SelectedItem.ToS
MessageBox.Show(combobox1.
can you please do the following and try
the reason is that before you supply the data source to databind you need to tell that from the data source which column will be the display member and which one will be the value member
this.combobox1.isplayMembe r = "Student_name";
this.combobox1.ValueMember = "student_id";
this.combobox1.DataSource = _ds.Tables[0];
the reason is that before you supply the data source to databind you need to tell that from the data source which column will be the display member and which one will be the value member
this.combobox1.isplayMembe
this.combobox1.ValueMember
this.combobox1.DataSource = _ds.Tables[0];
ASKER
i have already done that in my posting above.
ASKER
DataRow mRow = (DataRow)combobox1.Selecte dItem;
this.txtStudentID.Text = (String) mRow["student_ID"];
It says unable to cast DataRowView to DataRow
this.txtStudentID.Text = (String) mRow["student_ID"];
It says unable to cast DataRowView to DataRow
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ragi007, though agree on the .SelectedValue as i already posted above, .SelectedText is misleading, and returns any text that the user has highlighted.
.Text will return the actual displayed text of the selected item.
.Text will return the actual displayed text of the selected item.
This is not a problem you can just get what you are looking for like this:
DataRow mRow = (DataRow)combobox1.Selecte
this.txtStudentID.Text = (String) mRow["student_ID"];
Hope this helps.
MAC