【问题标题】:C# creating 2 rows, the first one is a text view list and the second is a comb box listC#创建2行,第一行是文本视图列表,第二行是组合框列表
【发布时间】:2014-12-12 23:05:12
【问题描述】:

我的项目是一个 C# 应用程序,用于将数据从 Excel 工作表导入数据库并提示用户手动映射它们。

我需要创建两行:

  1. 第一行是DataGridTextColumn,其中excel的标题 适合。
  2. 第二行是DataGridComboBoxColumn,具有数据库的column_name

每个 DataGridTextColumn 都有一个组合框。

但是我遇到了一个问题,我无法使 DataGridComboBoxColumn 工作,每次我启动应用程序时,第一行正常工作但第二行是空的

代码:

foreach (DataRow row in dt.Rows)
{
     DataGridTextColumn dgtc = new DataGridTextColumn();
     dgtc.MinWidth = 100;
     dgtc.CanUserSort = false;
     dgtc.Header = row["Column_name"].ToString();
     dg.Columns.Add(dgt);
     DataGridComboBoxColumn dgcbc = new DataGridComboBoxColumn();
     dgcbc.ItemsSource = columnList;
     dgcbc.MinWidth = 100;
     dg2.Columns.Add(dgcbc);
}

xaml:

 <DataGrid x:Name="dg" HorizontalAlignment="Left" Height="29" Margin="11,72,0,0" VerticalAlignment="Top" Width="579"/>
 <DataGrid x:Name="dg2" HorizontalAlignment="Left" Height="30" Margin="11,106,0,0" VerticalAlignment="Top" Width="579"/>

实时取景:

代码正在运行,但组合框始终显示空字段。

关于为什么 DataGridComboBoxColumn 不适合我的任何帮助?

【问题讨论】:

  • columnList 来自哪里?我猜那要么是null,要么是空的……
  • SqlConnection conn = new SqlConnection("server=***;database=***;Integrated Security = true"); string[] 限制 = new string[4] { null, null, "orders", null }; conn.Open(); var columnList = conn.GetSchema("Columns", 限制).AsEnumerable().Select(s => s.Field("Column_Name")).ToList();这足以确保 columnList 不为空并且它有 4 个字符串,我什至对其进行了测试..

标签: c# wpf datagrid datagridcomboboxcolumn datagridtextcolumn


【解决方案1】:

您可能指的是 2 个问题。 第一个是设置 dgcbc.ItemsSource = columnList 不足以使 ComboBox 显示可供选择的项目列表。

根据 columnList 的类型需要设置 DisplayMemberPath 和 SelectedValuePath 属性,例如:

var columnList = new Dictionary<string, string>
{
    { "123", "test 123" },
    { "aaa", "test aaa" },
    { "qwe", "test qwe" }
};

dgcbc.ItemsSource = columnList;
dgcbc.DisplayMemberPath = "Key";
dgcbc.SelectedValuePath = "Value";

另一个问题是将列绑定到在 DataGrid 对象上设置的 ItemsSource。

dg2.ItemsSource = dt;
dgcbc.TextBinding = new Binding(string.Format("[{0}]", "Column_Name");

您可能还想知道如何在 DataGridTextColumn 中显示文本:

dgtc.Binding = new Binding(string.Format("[{0}]", "Column_Name");

我不确定这是否是您想要实现的列映射,可能您想修改网格的标题模板并将网格数据显示为下面的文本。为此,请使用 DataGridTemplateColumn DataGridTextColumn 列,其标题中包含标题标签和组合框。 希望有帮助。

编辑:

我准备了一个快速而肮脏的代码解决方案。

XAML:

<DataGrid x:Name="dg" Grid.Row="0" AutoGenerateColumns="False"/>

后面的代码:

        // data is a rough equivalent of DataTable being imported
        var data = new List<Dictionary<string, string>>
        {
            new Dictionary<string, string> { { "column1", "asd asfs af" }, { "column2", "45dfdsf d6" }, { "column3", "7hgj  gh89" } },
            new Dictionary<string, string> { { "column1", "aaasdfda" }, { "column2", "45sdfdsf 6" }, { "column3", "78gh jghj9" } },
            new Dictionary<string, string> { { "column1", "s dfds fds f" }, { "column2", "4dsf dsf 56" }, { "column3", "78gh jgh j9" } },
        };

        // a list of columns to map to
        var importToColumns = new List<string>
        {
            "123",
            "aaa",
            "qwe",
            "456",
            "bbb"
        };

        importMappings = new Dictionary<string, int>();
        foreach(var column in data[0])
        {
            importMappings.Add(column.Key, -1);
        }

        foreach(var r in importMappings)
        {
            var dgtc = new DataGridTextColumn();
            dgtc.Binding = new Binding(string.Format("[{0}]", r.Key));
            var sp = new StackPanel();
            dgtc.Header = sp;
            sp.Children.Add(new Label { Content = r.Key });
            var combo = new ComboBox();
            sp.Children.Add(combo);
            combo.ItemsSource = importToColumns;
            var selectedBinding = new Binding(string.Format("[{0}]", r.Key));
            selectedBinding.Source = importMappings;
            combo.SetBinding(Selector.SelectedIndexProperty, selectedBinding);
            dgtc.MinWidth = 100;
            dgtc.CanUserSort = false;
            dg.Columns.Add(dgtc);
        }

        dg.ItemsSource = data;
    }

    private Dictionary<string, int> importMappings;

选择被批准后,importMappings 将包含列映射列表 - 对于每个导入文件列,它将包含 importToColumns 列表中元素的索引,如果没有选择任何元素,则为 -1。

【讨论】:

  • 现在多亏了你,我知道了我的问题.. 如何实现一个 DataGridTemplateColumn 列,其中包含标题标签和 ComboBox。不幸的是,已经进行了 3 天的研究,但一无所获..
  • 我对代码做了一些配置,但感谢它对我的帮助很大。
猜你喜欢
  • 1970-01-01
  • 2015-10-14
  • 2021-08-09
  • 2021-09-19
  • 2020-10-29
  • 2015-02-06
  • 1970-01-01
  • 2016-11-09
  • 2018-01-14
相关资源
最近更新 更多