Datagridview单元绘画事件引发问题单元振动. [英] Datagridview cell painting event Firing problem cell getting vibrating.

查看:85
本文介绍了Datagridview单元绘画事件引发问题单元振动.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个sql表,其中包含图像格式的Flightimages

FlightImage FlightName
Binarydata Sunset
Binarydata Lotus
Binarydata Winter
Binarydata Blue Hills

我将上面的表绑定到dataGridview中. dataGridview包含一个dataGridview图像列.我的要求是将两个标签插入DataGridview图像单元中. 并将图片名称加载到这两个标签中.

即cell lool喜欢关注

日落
图片
日落

为了在单元格中插入两个标签,我使用了绘画.

我的编码是

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;
                }
            }
        }


我为上述要求写了单元格绘画.我的疑问是我的Sql Images表中没有行.在运行我的项目后,DataGridview图像单元开始振动,为什么因为每个secon都触发了单元绘画事件,而且当iam从上到下滚动DataGridview时,图像单元也开始振动.
这对我的客户来说真是不好看.
为避免这种情况,我该怎么办,请帮助我.

谢谢&关于


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

推荐答案

我想更好的选择是根据您的需要创建自定义列类型,而不是总是绘制网格.我不确定是否可以直接将控件添加到网格单元而不是使用paint事件.
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.


这篇关于Datagridview单元绘画事件引发问题单元振动.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆