我有一个非常复杂的情况,我希望我能描绘出来。
所以,我有一个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;
问题是,&#34;食物&#34; col是一个int类型,因为我将数据库中的值存储为int(我有另一个表,它定义了什么数字意味着什么食物)。
如果我尝试像这样填充datagridview:
[...]
reader = commandDatabase.ExecuteReader();
while (reader.Read())
{
dt.Rows.Add(new object[] { reader.GetString(0), reader.GetInt32(1) });
}
DataGridViewTextBoxColumn无法设置正确的值。 所以我需要的是,如果&#34; reader.GetInt32(1)&#34;对于前1,我想在组合框中设置&#34; food2&#34;等。
提前致谢!