[英]c# set which item is selected in datagridview combobox cell
我有兩種形式,一種是用一些行填充DataGridView
,每行有兩個組合框。
我能夠從這些單元格中獲取值和格式化值,但是當我嘗試將所有這些數據復制到下一個不同形式的DataGridView
中時,我無法告訴他ComboBox
哪個項目應該被標記為選中.
當我環顧四周時,我發現了這些代碼行(不幸的是,它們來自 6 多年前)
dataGridView.Rows[index].Cells[3].Value = ImageFormat.Items[1];
(dataGridView.Rows[index].Cells[3] as DataGridViewComboBoxCell).Value = ImageFormat.Items[0];
DataGridViewComboBoxCell comboboxFormat = (DataGridViewComboBoxCell)(dataGridView.Rows[index].Cells[3]);
comboboxFormat.Value = ImageFormat.Items[0];
(dataGridView.Rows[index].Cells[3] as DataGridViewComboBoxCell).Value = (dataGridView.Rows[index].Cells[3] as DataGridViewComboBoxCell).Items[0];
不幸的是,這些都不起作用,而且大多數(如果不是全部)拋出“DataGridViewComboBoxCell 值無效”異常
也許值得一提的是,可能的項目是從數據庫中綁定的,如下所示:
string stm = "SELECT * FROM colours";
using var cmd = new SQLiteCommand(stm, MainWin.con);
SQLiteDataAdapter rdr = new SQLiteDataAdapter(cmd);
DataTable dataTableColour = new DataTable();
rdr.Fill(dataTableColour);
stm = "SELECT * FROM formats";
using var cmdd = new SQLiteCommand(stm, MainWin.con);
SQLiteDataAdapter reader = new SQLiteDataAdapter(cmdd);
DataTable dataTableFormat = new DataTable();
reader.Fill(dataTableFormat);
ImageFormat.ValueMember = "id";
ImageFormat.DisplayMember = "name";
ImageFormat.DataSource = dataTableFormat;
ColourImage.ValueMember = "id";
ColourImage.DisplayMember = "name";
ColourImage.DataSource = dataTableColour;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.