C#:DataGridView右侧和滚动条的多余空间 [英] C#: DataGridView's extra space on right-hand side and scrollbar

查看:101
本文介绍了C#:DataGridView右侧和滚动条的多余空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataGridView 控件,并且列数根据每10分钟来自用户的输入而变化。
需要实现的是:

I have a DataGridView control, and the number of columns changes depending on the input from the user every 10 minutes. What needs to be achieved is:


  1. DataGridView 需要显示所有没有水平滚动条的列,除非它影响任何单元格内容的可读性。如果开始隐藏单元格内容,则需要显示一个水平滚动条。

  2. 无论当前显示多少列,并且无论是否显示水平滚动条,右侧- DataGridView 的数据的手侧必须填充,以便没有未使用的空间。

  3. 无论单元格内容的长度如何,所有列宽都必须相同。 (一个单元格的整数可以从10到9999)

  1. The DataGridView needs to show all columns without a horizontal scrollbar unless it affects the readability of any cell contents. If it starts hiding a cell content, a horizontal scrollbar needs to appear.
  2. Regardless of how many columns are currently shown, and regardless of whether the horizontal scrollbar is shown or not, the right-hand side of the DataGridView's data must be filled so there is no unused space.
  3. Regardless of the length of the cell contents, all of the column widths need to be the same. (A cell can have an integer from 10 up to 9999)

这三个条件都需要同时满足,但是,如果我尝试满足一个要求,其他条件就会瓦解。我确定无论如何总是至少有16列。是否有更多列实际上取决于用户。

All of these 3 requirements need to be satisfied at the same time, but if I try to meet one requirement, the other(s) falls apart. I know for sure that there are always at least 16 columns no matter what. It is really up to the user if there will be more columns or not.

有人可以告诉我以下内容是什么问题吗?

Can someone tell me what is wrong with the following?

int countVisible = 0; //Count the number of columns displayed
foreach(DataGridViewColumn col in myDGV.Columns)
    if(col.Visible) countVisible++;

//By default, use Fill mode
DataGridViewAutoSizeColumnMode mode = DataGridViewAutoSizeColumnMode.Fill;

//I am trying to show up to 20 columns without a scrollbar
//A horizontal scrollbar required for more than 20 columns
//If there are more than 20 columns, use DisplayedCells mode
if(countVisible > 20)
    mode = DataGridViewAutoSizeColumnMode.DisplayedCells;

//Apply the mode to all columns
for(int i = 0; i < myDGV.Columns.Count; i++)
    myDGV.Columns[i].AutoSizeMode = mode;  

如果列数小于一定值,则此代码有效比特定值大,将缩小所有行中只有2位数字的列的宽度,这违反了要求#3。通过缩小某些列的宽度,它在右侧增加了空间,并且违反了要求2。

This code works if the number of columns is less than a certain value, but in the case of greater than the certain value, it shrinks the width of columns that only have 2-digit numbers in all rows, and it violates the requirement #3. By shrinking some column widths, it creates extra space on the right-hand side, and it violates the requirement #2.

我真是迷失了自己,陷入了困境。有人可以帮忙吗?任何建议将不胜感激。

I am so lost and stuck. Can someone please help? Any advice will be greatly appreciated.

推荐答案

这些要求对我来说似乎有些晦涩。一个问题是要求3…

The requirements appear to be somewhat obscure to me. One issue would be requirement 3…

3。无论单元格内容的长度如何,所有列宽都必须相同。 (一个单元格的整数可以从10到9999)

如果所有列的宽度必须相同且至少为一(1)单元格的值为 9999,那么无论其具有多少位,所有列的宽度都将为该宽度。

If ALL columns have to be the same width and at least ONE (1) cell has a value of "9999" then ALL the columns will be this width regardless of how many digits it has.

它的确确实是要求拥有所有数据在单元格中显示,此外…使所有列具有相同的宽度。这将需要遍历每个单元格,并找到具有最大宽度的单元格以显示其所有数据,并使所有列都具有该宽度以符合先前的要求。

It does appear a requirement is to have all the data display in a cell, in addition… have all the columns the same width. This will require going through each cell and find the cell with the largest width to display all its data and make ALL columns this width to keep with the previous requirement.

该点就是说,要求所有列的大小都相同,并确保显示每个单元格的所有内容,这无庸置疑……每一列都必须是最大值的宽度。任何其他情况都会违反其中一项要求。

The point being, that requiring all the columns to be the same size AND make sure ALL the contents of every cell is displayed leaves little to question… Each column will have to be the width of the largest value. Any other situation will break one of the requirements.

假设以上正确,其他要求

Assuming the above is correct, the other requirement

2。不管当前显示多少列,并且无论是否显示水平滚动条,都必须填充DataGridView数据的右侧,以确保没有未使用的空间。

我相信您将必须控制这一点。不幸的是,网格列填充属性将很高兴将100列填充到一个很小的空间中。这建议您必须获得显示的网格宽度,计算出多少列,找到这些列的宽度,然后检查它是否适合网格显示。

I am confident YOU will have to control this. Unfortunately, the grids column "Fill" property will gladly cram a hundred columns into a small space. This suggest that you will have to get the grids displayed width, count how many columns there are, find the width for the columns and check to see if it fits in the grids display.

这意味着列宽的MINIMUM值。如果将每列的宽度设置为最小值,并且所有列的总宽度大于网格宽度,那么显然您将需要水平滚动条。

This implies a MINIMUM value for a columns width. If each columns width is set to a minimum value AND the total width from all columns is greater than the grids width, then obviously you are going to need a horizontal scroll bar.

鉴于此,找到所有列的总宽度很容易。如果该值大于网格宽度,则水平滚动条将自动出现,除非您希望避免可能会涉及调整网格大小的列拆分,否则无需执行其他任何操作。如果列的总宽度小于网格宽度…,则只需将列设置为填充即可。下面是一个示例。

Given this, to find the total width of all the columns is easy enough. If this value is greater than the grids width, then the horizontal scroll bar will appear automatically, nothing else needs to be done unless you want to avoid the possible splitting of a column which would most likely involve resizing the grid. If the total width of the columns is less than the grids width… then simply set the columns to fill. Below is an example.

全局变量 minWidth 用于确保所有列均至少为该值。您可以通过其他方式获得此值。在这种情况下,其目的是设置列的最小宽度。

A global variable minWidth is used to ensure all columns are at least this value. This is a value you could get some other way; in this case, its purpose is to set a columns minimum width.

首先,检查宽度是否小于最小宽度,是否设置为最小宽度最小。接下来,将每列的宽度设置为给定值。最后检查所有列的总宽度是否大于网格宽度。如果列不适合网格宽度,则只需将网格设置为 DisplayedCells 即可弹出水平滚动条。如果列适合,则只需将网格 AutoColumnSizeMode 设置为填充。希望对您有所帮助。

First, a check is made to see if the width is less than the minimum and if so set it to the minimum. Next, set each columns width to the given value. Finally check to see if the total width of all columns is greater than the grids width. If the columns will not fit in the grids width then simply set the grid to DisplayedCells and the horizontal scroll bar will pop up. .If the columns do fit, then simply set the grids AutoColumnSizeMode to Fill. Hope this helps.

int minWidth = 40;

private int YourMethodToGetColumnWidth() {
  return myDGV.Width / myDGV.Columns.Count;
}

private void SetColumnWidths() {
  int columnWidth = YourMethodToGetColumnWidth();
  if (columnWidth < minWidth)
    columnWidth = minWidth;
  foreach (DataGridViewColumn col in myDGV.Columns)
    col.Width = columnWidth;
  int allColumnsWidth = columnWidth * myDGV.Columns.Count;
  if (allColumnsWidth > myDGV.Width) {
    myDGV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
  }
  else {
    myDGV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
  }
}

这篇关于C#:DataGridView右侧和滚动条的多余空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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