Hi,
I have one sql table It contains Flightimages in image format
FlightImage FlightName
Binarydata Sunset
Binarydata Lotus
Binarydata Winter
Binarydata Blue Hills
I bind the above table into dataGridview. dataGridview contains one dataGridview Image coloumn. My requirement is Insert two lables into DataGridview image cell.
and load picture name into that two lables.
i.e cell lool likes following
Sunset
Image
Sunset
For insertion of two lables inside the cell I used Painting.
My coding is
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=sts5;uid=sa;password=sst;database=master");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from Images", con);
ds = new DataSet();
da.Fill(ds, "upload");
dataGridView1.DataSource = ds.Tables[0];
con.Close();
}
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{ if (e.RowIndex > -1)
{ if (e.ColumnIndex != -1)
{
e.Paint(e.CellBounds, DataGridViewPaintParts.All);
foreach (DataGridViewRow drrow in dataGridView1.Rows)
{
if (e.ColumnIndex == 0 && e.RowIndex > -1 && e.RowIndex != this.dataGridView1.NewRowIndex)
{
string[] FlightCode= dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
Rectangle img1 = e.CellBounds;
Rectangle textRect = e.CellBounds;
Rectangle textRect1 = e.CellBounds;
textRect.Width -= e.CellBounds.Width / 2;
textRect.Height = -2;
textRect1.Width -= e.CellBounds.Width / 2;
textRect1.Height = 36;
Rectangle textbox = e.CellBounds;
textbox.Width = e.CellBounds.Width / 2;
textbox.Height = 10;
Rectangle lblFlight = e.CellBounds;
lblFlight.Y += textRect.Height;
lblFlight.Width = e.CellBounds.Width;
lblFlight.Height = 0;
Rectangle lblFlightCode = e.CellBounds;
lblFlightCode.Y += textRect1.Height;
lblFlightCode.Width = e.CellBounds.Width;
lblFlightCode.Height = 20;
e.Paint(lblFlight, DataGridViewPaintParts.All);
e.Paint(lblFlightCode, DataGridViewPaintParts.All);
e.Handled = true;
StringFormat formater = new StringFormat();
formater.Alignment = StringAlignment.Center;
Font fnt = new Font("Verdana", 7);
using (SolidBrush br = new SolidBrush(Color.Black))
{
e.Graphics.DrawString(FlightCode, fnt, br, lblFlightCode, formater);
e.Graphics.DrawString(FlightCode, fnt, br, lblFlight, formater); }
}
}
e.Handled = true;
}
}
}
I write cell painting for above requirement. my doubt is I have no of rows in my Sql Images table. after run my project the DataGridview image cell getting vibrate why because the cell painting event is fired for every secon and also when iam scrolling DataGridview from top to bottom then also image cells are getting vibrating.
This become bad look for my clients.
to avoid this what can i do please help me.
Thanks & Regards