2
votes

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

2 Answers

2
votes

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

  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.

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

1
votes

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