Baba mba
Baba mba

Reputation: 29

Why do i get System.Data.DataRow?

I wanna show my rows in a listbox and the table names but when i run my code it says System.Data.DataRow can someone help me

my code is

foreach (object SelectedValue in ListEmployees.SelectedItems)
            {
                using (connection = new SqlConnection(connectionstring))
                using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM " + selectedCel, connection))
                {
                    DataTable employees = new DataTable();
                   adapter.Fill(employees);

                    for (int i = 0; i < employees.Rows.Count; i++)
                    {
                        DataRow dr = employees.Rows[i];
                        ListViewItem itm = ListViewItem(dr.ToString());
                        listView1.Items.Add(itm);

                    }
                }
            } 

Upvotes: 1

Views: 5507

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460138

DataRow.ToString() just prints out the full type name.

Presuming you want to show the first column:

ListViewItem itm = new ListViewItem(dr.Field<string>(0));

i wanna show all columns that are in the table

string[] allColumns = dr.ItemArray.Select(obj => obj.ToString()).ToArray();
ListViewItem itm = new ListViewItem(allColumns);

Upvotes: 3

Related Questions