C# 在DataGridView中添加组合框列

C# 在DataGridView中添加组合框列,c#,winforms,datagridview,C#,Winforms,Datagridview,我正在使用WinFormDataGridView控件在MS Access数据库中添加、编辑和删除记录。当没有pk-fk关系时,它非常有效。现在我想在UserDetails表中添加一个UserTypeId列。UserTypeId来自UserTypes表 Table: UserDetails -------------------------------------- UserId | UserTypeId | Other fields...| ---------------------------

我正在使用WinFormDataGridView控件在MS Access数据库中添加、编辑和删除记录。当没有pk-fk关系时,它非常有效。现在我想在UserDetails表中添加一个UserTypeId列。UserTypeId来自UserTypes表

Table: UserDetails
--------------------------------------
UserId | UserTypeId | Other fields...|
--------------------------------------
       |            |                |
--------------------------------------

Table: UserTypes
---------------------------
UserTypeId | UserTypeName |
---------------------------
           |              |
---------------------------
这些是我现有的代码-

public partial class frmUser : Form
{
    private String connectionString = null;
    private OleDbConnection connection = null;
    private OleDbDataAdapter da = null;
    private OleDbCommandBuilder commandBuilder = null;
    private DataTable dataTable = null;
    private BindingSource bindingSource = null;
    private String selectQueryString = null;

    public frmUser()
    {
        InitializeComponent();
    }

    private void frmUser_Load(object sender, EventArgs e)
    {
        connectionString = GlobalVariables.ConnectionString;// ConfigurationManager.AppSettings["connectionString"];
        connection = new OleDbConnection(connectionString);
        selectQueryString = "SELECT * FROM UserDetail";

        connection.Open();

        da = new OleDbDataAdapter(selectQueryString, connection);
        commandBuilder = new OleDbCommandBuilder(da);

        dataTable = new DataTable();
        da.Fill(dataTable);
        bindingSource = new BindingSource();
        bindingSource.DataSource = dataTable;

        dataGridViewTrial.DataSource = bindingSource;

        // if you want to hide Identity column
        dataGridViewTrial.Columns[0].Visible = false;
    }

    private void addUpadateButton_Click(object sender, EventArgs e)
    {
        try
        {
            da.Update(dataTable);
        }
        catch (Exception exceptionObj)
        {
            MessageBox.Show(exceptionObj.Message.ToString());
        }
    }

    private void deleteButton_Click(object sender, EventArgs e)
    {
        try
        {
            dataGridViewTrial.Rows.RemoveAt(dataGridViewTrial.CurrentRow.Index);
            da.Update(dataTable);
        }
        catch (Exception exceptionObj)
        {
            MessageBox.Show(exceptionObj.Message.ToString());
        }
    }
}
如何使用上述代码添加查找列?
谢谢。

您需要2个
BindingSource
s,其中一个
BindingSource
将连接到您的
UserDetails
表(如您所做),而其他人将连接到
UserTypes

然后,您需要使用
DataGridView
datagridviewcomboxcolumn
附加第二个
BindingSource

dataGridViewTrial.DataSource = bindingSource;
// after you are binding your DataGridridView

// assuming that the UserTypeId Column is at 1st index
var colUserTypes = this.dataGridViewTrial.Columns[1];
// by default columns are added as Text columns
// so we are removing the auto added column
this.dataGridViewTrial.Columns.Remove(colUserTypes);

// creating new combobox Column
var cmbColumn = new DataGridViewComboBoxColumn();
cmbColumn.DataPropertyName = "UserTypeId";      // this is the property in UserDetails table
cmbColumn.ValueMember = "UserTypeId";           // this is the property in UserTypes table
cmbColumn.DisplayMember = "UserTypeName";       // again this property is in UserTypes table
cmbColumn.DataSource = userTypesBindingSource;  // this binding source is connected with UserTypes table
this.dataGridViewTrial.Columns.Add(cmbColumn);

通常,使用设计模式可以更容易地完成这件事。你可以在谷歌上搜索,我想有很多aricles可供选择。

如何在datagridview中生成列?如果列是动态生成的,则必须在网格中进行更改,首先手动创建所有列,并在每个DataGridView列的
DataPropertyName
中指定数据库字段名。现在,将列类型
datagridviewcomboxcolumn
分配给
UserTypeId

完成上述所有过程后,您需要在绑定
DataGridView
之前填充该列

string _SQL = "Select UserTypeId,UserTypeName From UserTypes Order By UserTypeName";
//Dont include Order By UserTypeName if you have created clustered index on it.

SqlDataAdapter Da = New SqlDataAdapter(_SQL, Connection);
DataTable Dt = New DataTable();
Da.Fill(Dt);

DataGridViewComboBoxColumn colUserTypeId = (DataGridViewComboBoxColumn)DataGridView1.Columns["UserTypeId"];

colUserTypeId.DisplayMember = "UserTypeName";
colUserTypeId.ValueMember = "UserTypeId";

colUserTypeId.DataSource = Dt;

//CODE TO FILL GRIDVIEW
现在,网格将在
UserTypeID
列中填充用户类型。应用程序将从
DataGridView1.Rows[RowIndex].Cells[“UserTypeID”]返回
UserTypeID
。Value
属性