DataGridViewの各列にグリッド全体のパーセンテージ幅を与える方法はありますか?現在、固定幅を使用していますが、1つの列に15%の幅、別の列に25%の幅などを指定して、テーブルの100%がグリッドで埋められ、サイズが変更されるようにします。
22808 次
3 に答える
17
DataGridViewColumn.FillWeightプロパティを使用してみてください。基本的に、すべての列に重みを割り当て、それらの重みに従って列のサイズを変更します。MSDNアークティックルはそれほど素晴らしいものではありません。より良い説明については、以下の記事を参照してください-
于 2013-03-22T20:34:30.803 に答える
3
これを試して
private void DgvGrd_SizeChanged(object sender, EventArgs e)
{
dgvGrd.Columns[0].Width = (int)(dgvGrd.Width * 0.2);
dgvGrd.Columns[1].Width = (int)(dgvGrd.Width * 0.2);
dgvGrd.Columns[2].Width = (int)(dgvGrd.Width * 0.4);
dgvGrd.Columns[3].Width = (int)(dgvGrd.Width * 0.2);
// also may be a good idea to set FILL for the last column
// to accomodate the round up in conversions
dgvGrd.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
于 2020-08-22T21:32:56.867 に答える
2
値コンバーターを使用できます
これはパラメーターを減算しますが、パラメーターで除算することもできます。
<local:WidthConverter x:Key="widthConverter"/>
<GridViewColumn Width="{Binding ElementName=lvCurDocFields, Path=ActualWidth, Converter={StaticResource widthConverter}, ConverterParameter=100}">
[ValueConversion(typeof(double), typeof(double))]
public class WidthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// value is the total width available
double otherWidth;
try
{
otherWidth = System.Convert.ToDouble(parameter);
}
catch
{
otherWidth = 100;
}
if (otherWidth < 0) otherWidth = 0;
double width = (double)value - otherWidth;
if (width < 0) width = 0;
return width; // columnsCount;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
于 2013-03-22T20:33:58.880 に答える