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

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

问题描述

我还是C#的新手,但我正在使用一个WinForms应用程序从Access数据库中提取数据。我目前正在使用一个表格,使用DataAdapter和DataSet动态地将表中的内容加载到DataGridView。



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



我可以添加一个组合框到datagridview在运行时间,但这不是我想做的。由于列是在运行时通过代码创建的,我不知道如何修改该方法后的列。



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

  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)
{
//将表中的所有记录加载到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;数据源= C:\ Users \ronniejones.JONES\Documents\Access Projects\Movie_2013.mdb;
}

private void btnUpdateRecords_Click(object sender,EventArgs e)
{
try
{
// DataSet changes = .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];


解决方案

您可能想这样做: p>

一旦您将数据源分配给您的gridview,这是来自查询的表:

  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] 是您的网格的[列,行],因此您可以计算您的数据库有多少行,并且 dt.Rows.Count + 1 [column,Row]



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


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.

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.

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.

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\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];

解决方案

You may want to do something like this:

Once you have assigned the datasource to your gridview which is your table coming from the query:

   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;

where this.dataGridView1[int, int] is the [column, row] of your grid, so you can count how many rows does your datatable has and dt.Rows.Count + 1 will be the Row value in the [column, Row]

Hope this is what you were looking for and this helps

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

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