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)
Related
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.
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.
I have created one report which hides some of the columns of table at end when some criteria don't match. For example, there are total 7 columns and I am hiding 2 columns when criteria don't match. This leads to so much empty space at right side as table don't centre automatically.
I have tried VerticalAlign = Middle but it is not working.
Finally after all efforts I didn't find exact solution but found one alternative.
I have applied Anchoring = Left, Right on table and that helps me to make my table looks better.
What it actually does is, it keeps width as it was specified and based on no. of columns, columns width either shrink or grows.
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
I'm developing a VSPackage (extension for Visual Studio 2010), and I have a tool window that is hosting a DataGridView control.
I have a very large data-set (e.g. 2D array of size 16384 x 16384) and I update data via VirtualMode.
I want to add index to the row header cells and column header cells,
so I tried:
Auto resize via grid methods
grid.AutoResizeRowHeadersWidth(
DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
Iterating through each row
foreach (DataGridViewRow row in grid.Rows) {
row.HeaderCell.Value = (row.Index + 1).ToString();
}
Subscribing to RowsAdded/OnPaint and updating the value there.
But, all these methods were SUPER SLOW!
They significantly degraded the rendering of the DGV (takes like ~ 5 seconds or more to render the view after each of these methods, or even worse).
What would you suggest to do instead?
I did do initial research into the issue and you've listed all the typical solutions, unfortunately setting 250 million cell values will take some time.
The only thing I didn't see you mention was to fake the header cells (ie first column and row with grey background) and populate them with the source data. Since the DataGridView cell values with be populated with Pixel values one idea is to a append 1 pixel strips to the left and top of each image that you load.
I still think the solution needs a better design. And below are indirect answers to your question.
Make a Picture Dialog with a Zoom-In function. When the user/developer zooms into the pixel level show them the RGB values. Here is an excellend Drawing Tool example written in C# based of MFC CLiDraw and it has an example to show you how to zoom in.
The other idea is a EyeDropper Colour Picker you can get from: http://www.codeproject.com/Articles/36540/Adobe-Eyedropper-Control