DataGridView 鼠标放上去 列的宽度自动变化

 if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                //新建一个GDI画图  这里的imagelist1随便找一个就好了  必须要的  不知道她有什么用
                Graphics g =Graphics.FromImage(imageList1.Images[0]);
                //获取DataGridView的字体
                Font myf = dgstockpicking.Font;
                //计算他的宽度
                SizeF size0 = g.MeasureString(dgstockpicking.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(), myf);
                //如果宽度大于默认的宽度100 才进行转换
                if ((int)size0.Width > 100)
                {
                    this.dgstockpicking.Columns[e.ColumnIndex].Width = (int)size0.Width;
                }
            }

 

posted @ 2012-07-02 11:08  Gandell  阅读(211)  评论(0)    收藏  举报