c#在listview中添加新行

c#在listview中添加新行,c#,wpf,listview,C#,Wpf,Listview,我使用sql语句从数据库中读取数据,您可以在文本框中键入sql语句。 运行查询时,我希望将返回的数据添加到listview的不同列中 我的代码: using (SqlConnection con = new SqlConnection(connectionString)) { con.Open(); try { using (SqlCommand cmd = new SqlCommand(textBoxQuery.Text, con)) {

我使用sql语句从数据库中读取数据,您可以在文本框中键入sql语句。 运行查询时,我希望将返回的数据添加到listview的不同列中

我的代码:

using (SqlConnection con = new SqlConnection(connectionString))
{
    con.Open();
    try
    {
        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
                     }
                     Console.WriteLine();
                }
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
    con.Close();
}

提前谢谢

如果希望在WPF中动态生成列,则需要在每次重新填充列表视图时动态添加具有适当绑定的列定义

试试这个:

MainWindow.xaml.cs

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()
        {
            InitializeComponent();

            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;
                }

                myItems.Add(myItem);
            }

            // 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;

                columns.Add(column);
            }

            // Add the column definitions to the list view
            gridView.Columns.Clear();

            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)
            {
                this.myListView.Items.Add(item);
            }
        }
    }
}
使用System.Collections.Generic;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
名称空间ListViewTest
{
公共类专栏
{
公共字符串标题{get;set;}
公共字符串源字段{get;set;}
}
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
GridView GridView=新的GridView();
this.myListView.View=gridView;
List myItems=新列表();
动态myItem;
i字典值;
//用动态列填充对象
对于(变量i=0;i<100;i++)
{
myItem=new System.Dynamic.ExpandoObject();
foreach(新字符串[]{“Id”、“Name”、“Something”}中的字符串列)
{
myItemValues=(IDictionary)myItem;
myItemValues[列]=“我对“+列+”-“+i的值;
}
添加(myItem);
}
//假设所有对象都有相同的列-使用第一项确定列
列表列=新列表();
myItemValues=(IDictionary)myItems[0];
//键是列,值是值
foreach(myItemValues中的var对)
{
Column Column=新列();
column.Title=pair.Key;
column.SourceField=pair.Key;
列。添加(列);
}
//将列定义添加到列表视图
gridView.Columns.Clear();
foreach(列中的var列)
{
var binding=新绑定(column.SourceField);
添加(新的GridViewColumn{Header=column.Title,DisplayMemberBinding=binding});
}
//将所有项目添加到列表中
foreach(myItems中的动态项)
{
this.myListView.Items.Add(item);
}
}
}
}
XAML:

<Window x:Class="ListViewTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ListViewTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListView x:Name="myListView"></ListView>
    </Grid>
</Window>

唯一需要更改的是使用sql查询中的列名从读取器中添加列。并填充
读卡器从查询返回的
字段
中的
ExpandoObject


这是我编写的一个快速演示,没有访问本地数据库的权限来测试它,因此我通过for循环和字符串数组来模拟它。

可以使用它从字典中填充listview,您可以从读取器结果创建它,不,这是不可能的,因为我使用WPF而不是winforms。在WPF中,子项不存在。为什么要以编程方式而不是在XAML文件中执行此操作?当我输入新sql语句时,列标题和列数正在更改,因此我需要以编程方式执行此操作