1

I have a ComboBox which is populated on form load. I get the data from the database in the form of a dataset and use the following to populate the combobox.

checkBox4_combo.DataSource = ds.Tables[0].DefaultView.ToTable(true,"Work_Location");
checkBox4_combo.DisplayMember = "Work_Location";
checkBox4_combo.BindingContext = this.BindingContext;

But, when I try accessing the items of this combobox, it returns an undesirable output. I have tried the following:
Method 1:

object ob = checkBox4_combo.Items[0];
MessageBox.Show(ob.ToString());
//Returns "System.Data.DataRowView"

Method 2:

checkBox4_combo.SelectedIndex = 1;
object ob = checkBox4_combo.SelectedItem;
//OR
object ob = checkBox4_combo.SelectedValue;
MessageBox.Show(ob.ToString());
//Returns "System.Data.DataRowView"

Method 3:

checkBox4_combo.SelectedIndex = 1;
object ob = checkBox4_combo.SelectedText;
MessageBox.Show(ob.ToString());
//Returns <blank>

But, if I use:

checkBox4_combo.SelectedIndex = 1;
object ob = checkBox4_combo.Text;
MessageBox.Show(ob.ToString());

I will get the desired output but that means that the selected text gets changed. So essentially, I would not want to use this, and methods 2 & 3.

Now, besides asking for a solution, I want to understand why the output is either null or "System.Data.DataRowView". Is there something in the way that I am binding the data to the combobox which is a problem? Or is this how it works and I should stick to my last solution?

leppie
  • 115,091
  • 17
  • 196
  • 297
Sanketh. K. Jain
  • 489
  • 1
  • 9
  • 24
  • 2
    try to set the DataMember-Property of your Combobox to the Column you want to see in your MessageBox. – Marco Forberg Dec 07 '15 at 11:44
  • 1
    Are the values in the datatable expected to change or will they remain constant? Because in the second situation, you should convert the given column into a list and make it the datasource (lists, unlikely datatables, are the ideal types for being a combobox datasource and that's why they will automatically deliver the behaviour you want). – varocarbas Dec 07 '15 at 11:59
  • @MarcoForberg Sorry but comboboxes in winforms don't have such a property. – varocarbas Dec 07 '15 at 12:02
  • 1
    @varocarbas Not exactly, but they have something similar - ValueMember. It works as DataMember I assume – Sanketh. K. Jain Dec 07 '15 at 12:06
  • 1
    This doesn't work, at least not directly/in the most logical way. If you want to get the displayed values right away with something like `checkBox4_combo.SelectedValue` you would have to rely on a "more appealing" datasource, like a list. – varocarbas Dec 07 '15 at 12:08
  • 1
    For example: `checkBox4_combo.DataSource = ds.Tables[0].DefaultView.ToTable(true,"Work_Location").AsEnumerable().Select(x => x["Work_Location"]).ToList();` – varocarbas Dec 07 '15 at 12:09
  • @varocarbas I took your advice and did some research and found [this](http://stackoverflow.com/questions/600869/how-to-bind-a-list-to-a-combobox-winforms). As per that, I still would be binding it and would have to use a valuemember, much like what Marco suggested together with user2946329's code. It works. – Sanketh. K. Jain Dec 07 '15 at 12:12
  • 1
    There are many alternatives (even you might rely on a binding approach different than the DataSource property). But the best way to keep things simple and working fine with a combobox is DataSource + List. – varocarbas Dec 07 '15 at 12:13
  • @varocarbas Okay, yes. You're right. This works. The example that you stated works, and it is reduced code. – Sanketh. K. Jain Dec 07 '15 at 12:15
  • @varocarbas you're right. actually i meant ValueMember – Marco Forberg Dec 07 '15 at 12:41

2 Answers2

2

You need to cast checkBox4_combo.SelectedItem to DataRowView like this:

var ob = (DataRowView)checkBox4_combo.SelectedItem;
MessageBox.Show(ob[0].ToString());//Change ob[0] to each column index that you want
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
2

I think you are mixing what you see (controlled by the DisplayMember property) with what you get (controlled by the ValueMember property). When you use DataTable as data source and don't specify ValueMember, it's normal to get DataRowView object from SelectedValue property. Items property elements are also of type DataRowView.

In order to get the display text of a combo box item, you can use GetItemText method like this

int index = ...;
var item = comboBox.Items[index];
var text = comboBox.GetItemText(item);
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343