Zaurb
asked on
How to set a column in a datagridview as a picture column
Hi!
I need to make one of the columns of my datagridview a picture column.
I tried in several ways but fail.
The column which I need to display as pictures contains the path to image. When I display a column normally i can see the path to where the image file is located. however, I would like it to show an image instead of the path to an image. How to do this?
Here's my code:
I need to make one of the columns of my datagridview a picture column.
I tried in several ways but fail.
The column which I need to display as pictures contains the path to image. When I display a column normally i can see the path to where the image file is located. however, I would like it to show an image instead of the path to an image. How to do this?
Here's my code:
private void buttonExecuteSearch_MA_Items_Click(object sender, EventArgs e)
{
string connectionString = "my String";
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
string sql = "SELECT * FROM MA_Items WHERE 1=1 ";
// Create new DataAdapter
using (SqlDataAdapter a = new SqlDataAdapter(sql, conn))
{
DataTable t = new DataTable();
a.Fill(t);
dataGridView1.DataSource = t;
dataGridView1.AutoResizeColumns();
dataGridView1.CellFormatting +=
new System.Windows.Forms.DataGridViewCellFormattingEventHandler(
this.dataGridView1_CellFormatting);
}
}
private void dataGridView1_CellFormatting(object sender,
System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Picture"))
{
DataGridViewImageColumn ColumnImage = new DataGridViewImageColumn();
ColumnImage.Name = "Picture";
ColumnImage.HeaderText = "Picture";
ColumnImage.ImageLayout = DataGridViewImageCellLayout.Zoom;
ColumnImage.Width = 40;
dataGridView1.Columns.Add(ColumnImage);
int i = 0;
foreach (DataRow row in ds.Tables["MA_Items"].Rows)
{
DataGridViewImageCell ImgCell = new DataGridViewImageCell();
ImgCell = (DataGridViewImageCell)dataGridView1.Rows[i].Cells["Picture"];
ImgCell.Value = Image.FromFile(e.Value.ToString());
i += 1;
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Many Thanks!
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewimagecolumn(v=VS.80).aspx
Basically, you read images (from file etc), and set them in a grid. Using DataGridViewImageColumn class.