I have two comboBoxes. In first I select Brand, in the second I select the model. I want also to display a picture according to model choice. Everything seemed to work until I added that FileInfo column. Now the code is not working (I cant even choose the brand and model – comboboxes are empty). What is wrong:
dtCategories = new DataTable();
dtCategories.Columns.Add("CategoryID", typeof(int));
dtCategories.Columns.Add("CategoryName", typeof(string));
dtCategories.Rows.Add(0, "--Select--");
dtCategories.Rows.Add(1, "Audi");
dtCategories.Rows.Add(2, "BMW");
dtCategories.Rows.Add(3, "Mercedes");
dtItems = new DataTable();
dtItems.Columns.Add("ItemID", typeof(int));
dtItems.Columns.Add("ItemName", typeof(string));
dtItems.Columns.Add("CategoryID", typeof(int));
dtItems.Columns.Add("Obrazek", typeof(FileInfo));
dtItems.Rows.Add(2, "A1", 1, "a1.jpeg");
dtItems.Rows.Add(3, "A3", 1, "a3.jpeg");
dtItems.Rows.Add(4, "A4", 1, "a5.jpeg");
dtItems.Rows.Add(5, "Seria 1", 2, "bmw1.jpeg");
dtItems.Rows.Add(6, "Seria 2", 2, "bmw3.jpeg");
dtItems.Rows.Add(7, "Seria 3", 2, "bmw5.jpeg");
dtItems.Rows.Add(8, "C Klasa", 3, "C Klasa.jpeg");
dtItems.Rows.Add(9, "E Klasa", 3, "E Klasa.jpeg");
dtItems.Rows.Add(10, "S Klasa", 3, "S Klasa.jpeg");
cbCategory.DisplayMember = "CategoryName";
cbCategory.ValueMember = "CategoryID";
cbCategory.DataSource = dtCategories;
And :
private void cbCategory_SelectedIndexChanged(object sender, EventArgs e)
{
int categoryId = Convert.ToInt32(cbCategory.SelectedValue);
if (categoryId > 0)
{
DataTable dtTemp = dtItems.Select(string.Format("CategoryID = {0}", categoryId)).CopyToDataTable();
DataRow drTemp = dtTemp.NewRow();
drTemp["ItemID"] = 0;
drTemp["ItemName"] = "--Select--";
drTemp["CategoryID"] = 0;
dtTemp.Rows.InsertAt(drTemp, 0);
cbItems.DisplayMember = "ItemName";
cbItems.ValueMember = "ItemID";
cbItems.DataSource = dtTemp;
pictureBox1.Image = Image.FromFile(dtTemp.Columns[3].ToString());
}
else if (cbItems.DataSource != null)
{
cbItems.SelectedIndex = 0;
}
}
FileInfois not a valid data type for a column of aDataTable. See DataColumn.DataType Property for more information.Do you receive an exception of some kind?
It might be your image paths are not correct.
You could try this code, which might be easier to debug: