Reputation: 17701
I have data grid view with columns product name and product image and I am populating these values coming from database...
i am using winforms desktop application.....
my problem is I am not able to showing the image in datagridview cell properly ..see the diagram below
i want to display this image in actual product image column for every cell in that column
this task is very simple in webforms by using datalist control but i dont know how to display full image in grid view cell
can any one help on this....
many thanks.......
and this is where i am binding the datagridview by using linq query..
private void EquipmentFinder_Load(object sender, EventArgs e)
{
var products = from prods in abc.products
select new
{
productid = prods.product_Id, //0
productname = prods.product_Name, //1
productimage = prods.product_Image, //2
productprice = prods.product_Price,//3
productdescr = prods.product_Description, //4
};
productbindingsource.DataSource = products;
productgridview.DataSource = productbindingsource;
productgridview.Columns[0].Visible = false;
productgridview.Columns[3].Visible = false;
productgridview.Columns[4].Visible = false;
}
Upvotes: 5
Views: 18063
Reputation: 1
first right click on grid go to properties of grid in that you will get column (collection) properties click on that. click on image column in that go in appearance-image layout-and choose stretch property you can also increase size of your image column. its really helpful in that case .you have do not need for such type code.
Upvotes: 0
Reputation: 2179
Set DataGridViewImageColumn.ImageLayout property to DataGridViewImageCellLayout.Zoom
If you set to "Stretch" your image will be inproportionally scaled to fit whole cell. And that is probably not what you want.
Set to zoom to have The graphic is uniformly enlarged until it fills the width or height of the containing cell.
By default the value is set to "Normal": The graphic is displayed centered using its native resolution.
Upvotes: 0
Reputation: 5340
Set the column's ImageLayout to the Stretch value to resolve this problem.
UPDATE: use the following code to change the ImageLayout property:
for(int i = 0; i < dataGridView1.Columns.Count; i ++)
if(dataGridView1.Columns[i] is DataGridViewImageColumn) {
((DataGridViewImageColumn)dataGridView1.Columns[i]).ImageLayout = DataGridViewImageCellLayout.Stretch;
break;
}
Upvotes: 12