【问题标题】:How to add DataGridViewComboboxColumn to a Datagrid如何将 DataGridViewComboboxColumn 添加到 Datagrid
【发布时间】:2019-09-02 13:12:10
【问题描述】:

我正在创建一个表格视图,其中给出了一些信息,并且用户有机会添加在组合框中找到的一些附加信息。

添加普通文本字段没问题。但是当我尝试添加 Combobox 列时,出现以下错误:

错误 CS1503 参数 1:无法从 >'System.Windows.Forms.DataGridViewComboBoxColumn' 转换为 >'System.Data.DataColumn' 测试 C:\Users\TomekJasinski\Documents>\Hello_world\test\test\Window1.xaml .cs 52 活跃

private void populateGrid(List<String> str) 
{

     DataTable table = new DataTable();

     DataColumn xCord = new DataColumn("X Cord", typeof(string));
     DataColumn yCord = new DataColumn("Y Cord", typeof(string));
     DataColumn rotation = new DataColumn("Rotation", typeof(string));
     DataColumn partRef = new DataColumn("Part Reference", typeof(string));
     DataColumn partNumb = new DataColumn("Part Number", typeof(string));

     table.Columns.Add(xCord);
     table.Columns.Add(yCord);
     table.Columns.Add(rotation);
     table.Columns.Add(partRef);
     table.Columns.Add(partNumb);

     DataGridViewComboBoxColumn dgvCmb = new DataGridViewComboBoxColumn();
     dgvCmb.HeaderText = "Name";
     dgvCmb.Items.Add("Ghanashyam");
     dgvCmb.Items.Add("Jignesh");
     dgvCmb.Items.Add("Ishver");
     dgvCmb.Items.Add("Anand");
     dgvCmb.Name = "cbColumn";
     table.Columns.Add(dgvCmb); // <- Error

     foreach (string current in str)
     {
           string[] temp = current.Split(Convert.ToChar(","));
           DataRow row = table.NewRow();
           row[0] = temp[0];
           row[1] = temp[1];
           row[2] = temp[2];
           row[3] = temp[3];
           row[4] = temp[4];

           table.Rows.Add(row);
      }
      dataGrid.ItemsSource = table.DefaultView;
}

【问题讨论】:

    标签: c# wpf datatable datagrid


    【解决方案1】:

    您应该将System.Windows.Controls.DataGricComboBoxColumn 添加到DataGrid

    DataGridComboBoxColumn dgvCmb = new DataGridComboBoxColumn();
    dgvCmb.ItemsSource = new List<string>
    {
        "Ghanashyam",
        "Jignesh",
        "Ishver",
        "Anand"
    };
    dataGrid.Columns.Add(dgvCmb);
    

    WPF 不是 Windows 窗体。在 WPF 中,您通常在 XAML 中定义 UI 元素。

    【讨论】:

    • 太好了,做到了。我将把问题留待更长时间,但非常感谢!
    猜你喜欢
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    • 2015-11-03
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多