从 C# 中的数据源填充 dataGridView 后,如何调整列大小?
我无法让它发挥作用。这是我的代码的一部分,可以给您一些想法。
Scout ScoutInstance = new Scout();
String sql;
DataTable table;
sql = "select * from FD_GROUP";
table = ScoutInstance.getTableValues(sql);
dataGridView1.DataSource = table;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
dataGridView1.AutoResizeColumns();
int width = 0;
foreach (DataGridViewColumn column in dataGridView1.Columns)
if (column.Visible == true)
{
width += column.Width;
Console.WriteLine("{0}", column.Width);
}
Console.WriteLine("{0}", width);
我得到的输出告诉我,两列的宽度相等 (100),但是程序本身显示不同宽度的列。
I can not get this to work. Here is part of my code to give you some idea.
Scout ScoutInstance = new Scout();
String sql;
DataTable table;
sql = "select * from FD_GROUP";
table = ScoutInstance.getTableValues(sql);
dataGridView1.DataSource = table;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
dataGridView1.AutoResizeColumns();
int width = 0;
foreach (DataGridViewColumn column in dataGridView1.Columns)
if (column.Visible == true)
{
width += column.Width;
Console.WriteLine("{0}", column.Width);
}
Console.WriteLine("{0}", width);
The output I get tells me that both the columns are equal in width (100), however the program itself displays the columns of different widths.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以尝试这样的事情......
然后在将 datagridview 与数据源绑定后放置此函数(即)
you could try something like this....
and then put this function after binding the datagridview with datasource(i.e)
您应该更新网格布局才能显示正确的值。否则,在您离开子系统之前它不会更新,因为布局更新已排队。 使用 : ,您将获得您的值。
因此,在 AutoResizeColumns 之后
You should have the Grid Layout updated for the right values to show. It won't update before you leave your sub otherwise, since the Layout update is queued. So use :
After your AutoResizeColumns, and you'll get your values.
关于更新布局:
http://msdn.microsoft.com /en-us/library/system.windows.uielement.updatelayout.aspx
“确保此元素的所有可视子元素都已正确更新布局。”
“所有计算出的尺寸都将得到验证。”
“如果您绝对需要更新的尺寸和位置,您应该只调用 UpdateLayout”......这就是您的情况。
About update Layout :
http://msdn.microsoft.com/en-us/library/system.windows.uielement.updatelayout.aspx
"Ensures that all visual child elements of this element are properly updated for layout."
"all computed sizes will be validated."
"You should only call UpdateLayout if you absolutely need updated sizes and positions" ... which is your case.