1
votes

I have a comboBox that is populated with the headers of a database.

When a user selects an header, i want to display the values of that column in a listBox.

I am trying to do it this way.

 private void button3_Click_2(object sender, EventArgs e)
    {
        listBox1.Items.Clear();

        //name of column to display data from
        var selectedColumn = comboBox1.Text.ToString();

        //dataRow array of data
        var selectedColumnItems = aSH_ORDER_DBDataSet1.ASH_PROD_ORDERS.Select(selectedColumn);

        //iterate through dataRow and display in listBox
        foreach (var columnItem in selectedColumnItems)
        {
          listBox1.Items.Add(columnItem);
        }
    }

However, if i select a non boolean type column it get this error:

"Filter expression does not evaluate to a Boolean term"

And even when i do select a boolean column, it just displays the name of the program. It does however seem to be getting some data from the database as it displays the correct number of program names from the number of "true" terms in the database.

1
The important information is missing here. What is the content of comboBox1.Text? (And do not call ToString on a string property, it is useless) - Steve
"it just displays the name of the program" I guess it's the namespace of the type of the items in ASH_PROD_ORDERS, because you add the whole columnItem. I think you either need to set a DisplayMember in your listBox1 or use listBox1.Items.Add(columnItem.Name); or whatever property of your ash_prod_order you want to display. - René Vogt

1 Answers

2
votes

If you want to show the content of a single column without any condition you don't need to SELECT but you just loop over the rows of your table

private void button3_Click_2(object sender, EventArgs e)
{
    listBox1.Items.Clear();

    //name of column to display data from
    var selectedColumn = comboBox1.Text.ToString();

    //iterate through dataRow and display in listBox
    foreach (DataRow row in aSH_ORDER_DBDataSet1.ASH_PROD_ORDERS.Rows)
    {
        listBox1.Items.Add(row[selectedColumn].ToString());
    }
}

The DataTable.Select method is used when you need to filter the content of the table adding some kind of expression that could be parsed to a boolean value.

DataRow[] result aSH_ORDER_DBDataSet1.ASH_PROD_ORDERS.Select("ColumnName = 'ColumnValue'");

In the example above we take a subset of the table rows that have the 'ColumnValue' string present in the column named 'ColumnName'.
Other useful uses of Select are tied to the creation of a new Table with a particular order. See the DataColumn.Select and the DataTableExtensions.CopyToDataTable documentation