Reputation: 255
Basically I got a datagridview in which I want to insert images in the first column and text in the rest. Note that the datagridview is bound to a source which is where I insert the rows.
Before the image requirement I used to just add the row as an array of strings as below:
string[] row = **value**;
myDataTable.Rows.Add(row);
Which all worked good. But now I don't know how to add the image (I've added an image column to the datatable). Obviously I cannot insert a bitmap image in an array of strings. Nor can I insert the image and array of strings as below :
myDataTable.Rows.Add(image, textArray);
because it would result in inserting the whole array in the second column and not insert the specific items in each of the columns.
How can I do this?
Upvotes: 1
Views: 1014
Reputation: 691
we can bind any type of data to the DataGridViewImageColumn. We only need to handle the CellFormatting event to convert the data into an image. For example, we bind some image paths to the column, we can follow the code below to show the images:
private void dataGridView1_CellFormatting(object sender,DataGridViewCellFormattingEventArgs e)
{
if (this.dataGridView1.Columns[e.ColumnIndex] is DataGridViewImageColumn)
{
string imagePath = (e.Value ?? "").ToString().Trim();
if (imagePath != "")
e.Value = Image.FromFile(imagePath);
}
}
Upvotes: 0
Reputation: 103575
You can pass an array of objects instead of an array of strings:
object[] row = new object[] { image, "string 1", "string 2" };
myDataTable.Rows.Add(row);
Upvotes: 2