Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > how can i set an image as a backgrouond to RowHeadersDefaultCellStyle?
 

how can i set an image as a backgrouond to RowHeadersDefaultCellStyle?

i wanted to know is there any way to set an image to datagrid view header instead of a colour?
i want to set an image on default rows header background please help me in this regard.
thanks
Muhammad Wasim  Saturday, August 01, 2009 10:08 AM
Hi Muhammad Wasim,

Based on my experience, the way to achieve this is handle the CellPainting event. Here is an example.
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex < 0 && e.RowIndex >= 0)
{
Image img = Image.FromFile(@"E:\G.jpg");
ImageList imgLst = new ImageList();
imgLst.Images.Add(img);
e.Graphics.DrawImage(imgLst.Images[0], e.CellBounds.Location);
e.Handled = true;
}
}
The example will draw an image to the row header. You can resize the image in the ImageList. Use e.Graphics.DrawImage to draw the image.

Wish this can help you.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Tuesday, August 04, 2009 2:52 AM
please any one can help me in this regard. if i am wrong just guide me how to make the rows headers attractive? is there any way to set multi-colors instead a single color??
Muhammad Wasim  Monday, August 03, 2009 5:56 AM
Hi Muhammad Wasim,

Based on my experience, the way to achieve this is handle the CellPainting event. Here is an example.
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex < 0 && e.RowIndex >= 0)
{
Image img = Image.FromFile(@"E:\G.jpg");
ImageList imgLst = new ImageList();
imgLst.Images.Add(img);
e.Graphics.DrawImage(imgLst.Images[0], e.CellBounds.Location);
e.Handled = true;
}
}
The example will draw an image to the row header. You can resize the image in the ImageList. Use e.Graphics.DrawImage to draw the image.

Wish this can help you.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Tuesday, August 04, 2009 2:52 AM

You can use google to search for other answers

Custom Search

More Threads

• Copy & Paste functions in datagrid?
• Populating Form Dynamically Via DataColumn Name
• Displaying Names rather than IDs in a ComboBox
• DataGridView column order resetting
• Creating a DataGrid where one record is painted on multiple raows
• DataGridView and ContextMenu's
• DataGridView Decimal validation on column keypress
• Find number from string
• Data Problem / Committal
• Speeding up populating a ListView