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