【问题标题】:C# Replace default textbox in DataGridView with a ComboboxC#用组合框替换DataGridView中的默认文本框
【发布时间】:2014-02-14 22:19:45
【问题描述】:

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

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

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

因此,例如,我想将“Rating”的文本框单元格替换为“R”、“PG-13”、“PG”等组合框成员。

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\n" + 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\ronniejones.JONES\Documents\Access Projects\Movie_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];

【问题讨论】:

    标签: c# winforms datagridview combobox


    【解决方案1】:

    你可能想做这样的事情:

    一旦您将数据源分配给您的网格视图,即来自查询的表格:

       DataGridViewComboBoxCell ComboBoxCell1 = new DataGridViewComboBoxCell();
       ComboBoxCell1.Items.AddRange(new string[] { "aaa", "bbb", "ccc" });
       this.dataGridView1[0, 2] = ComboBoxCell1;
    
       DataGridViewComboBoxCell ComboBoxCell2 = new DataGridViewComboBoxCell();
       ComboBoxCell2.Items.AddRange(new string[] { "aaa", "bbb", "ccc" });
       this.dataGridView1[1, 2] = ComboBoxCell2;
    
       DataGridViewComboBoxCell ComboBoxCell3 = new DataGridViewComboBoxCell();
       ComboBoxCell3.Items.AddRange(new string[] { "aaa", "bbb", "ccc" });
       this.dataGridView1[2, 2] = ComboBoxCell3;
    

    this.dataGridView1[int, int] 是网格的 [column, row],因此您可以计算数据表有多少行,dt.Rows.Count + 1 将是 [column, Row] 中的 Row 值

    希望这是您正在寻找的,这对您有帮助

    【讨论】:

    • 首先让我道歉。我是 StackOverflow 的新手,我想我会收到一封电子邮件,通知我有回复。对不起!
    • 所以 - 使用这种方法需要我列出每一行(1247 行)并添加单元格。我肯定需要使用 DataGridViewComboboxColumn 方法来让它完成我的意图。不幸的是,这种方法对我不起作用。但感谢您的意见和建议。
    【解决方案2】:

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

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

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

    【讨论】:

    • 好的,因为我正在尝试使用代码动态地执行此操作,所以我已经尝试了您的第 2 步,我已经接近但不是我想要的。
    • 我按照第一个建议做了,效果很好!添加了新创建的 DataGridViewComboBoxColumn,将它们移动到预期位置并隐藏文本框列。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-10
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多