C# 用组合框替换 DataGridView 中的默认文本框 [英] C# Replace default textbox in DataGridView with a Combobox

查看:16
本文介绍了C# 用组合框替换 DataGridView 中的默认文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 C# 还是很陌生,但我正在开发一个从 Access 数据库中提取数据的 WinForms 应用程序.我目前正在开发一个表单,该表单使用 DataAdapter 和 DataSet 将表中的内容动态加载到 DataGridView 中.

I’m still pretty new to C# but I’m working on a WinForms application that pulls data from an Access database. I’m currently working on a form that loads the contents from a table into a DataGridView dynamically using a DataAdapter and DataSet.

到目前为止,表单按预期工作.目前,要添加新记录,您必须在一行中的每个单元格中键入数据.我想要做的是用组合框替换几列(文本框).换句话说,不是手动输入数据,而是有一个下拉组合框并从列表中选择条目.我还想从 SQL 生成组合框数据成员,但如果太多,我可以手动对每个项目进行编码.

So far, the form is working as expected. Currently, to add a new record, you have to type data into each cell in a row. What I want to do is replace a couple columns (textbox) with a combobox. In other words, instead of manually entering the data, have a dropdown combobox and select the entry from a list. I also want to generate the combobox data members from SQL but if that’s too much, I can just manually code each item.

我可以在运行时向 datagridview 添加一个组合框,但这不是我想要做的.由于列是在运行时通过代码创建的,我不确定如何按照该方法修改列.

I can add a combobox to the datagridview at run time but that’s not what I’m trying to do. And since the columns are being created at run time via code, I’m not sure how to modify a column following that method.

因此,例如,我想用R"、PG-13"、PG"等组合框成员替换Rating"的文本框单元格.

So, for example, I want to replace the textbox cells for "Rating" with a combobox members such as "R", "PG-13", "PG", etc.

public partial class frmBulkInsert : Form
{
    OleDbConnection conn;
    OleDbDataAdapter da;
    DataSet ds;
    OleDbCommandBuilder cmdbl;


    public frmBulkInsert()
    {
        InitializeComponent();
    }

    private void frmBulkInsert_Load_1(object sender, EventArgs e)
    {
        // Load all records from the table into the datagridview.
        try
        {
            dataConnectionSettings();              
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error
" + ex.Message, "Error", MessageBoxButtons.OK,   MessageBoxIcon.Error);
        }
    }


    private void button1_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void ConnectToDatabase()
    {
        conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data  Source=C:Users
onniejones.JONESDocumentsAccess ProjectsMovie_2013.mdb";
    }

    private void btnUpdateRecords_Click(object sender, EventArgs e)
    {
        try
        {
            //DataSet changes = (ds).GetChanges();

            cmdbl = new OleDbCommandBuilder(da);
            //da.Update(ds, "Movie_2013");
            int numRows = da.Update(ds, "Movie_2013");
            MessageBox.Show(numRows + " Record(s) Updated", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,  MessageBoxIcon.Error);
        }
    }

private void dataConnectionSettings()
    {
        //DataGridViewComboBoxColumn cboColumn;
        conn = new OleDbConnection();
        ConnectToDatabase();
        conn.Open();
        da = new OleDbDataAdapter("SELECT ID, Title, [Minutes], Rating, Category,     Format, Actor1, Actor2, Actor3, Actor4, [Status] FROM [Movies] ORDER BY ID", conn);
        ds = new DataSet();
        da.Fill(ds, "Movie_2013");
        dataGridView1.DataSource = ds.Tables[0];

推荐答案

一列创建后,您将无法更改其类型.您可以通过两种方式实现目标.

Once a column is created you can't change its type. You can achieve your goal in two ways.

  1. 正如您已经提到的,创建所需的列,将其添加到网格并绑定数据.如果您设置列的 DataPropertyName 属性,那么当您的数据绑定时,该数据列将绑定到您的网格列.DataGridView 将自动生成其他列.将第二个数据源绑定到组合框列本身.

  1. As you have already mentioned, create the required columns, add it to the grid and bind the data. If you set the DataPropertyName property of your column then when your data is bound that data column will get bound to your grid column. The DataGridView will generate other columns automatically. Bind a second datasource to the comboboxcolumn itself.

修改您的数据库,使列表由数据库提供,绑定将自动创建组合框列.

Modify your database such that the list is provided by the database and binding will automatically create comboboxcolumn.

这篇关于C# 用组合框替换 DataGridView 中的默认文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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