如果我將DataGridView(dgv)綁定到適配器,則dgv會自動填充表中的內容。不過,現在以文本形式呈現的其中一列,我想將其更改爲ComboBox。帶適配器和組合框的DataGridView
此代碼準備數據適配器:
MySqlDataAdapter awardsAdapter, spendingsAdapter;
DataSet cachedData = new DataSet();
MySqlDataAdapter PrepareSpendingsAdapter() {
try {
MySqlDataAdapter adapter;
adapter = new MySqlDataAdapter("select * from spendings", connection);
adapter.TableMappings.Add("Table", "spendings");
adapter.UpdateCommand = new MySqlCommand(
"UPDATE spendings SET [email protected], [email protected] WHERE [email protected];",
connection);
adapter.UpdateCommand.Parameters.Add("@id", MySqlDbType.Int32, 10, "ID");
adapter.UpdateCommand.Parameters.Add("@category", MySqlDbType.Int32, 10, "Category");
adapter.UpdateCommand.Parameters.Add("@points", MySqlDbType.Int32, 10, "Points");
adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.None;
adapter.InsertCommand = new MySqlCommand(
"INSERT INTO spendings VALUES (@id,@category,@points);",
connection);
adapter.InsertCommand.Parameters.Add("@id", MySqlDbType.Int32, 10, "ID");
adapter.InsertCommand.Parameters.Add("@category", MySqlDbType.Int32, 10, "Category");
adapter.InsertCommand.Parameters.Add("@points", MySqlDbType.Int32, 10, "Points");
adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None;
adapter.DeleteCommand = new MySqlCommand(
"DELETE FROM spendings WHERE [email protected];", connection);
adapter.DeleteCommand.Parameters.Add("@id", MySqlDbType.Int32, 10, "ID");
adapter.DeleteCommand.UpdatedRowSource = UpdateRowSource.None;
return adapter;
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
return null;
}
該代碼將與我需要
DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
MySqlDataAdapter adapterSpendCategory = new MySqlDataAdapter("select category,details from spendcategory", connection);
adapterSpendCategory.TableMappings.Add("Table", "spendcategory");
adapterSpendCategory.Fill(cachedData);
cmb.DataSource = cachedData.Tables["spendcategory"];
cmb.DisplayMember = "details";
cmb.ValueMember = "category";
//Put the missing piece of the puzzle here
此代碼填充DataGridView中的信息的額外的列:
spendingsAdapter = PrepareSpendingsAdapter();
spendingsAdapter.Fill(cachedData);
dgvPointsSpent.DataSource = cachedData.Tables["spendings"];
相反添加這個額外的列我希望類別列改變,看起來像額外的公司lumn
我不使用設計師。是否有可能在mySQL中使用設計器? –