简体   繁体   English

如何将DataGridViewComboboxColumn添加到Datagrid

[英]How to add DataGridViewComboboxColumn to a Datagrid

I am creating a table view where some info is given and the user gets a chance to add some additional info found in a Combobox. 我正在创建一个表视图,其中提供了一些信息,用户有机会添加在组合框中找到的一些其他信息。

Adding the normal text fields is no problem. 添加普通文本字段没有问题。 But when I try to add the Combobox column I get the following error: 但是,当我尝试添加“组合框”列时,出现以下错误:

Error CS1503 Argument 1: cannot convert from >'System.Windows.Forms.DataGridViewComboBoxColumn' to >'System.Data.DataColumn' test C:\\Users\\TomekJasinski\\Documents>\\Hello_world\\test\\test\\Window1.xaml.cs 52 Active 错误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;
}

You should add a System.Windows.Controls.DataGricComboBoxColumn to the DataGrid : 您应该将System.Windows.Controls.DataGricComboBoxColumn添加到DataGrid

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

WPF isn't Windows Forms. WPF不是Windows窗体。 In WPF, you generally define UI elements in XAML. 在WPF中,通常在XAML中定义UI元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM