How to automatically resize columns in C1flexGrid? - c#

If I choose one of the WF which have C1flexGrid, and try to maximize that form, columns in my grid have the same width. What I am trying to do is have the columns auto fit to the width of the content, and have the last column fill the remaining space. Is this possible?

For anyone still looking for the answer:
You can use AutoSizeCols method and ExtendLastCol property
c1FlexGrid1.ExtendLastCol = true;
c1FlexGrid1.AutoSizeCols(c1FlexGrid1.Cols.Fixed, c1FlexGrid1.Cols.Count - 1)
Reference.

Related

C#: DataGridView's extra space on right-hand side and scrollbar

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:
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.
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.
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)
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;
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.
The requirements appear to be somewhat obscure to me. One issue would be requirement 3…
”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)” …
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. 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.” …
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.
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.
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.
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;
}
}
I read your interesting question yesterday. Thinking about it, in my opinion the best solution is to get rid of AutoSizeMode at all and create a variable ColMinWidth for a minimum width of a column. Then get DataGridView.Width and divide it by the number of required columns (with a little tweak, perhaps 2px less). If the calculated width of the column is greater then ColMinWidth, then this will be a width of the column, otherwise use ColMinWidth.
Finally set that width to all columns.
I think that this is minimal solution and unlike AutoSize with hidden catches, reliable.

Remove extra padding in DataGridView column label?

I have a DataGridView that I am trying to reduce the width of so it displays how I wish in my form. I am having issues "removing" the extra padding in the column names when reducing the size of the DataGridView.
On the left is the table as I originally had it, I realized that I would like it smaller so I decreased its width. The center image represents the smallest the columns will go before the text wraps to the next line. The right image is the size I would like the table to be, I have shown that the text "(px)" will fit in the space.
I have gone through all of the settings I can find in the designer and have not found anything to help. Does anyone know how to fix this?
Here is my example. Although it might not be the best solution but worked.
I measured the exact width of HeaderCell's content ("Height (px)") and set the column's width with some additional padding (For example, 9).
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
dataGridView1.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False;
Size textSize = TextRenderer.MeasureText(dataGridView1.Columns[0].HeaderText, dataGridView1.Font);
dataGridView1.Columns[0].Width = textSize.Width + 9; // Adding some padding

DevExpress Grid Custom Column Sizing calculation based on Grid Width

My project needs to display a list of contacts in a grid. Normal data - First Name, Last, City, State, Zip, Email, Phone, Company Name.
1 - We need to support wide variation in screen widths 800px -> 2,000px.
2 - We want to display as much information as possible, with as little white space as possible.
3 - As the grid gets wider, we want some fields to expand (Organization Name), others to stay at a maximum Width (State - 2 char).
None of the standard column resize modes seem to work since there is no Max Width column. The State column ends up with a ton of space, and the organziation is still being chopped off.
Has anyone resolved this problem?
I had similar problems with our Gridview and I have raised a ticket with Devexpress team for the same reasonsDevexpress ticket. It seems SetWidth() client side JS method does not work properly when you have fixed width columns.
In your case, I would suggest using percentage size for your columns which needs to be dynamically expanded and static size for the other ones.Also set the text wrap true for cells that you want texts tightly wrapped inside.
settings.Columns.Add(column =>
{
var commmonHeaderStyle = column.HeaderStyle as GridViewHeaderStyle;
commmonHeaderStyle.Font.Bold = true;
column.CellStyle.Wrap = DefaultBoolean.True;
column.FieldName = "Test";
column.Width = System.Web.UI.WebControls.Unit.Percentage(30);
});
You might also want to take a look at this example : Full Screen mode (100% browser Width and Height)

Calculate width available for columns in dataGridView

My question is very simple - after data binding I want to set all columns width so that it will not be horizontal scrolling. Something like this:
columnWidth = grid.width/grid.Columns.Count
But this expression does not consider the "left part of gridView" with which I can select row, which displays current row. How can I calculate its width? [(grid.width - X)/grid.Columns.Count]
Very easy to do this. Just set the DataGridView.AutoSizeColumnMode property to Fill.
grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
And you may need to explicitly ask the grid to resize column spacing. I can't remember for sure. But that is simple to do:
grid.AutoResizeColumns();
According to the documentation, Fill will do the following:
The column widths adjust so that the widths of all columns exactly
fill the display area of the control, requiring horizontal scrolling
only to keep column widths above the DataGridViewColumn.MinimumWidth
property values. Relative column widths are determined by the relative
DataGridViewColumn.FillWeight property values.
So basically, as long as you don't have any columns with a MinimumWidth that will prevent it, this property does exactly what you are looking for.
EDIT: Addressing the comment of the OP:
You can always create a simple property to report that information to you. Something like...
public int DataGridViewDeadSpaceWidth
{
get
{
int x = grid.Width;
foreach (DataGridViewColumn column in grid.Columns)
x -= column.Width;
return x;
}
}
You could also use this to inherit from a DataGridView and add the property yourself to the control. Note that there is probably some other dead space from the borders of the DataGridView that you need to account for.

Trigger the "OnCellSeperatorDoubleClick" in a WPF DataGridView?

I was just wondering if there was a way to trigger the same event that happens when you double click the line between two columns in a DGV. I would like 2 of my columns to, after setting the width of all of the columns, auto resize to the width of their text to take up the extra space.
Is that possible?
If you mean the WPF 'DataGrid', in .NET 4.0, from a quick look at PresentationFramework in ILSpy, it looks like all double clicking on the resize gripper does is sets the width of the column to DataGridLength.Auto.
So you could do something like the following to get the same result as double clicking:
someGrid.Columns[0].Width = DataGridLength.Auto;
If you want the column to fill available space (using the wpf star weighting) then you could use something like:
someGrid.Columns[0].Width = new DataGridLength(1, DataGridLengthUnitType.Star);

Categories

Resources