代码之家  ›  专栏  ›  技术社区  ›  NoWar

DataGridView的数据集和组合框

  •  0
  • NoWar  · 技术社区  · 7 年前

    我想使用 ComboBox DataGridView . DataSet 并添加基本表“Patients”和“PatientTypes”表。

    我需要在 数据集

    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);  
    
    3 回复  |  直到 7 年前
        1
  •  1
  •   Caius Jard    7 年前

    不需要,您不需要对数据集进行更多更改,但需要将DataGridView设置为具有以下组合框列:

    • DataPropertyName属性设置为您希望组合框编辑的患者列,可能是“PatientTypeId”
    • 设置为PatientType数据表的.DataSource属性
    • .ValueMember属性设置为您希望组合框用作PatientTypeId的值,可能是“id”

        2
  •  1
  •   ClearLogic    7 年前

    你需要创建一个 DataGridViewComboBoxColumn 并设置其 DataSource , HeaderText Name , DataPropertyName , ValueMember , DisplayMember

    然后将其添加到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;
    
        3
  •  0
  •   NoWar    7 年前

    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
    }