在数据网格中自动生成的列上设置最大自动生成宽度 [英] Setting max autogenerate width on autogenerated columns in a datagrid

查看:73
本文介绍了在数据网格中自动生成的列上设置最大自动生成宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要绑定到由SQL查询填充的DataTable的DataGrid。我希望此网格的列可以自动生成,宽度最多可达到一定数量的像素,但如果用户希望它更宽,则仍可以由用户扩展。当前,datagrid上的ColumnWidth属性设置为SizeToHeader(因为它仍在调整单元格内容的大小,所以无论如何似乎都无法如所描述的那样工作。)

I have a DataGrid that I'm binding to DataTable that got filled by a SQL query. I would like the columns of this grid to be auto-generated with a width of up to a certain number of pixels, but still be expandable by the user if they want it to be wider. Currently the ColumnWidth property on the datagrid is set to SizeToHeader (which doesn't seem to work as described anyway, since it's still sizing to cell content).

有没有设置最大生成宽度的方法?

Is there any way to set a max generation width?

设置MaxColumnWidth可以防止用户调整大于该宽度的列的大小。

Setting MaxColumnWidth prevents the user from resizing the column larger than that width.

我也尝试过钩入AutoGeneratedColumns,但是由于未加载行,而ActualWidth属性仅表示我设置的MinWidth。

I've also tried hooking into AutoGeneratedColumns, but since the rows aren't loaded yet the ActualWidth properties just represent the MinWidth I've set. Is there an event that fires once the datagrid finishes loading its initial set of rows?

推荐答案

对列集合进行迭代并获取每列的宽度。如果宽度大于阈值,则将其设置为最大宽度。您希望在控件初始化后执行此操作。

Iterate over the column collection and get the width for each column. If the width is greater than the threshold set it to your maximum width. You would want to do this after the control has initialized.

本文可能会有所帮助: http://support.microsoft.com/kb/812422

This article may be helpful: http://support.microsoft.com/kb/812422

类似的方法可能有效:

foreach(var c in grid.Columns)
{
    var width = c.Width;
    if(width > threshold)
    {
        c.Width = threshold;
    }
}

这篇关于在数据网格中自动生成的列上设置最大自动生成宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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