C#列表视图中添加新行 [英] c# Add new row in listview

查看:140
本文介绍了C#列表视图中添加新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读使用SQL语句,您可以键入文本数据库中的数据。
当我运行查询我要返回的数据添加到列表视图的不同列。

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.

我的code:

            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,你需要在每次重新填充列表视图时动态添加列定义与相应的绑定。

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.

试试这个:

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

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循环和字符串数组模仿它。

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.

这篇关于C#列表视图中添加新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆