Solved

Winform comboBox returns dataRowView

Posted on 2008-10-01
8
856 Views
Last Modified: 2013-12-17
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.DisplayMember = "Student_name";
this.combobox1.ValueMember = "student_id";
this.combobox1.SelectedIndex = -1;
this.combobox1.SelectedIndexChanged += new System.EventHandlerthis.combobox1_SelectedIndexChanged);
this.combobox1.SelectedIndex = 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_SelectedIndexChanged(object sender, EventArgs e)
        {

         if ( combobox1.SelectedIndex > 0 || combobox1.selectedindex !=-1)    
            {    

                 // this.txtStudentID.Text = combobox1.SelectedItem.ToString();
                MessageBox.Show(combobox1.SelectedItem.ToString());
0
Comment
Question by:dotnet0824
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 8

Expert Comment

by:mac-will
ID: 22618239
Because you are binding to a DataTable each selection is in fact a DataRow.

This is not a problem you can just get what you are looking for like this:



DataRow mRow = (DataRow)combobox1.SelectedItem;
this.txtStudentID.Text = (String)   mRow["student_ID"];


Hope this helps.

MAC
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22618907
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.isplayMember = "Student_name";
this.combobox1.ValueMember = "student_id";
this.combobox1.DataSource = _ds.Tables[0];
0
 

Author Comment

by:dotnet0824
ID: 22619073
i have already done that in my posting above.
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

Author Comment

by:dotnet0824
ID: 22619107
DataRow mRow = (DataRow)combobox1.SelectedItem;
this.txtStudentID.Text = (String)   mRow["student_ID"];

It says unable to cast DataRowView to DataRow
0
 
LVL 23

Accepted Solution

by:
Snarf0001 earned 300 total points
ID: 22619587
Just use:

this.txtStudentID.Text = combobox1.SelectedValue.ToString();

Since you bound to a datatable, the selectedItem is actually the datarowview from the table.
But since you specified the ValueMember when binding, you can retrieve with .SelectedValue.
0
 
LVL 8

Assisted Solution

by:mac-will
mac-will earned 100 total points
ID: 22621056
Try:

DataRow mRow = ((DataRowView)combobox1.SelectedItem).Row;
this.txtStudentID.Text = (String)   mRow["student_ID"];


0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 100 total points
ID: 22621670
if you are binding you combobox with DisplayMember and ValueMember properties then to reterieve the student id you can do this
comboBox1.SelectedValue.ToString ()

for fetching the text from the combobox (student name) you can use
comboBox1.SelectedText.ToString()

0
 
LVL 23

Expert Comment

by:Snarf0001
ID: 22623960
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.
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question