Darshan N
Darshan N

Reputation: 3

c# combobox DisplayMember showing empty rows from datatable

I have a multi column datatable like this, datatable

I'm loading it to a combobox.

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

            datatable osdat = Loaddatatable();
            postOScomboBox2.DataSource = osdat;
            postOScomboBox2.DisplayMember = "Product";
            postOScomboBox2.ValueMember = "Product";
            postOScomboBox2.SelectedIndex = -1;

    }

Instead of just showing p1,p2 the combobox is also showing two extra empty rows. Same happens if i load the p1 column, 1 empty row is shown in combobox. Does display member have any property to check empty values and load only filled ones or any other way to achieve the same?

Upvotes: 0

Views: 370

Answers (2)

mperry
mperry

Reputation: 91

You can filter your table using methods from System.Linq.

datatable osdat = Loaddatatable();
var filteredTable = osdat.AsEnumerable()
     .Where(row => row.Field<String>("Product") != null).CopyToDataTable();
postOScomboBox2.DataSource = filteredTable;
postOScomboBox2.DisplayMember = "Product";
postOScomboBox2.ValueMember = "Product";
postOScomboBox2.SelectedIndex = -1;

Upvotes: 1

Access Denied
Access Denied

Reputation: 9461

You have two empty values in product column and one empty value in p1 column. Combobox works as expected. If you want to filter these values filter your datasource instead. it's not a responsibility of combobox to filter datasource.

Here is a link how to filter datatable from empty values: Filtering a empty string in DataTable

Upvotes: 1

Related Questions