我有一个非常复杂的情况,我希望我可以描绘。
所以,我有一个datagridview,它是这样的:
DataTable dt = new DataTable();
dataGridViewFood.AutoGenerateColumns = false;
dt.Columns.Add("date", typeof(string));
dt.Columns.Add("food", typeof(int));
DataGridViewTextBoxColumn date = new DataGridViewTextBoxColumn();
date.HeaderText = "date";
date.DataPropertyName = "date";
DataGridViewComboBoxColumn name = new DataGridViewComboBoxColumn();
var list11 = new List<String>() { "food1", "food2", "food3" }; // here later I'll use a mysql select query to fill the list
name.DataSource = list11;
name.HeaderText = "food";
name.DataPropertyName = "food";
dataGridViewFood.DataSource = dt;
dataGridViewFood.Columns.AddRange(date, name);
dataGridViewFood.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
问题是,“food”列是int类型的,因为我将数据库中的值存储为int(我还有另一个表,它定义了什么数字代表什么食物)。
如果我尝试这样填充datagridview:
[...]
reader = commandDatabase.ExecuteReader();
while (reader.Read())
{
dt.Rows.Add(new object[] { reader.GetString(0), reader.GetInt32(1) });
}
datagridviewtextboxcolumn无法设置正确的值。所以我需要的是,如果“reader.getint32(1)”是ex1的,我想在组合框中设置为“food2”,等等。
提前谢谢!
暂无答案!
目前还没有任何答案,快来回答吧!