Click here to Skip to main content
15,500,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 18-Aug-11 23:08pm
v2

1 solution

I guess the better option would be to create a custom column type based on your need rather than painting the grid always. I am not sure if you can directly add controls to grid cell rather than using paint event.
 
Share this answer
 
Comments
kranthi.oru 19-Aug-11 7:34am     CRLF
if u don't the answer why did u sent to me this useless answer.Because of ur answer others are not seeing my Question.All browsers are feel this is answered question we don't give solution.but they don't know you are giving worst answer. my order to u next time don't give this type of suggestions.
dan!sh 19-Aug-11 7:41am    
I have suggested you an alrenative already. Now it is up to you to decide and implement if you wish. Just to clarify, at CP, code demand is considered to be rude. If you want someone to write code for you, opt for RentACoder.
dan!sh 19-Aug-11 7:42am    
You should also try not be rude next time since that annoys people who are trying to help others just because they like it.
stingrayweb 3-May-12 1:47am    
@kranthi.oru, you need a lesson in manners. Grow up.
kranthi.oru 19-Aug-11 8:00am    
i didn't demand any code. already i got reply from others.u gave to me same suggestion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900