怎样使到DataGrid的列根据内容自动调整列宽

allanli 2003-04-24 06:04:50
如题
...全文
51 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
allanli 2003-04-24
  • 打赏
  • 举报
回复
to Latitude(Henry)
你的办法是不行的,我想只能用你楼上的办法了
Latitude 2003-04-24
  • 打赏
  • 举报
回复
http://www.csdn.net/develop/author/netauthor/Latitude/
我专栏中的《 Henry手记— WinForm Datagrid结构剖析(一)》
wuzhiwen 2003-04-24
  • 打赏
  • 举报
回复
public void AutoSizeCol(int col)

{

float width = 0;

int numRows = ((DataTable) dataGrid1.DataSource).Rows.Count;



Graphics g = Graphics.FromHwnd(dataGrid1.Handle);

StringFormat sf = new StringFormat(StringFormat.GenericTypographic);

SizeF size;



for(int i = 0; i < numRows; ++ i)

{

size = g.MeasureString(dataGrid1[i, col].ToString(), dataGrid1.Font, 500, sf);

if(size.Width > width)

width = size.Width;

}



g.Dispose();



dataGrid1.TableStyles["customers"].GridColumnStyles[col].Width = (int) width + 8; // 8 is for leading and trailing padding

}

给你,还有例子,也给你
http://www.syncfusion.com/faq/winforms/Files/datagridautosizecols.zip

16,555

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧