Fuzzi59 Fuzzi59 - 1 year ago 69
C# Question

c# Add new row in listview

I read data from a database using sql-statements, which you can type in a textbox.
When I run the query I want to add the returned data into different columns of the listview.

My code:

using (SqlConnection con = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand(textBoxQuery.Text, con))
using (SqlDataReader reader = cmd.ExecuteReader())
GridView view = new GridView();
string[] head = getColumnsName(textBoxQuery.Text);//Getting the columns names
for (int i = 0; i != reader.FieldCount; i++)
view.Columns.Add(new GridViewColumn() { Header = head[i] });//columns heading
listView.View = view;
while (reader.Read())
for (int i = 0; i != reader.FieldCount; i++)
//inserting data into one row and different columns
catch (Exception ex)

Thanks in advance!

Answer Source

If you want the columns to generate dynamically for you in WPF you need to dynamically add column definitions with appropriate bindings each time you repopulate the list view.

Try this:


using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace ListViewTest
    public class Column
        public string Title { get; set; }
        public string SourceField { get; set; }

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        public MainWindow()

            GridView gridView = new GridView();
            this.myListView.View = gridView;

            List<dynamic> myItems = new List<dynamic>();
            dynamic myItem;
            IDictionary<string, object> myItemValues;

            // Populate the objects with dynamic columns
            for (var i = 0; i < 100; i++)
                myItem = new System.Dynamic.ExpandoObject();

                foreach (string column in new string[] { "Id", "Name", "Something" })
                    myItemValues = (IDictionary<string, object>)myItem;
                    myItemValues[column] = "My value for " + column + " - " + i;


            // Assuming that all objects have same columns - using first item to determine the columns
            List<Column> columns = new List<Column>();

            myItemValues = (IDictionary<string, object>)myItems[0];

            // Key is the column, value is the value
            foreach (var pair in myItemValues)
                Column column = new Column();

                column.Title = pair.Key;
                column.SourceField = pair.Key;


            // Add the column definitions to the list view

            foreach (var column in columns)
                var binding = new Binding(column.SourceField);

                gridView.Columns.Add(new GridViewColumn { Header = column.Title, DisplayMemberBinding = binding });

            // Add all items to the list
            foreach (dynamic item in myItems)


<Window x:Class="ListViewTest.MainWindow"
        Title="MainWindow" Height="350" Width="525">
        <ListView x:Name="myListView"></ListView>

Only thing you need to change is adding columns from the reader by using the column name from sql query. And populating the ExpandoObject from fields returned by the reader from your query.

This is a quick demo I written up and didn't have access to a local database to test it so I mimicked it by the for loops and the string array.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download