【发布时间】: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