2017-08-04 75 views
0

我想在可編輯的DataGridViewPatientType列中使用ComboBox。 所以我假設創建DataSet並添加基本表「患者」和「PatientTypes」表。DataGridView的數據集和組合框

我是否需要在DataSet中做出更多更改才能實現我的目標?

DataTable table1 = new DataTable("Patients"); 
table1.Columns.Add("Id"); 
table1.Columns.Add("Name"); 
table1.Columns.Add("PatientTypeId"); 
table1.Rows.Add("sam", 1, 1); 
table1.Rows.Add("mark", 2, 2); 

DataTable table2 = new DataTable("PatientTypes"); 
table2.Columns.Add("id"); 
table2.Columns.Add("Name"); 
table2.Rows.Add(1, "rich"); 
table2.Rows.Add(2, "poor"); 

// Create a DataSet and put both tables in it. 
DataSet set = new DataSet("Office"); 
set.Tables.Add(table1); 
set.Tables.Add(table2); 
+0

這取決於你將如何使用它。您需要使用PatientsTypes表作爲ComboboxColumn的DataSOurce。其實你可以做到這一點,即使沒有DataTable – Fabio

+0

@Fabio聽起來不錯!你想介紹一些例子嗎?謝謝! –

+1

[DataGridView中的外鍵的Combobox]的可能重複(https://stackoverflow.com/questions/2256924/combobox-for-foreign-key-in-datagridview) – Fabio

回答

0

我創造了一些示例應用來了解它如何工作。 以下是WinForm應用程序的完整代碼。

namespace DataGridAndComboBox 
{ 
    public partial class Form1 : Form 
    { 
     List<Users> users = new List<Users>(); 

     List<Months> months = new List<Months>(); 

     public Form1() 
     { 
      InitializeComponent(); 

      dataGridView1.CellValueChanged += DataGridView1_CellValueChanged; 
      Load += Form1_Load; 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      months.Add(new Months() { ID = 1, Name = "Jan" }); 
      months.Add(new Months() { ID = 2, Name = "Feb" }); 
      months.Add(new Months() { ID = 3, Name = "Mar" }); 


      users.Add(new Users() { ID = 1, MonthID = 1, Name = "Trump" }); 
      users.Add(new Users() { ID = 2, MonthID = 2, Name = "Clinton" }); 
      users.Add(new Users() { ID = 3, MonthID = 3, Name = "Obama" }); 
      users.Add(new Users() { ID = 4, MonthID = 1, Name = "Reygan" }); 
      users.Add(new Users() { ID = 5, MonthID = 2, Name = "Kennedi" }); 
      users.Add(new Users() { ID = 6, MonthID = 3, Name = "Bush" }); 

     } 

     //Get data from database 
     private void ButtonGetData_Click(object sender, EventArgs e) 
     { 
      var bindingSourceMonths = new BindingSource() 
      { 
       DataSource = months 
      }; 

      #region Create DataGridView columns 

      dataGridView1.Columns.Clear(); 

      var userID = new DataGridViewTextBoxColumn() 
      { 
       HeaderText = "ID user", 
       Width = 50, 
       DataPropertyName = "ID", 
       Name = "ID" 
      }; 
      dataGridView1.Columns.Add(userID); 

      var userName = new DataGridViewTextBoxColumn() 
      { 
       HeaderText = "User Name", 
       Width = 100, 
       DataPropertyName = "Name", 
       Name = "Name" 
      }; 
      dataGridView1.Columns.Add(userName); 


      var userMonthID = new DataGridViewComboBoxColumn() 
      { 
       HeaderText = "Month", 
       Width = 100, 
       DataPropertyName = "MonthID", 
       DataSource = bindingSourceMonths, 
       ValueMember = "ID", 
       DisplayMember = "Name", 
       Name = "MonthID" 
      }; 
      dataGridView1.Columns.Add(userMonthID); 

      #endregion 

      dataGridView1.DataSource = users; 
     } 

     //Update database 
     private void ButtonUpdateData_Click(object sender, EventArgs e) 
     { 
      for (int i = 0; i < dataGridView1.Rows.Count; i++) 
      { 
       var userID = (int)dataGridView1.Rows[i].Cells[0].Value; 
       var userName = (string)dataGridView1.Rows[i].Cells[1].Value; 
       var userMonthID = (int)dataGridView1.Rows[i].Cells[2].Value; 
       var m = months.First(x => x.ID == userMonthID); 
      } 
     } 

     private void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) 
     { 
      if (dataGridView1.Columns[e.ColumnIndex].Name == "MonthID") 
      { 
       Users users = (Users)this.dataGridView1.CurrentRow.DataBoundItem; 

       var monthName = months.First(x => x.ID == users.MonthID); 
      } 
     } 
    } 


    #region Classes 

    public class Months 
    { 
     public int ID { get; set; } 

     public string Name { get; set; } 
    } 

    public class Users 
    { 
     public int ID { get; set; } 

     public string Name { get; set; } 

     public int MonthID { get; set; } 
    } 

    #endregion 
} 
1

不,你並不需要做出的數據集的更多變化,但你需要設置你的DataGridView有,有一個組合框柱:

  • 的.DataPropertyName屬性設置爲你想組合框編輯,可能是「PatientTypeId」
  • 的.DataSource屬性設置爲您PatientType數據表哪一列患者
  • 的.DisplayMember屬性設置爲你想要的組合顯示什麼,可能是「名」
  • 的.ValueMember屬性設置爲你想要的組合框作爲PatientTypeId值幹什麼用的,可能是「ID」

我張貼即使法比奧已經連接到一個良好的相關文章這個建議,因爲文章使用類型數據集和datagridview的可視化設計,所以可能很難它給的建議適應您的情況

1

你需要創建一個DataGridViewComboBoxColumn並設置其DataSourceHeaderTextNameDataPropertyNameValueMemberDisplayMember性能

,然後添加到您的datagridview的Columns

下面是代碼(與您的樣本數據進行測試)

var comboBox = new DataGridViewComboBoxColumn(); 
    comboBox.DataSource = dataset.Tables["PatientTypes"]; 
    comboBox.HeaderText = "PatientTypeId"; 
    comboBox.Name = "PatientTypeId"; 
    comboBox.DataPropertyName = "PatientTypeId"; //important 
    comboBox.ValueMember = "id"; 
    comboBox.DisplayMember = "Name"; 

dataGridView1.Columns.Add(comboBox); 
dataGridView1.DataSource = dataset.Tables["Patients"]; 
// set the column poition in the grid same as table 
dataGridView1.Columns["PatientTypeId"].DisplayIndex = dataset.Tables["Patients"].Columns["PatientTypeId"].Ordinal;