2016-08-05 46 views
1

我有一個現有的數據網格視圖,其DataSource是一個數據表,我從一個自定義對象的列表中填充。將ComboBox列添加到現有的DataTable DataGridView

private myDataTable = new DataTable(); 

List<SomeObjectModel> dataSource = (from e in queryResults 
    select 
     new SomeObjectModel 
     { 
      Id = e.Id, 
      Priority = e.Name, 
      Channel = e.Channel 
     }).ToList(); 

myDataTable = ToDataTable(dataSource); //See method below 
dataGridView.DataSource = myDataTable; 

從StackOverflow上先前的問題,我發現我使用那裏的轉彎對象的名單到一個DataTable使用反射的工作方法ToDataTable:

public static DataTable ToDataTable<T>(List<T> items) 
    { 
     DataTable dataTable = new DataTable(typeof(T).Name); 
     PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); 

     foreach (PropertyInfo prop in Props) 
     { 
       dataTable.Columns.Add(prop.Name); 
     } 

     foreach (T item in items) 
     { 
      var values = new object[Props.Length]; 
      for (int i = 0; i < Props.Length; i++) 
      { 
       values[i] = Props[i].GetValue(item, null); 
      } 
      dataTable.Rows.Add(values); 
     } 

     dataTable.AcceptChanges(); 

     return dataTable; 
    } 

這適用於所有的我的DataGridViewTextBox列。現在,我想將一個名爲「Person」的新DataGridViewComboBoxColumn添加到從Person對象列表填充ValueMember =「PersonId」和DisplayMember =「PersonName」的數據網格中。

我在這裏遇到了解如何將這種類型的列添加到數據表的數據網格中。

我的Person對象只是具有PersonId和PersonName屬性,我想用它作爲組合框的ValueMember和DisplayMember。

我卡住了,但我的想法是:1。 )更新SomeObjectModel包含人, 2)更新ToDataTable方法包含如果趕上條款時,該項目名稱爲「人」,但我米不知道該怎麼做的行。此外,它只是尋找屬性名稱,因爲我想保持ToDataTable乾淨,感覺很難受。

public static DataTable ToDataTable<T>(List<T> items) 
    { 
     DataTable dataTable = new DataTable(typeof(T).Name); 
     PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); 

     foreach (PropertyInfo prop in Props) 
     { 
      if (prop.Name = "Person") { // Create DataGridViewComboBoxColumn } 

      else 
      { 
       dataTable.Columns.Add(prop.Name); 
      } 

     } 

     foreach (T item in items) 
     { 
      var values = new object[Props.Length]; 
      for (int i = 0; i < Props.Length; i++) 
      { 
       values[i] = Props[i].GetValue(item, null); 
      } 

      dataTable.Rows.Add(values); 
     } 

     dataTable.AcceptChanges(); 

     return dataTable; 
    } 
+0

爲什麼要將'DataGridView'綁定到您使用'List '創建的'DataTable'? –

+0

另外'ToDataTable'方法在添加列時需要修復,您應該以這種方式添加列:'dataTable.Columns.Add(prop.Name,prop.PropertyType);'。如果你不指定列的類型,它將是字符串類型。 –

+0

我正在使用DataTable,因爲我想使用DataTable RowFilter從文本框中進行基本過濾。 –

回答

0

下面是我爲過去做過的一個例子...簡單的表單,下拉列表中加載了「個人」列表。按鈕,根據您有任何標準進行過濾...希望它有幫助。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     private class Person 
     { 
      public int PersonId { get; set; } 
      public string Name { get; set; } 
     } 

     private class Course 
     { 
      public int CourseId { get; set; } 
      public string CourseName { get; set; } 
      public int ProfessorId { get; set; } 
     } 

     private List<Course> courses = new List<Course>(); 
     private List<Person> professors = new List<Person>(); 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      courses.Add(new Course { CourseId = 1, CourseName = "Math", ProfessorId = 1 }); 
      courses.Add(new Course { CourseId = 2, CourseName = "English", ProfessorId = 2 }); 
      courses.Add(new Course { CourseId = 3, CourseName = "History", ProfessorId = 1 }); 

      professors.Add(new Person { PersonId = 1, Name = "John Doe" }); 
      professors.Add(new Person { PersonId = 2, Name = "Jane Doe" }); 

      cboProfessors.DisplayMember = "Name"; 
      cboProfessors.ValueMember = "PersonId"; 
      cboProfessors.DataSource = professors; 

      grdCourses.AutoGenerateColumns = false; 
      grdCourses.Columns.Add(new DataGridViewTextBoxColumn { Name = "CourseId", HeaderText = "Course ID #", DataPropertyName="CourseId" }); 
      grdCourses.Columns.Add(new DataGridViewTextBoxColumn { Name = "Name", HeaderText = "Course Name", DataPropertyName="CourseName" }); 
      grdCourses.Columns.Add(new DataGridViewComboBoxColumn { Name = "Professor", HeaderText = "Professor", DataSource = professors, 
       DisplayMember = "Name", ValueMember = "PersonId", DataPropertyName="ProfessorId" }); 

      grdCourses.DataSource = courses; 
     } 

     private void btnFilter_Click(object sender, EventArgs e) 
     { 
      int professorId = (int)cboProfessors.SelectedValue; 
      List<Course> filteredCourses = courses.Where(x => x.ProfessorId == professorId).ToList(); 
      grdCourses.DataSource = filteredCourses; 
     } 
    } 
} 
相關問題