将组合框添加到datagridview C#Winforms [英] Add combobox to datagridview C# Winforms

查看:170
本文介绍了将组合框添加到datagridview C#Winforms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将combobox列添加到datagridview。
我使用这个代码绑定我的访问数据库的数据。

I want to add combobox column to datagridview. I use this code to bind data from my access database.

            Class1.Connection.Open();
            oleCommand = new OleDbCommand("SELECT tbAuditDetails.AuditNo, tbAuditQuestions.AutoSubcontent, tbAuditQuestions.AutoID, tbAuditQuestions.Questions, tbScore.Description, tbAuditDetails.QuestionID, tbAuditQuestions.QuestAutoID, tbAuditDetails.ScoreID, tbScore.Score, tbAuditQuestions.SubContentID, tbAuditDetails.ProfileID FROM ((tbAuditDetails INNER JOIN tbAuditQuestions ON tbAuditDetails.QuestionID = tbAuditQuestions.QuestionID) INNER JOIN tbScore ON tbAuditDetails.ScoreID = tbScore.ScoreID) WHERE (([tbAuditDetails.AuditNo] = " + Class1.detailsauditno + ") AND ([tbAuditQuestions.AutoSubcontent] = '" + newautosubcontentid + "') AND ([tbAuditDetails.ProfileID] = " + proid + ")) ORDER BY [tbAuditQuestions.QuestAutoID], [tbAuditDetails.QuestionID]", Class1.Connection);
            oleAdapter = new OleDbDataAdapter(oleCommand);
            oleBuilder = new OleDbCommandBuilder(oleAdapter);

            oleDs = new DataSet();
            oleAdapter.Fill(oleDs, "tbAuditDetails");
            oleTable = oleDs.Tables["tbAuditDetails"];

            Class1.Connection.Close();

            dataGridView1.DataSource = oleDs.Tables["tbAuditDetails"];

我想将一列变成combobox,所以我可以选择值。此组合框值来自另一个表,它称为tbscore(id,说明)。

I want change one column into combobox, so i can choose the value. This combobox value come from another table, it called "tbscore" (id, description).


tbScore.Description

tbScore.Description

有没有人知道如何实现?非常感谢。
谢谢。

Does anyone know how to achieve this? Really appreciated. Thank you.

推荐答案

private void AddComboboxColumn()
{
 DataGridViewComboBoxColumn ColComboBox = new DataGridViewComboBoxColumn();
 dataGridView1.Columns.Add(ColComboBox );
 ColComboBox.DataPropertyName = "ScoreID";
 ColComboBox.HeaderText = "Category";
 ColComboBox.ValueType = typeof(string);
 ColComboBox.DisplayStyle = DataGridViewComboBoxDisplayStyle.DropDownButton;
 ColComboBox.DisplayIndex = 2;
 ColComboBox.Width = 150;
 ColComboBox.DataSource = oleDs ;
 ColComboBox.DisplayMember = "description";
 ColComboBox.ValueMember = "ScoreID";
 ColComboBox.Name = "ScoreID";
 ColComboBox.DataPropertyName = "ScoreID";
}

您可以在代码之后调用此函数

You can call this function after your code

这篇关于将组合框添加到datagridview C#Winforms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆