I want to make my row to auto resize depending on screen size.
When I do this for column it really works:
grid.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
But when I want to do same thing for row. I don't have fill option.
I only have like
grid.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCells;
How can I make my rows to fill my screen depending on it's size?
You can use the AutoResizeRow method.
Per MSDN:
This method is useful if you want to control when a row resizes. The row height is adjusted only once per method call; if the contents of the row later change, the row will not automatically adjust. To resize all rows, use the AutoResizeRows method. To set the row to automatically resize whenever its contents change, use the AutoSizeRowsMode property.
There's also a MSDN sample here.
You may want to look in to the RowTemplate property named Height. You can do your calculation as to when the screen size changes
EDIT:
You can put this in a resizing event, such as Form_ResizeEnd. YMMV on how the grid may perform with a large number of rows.
foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.Height = 22; //your calculation based on the screen height
}
I believe you can use System.Windows.Form.Screens class, specifically Screens.PrimaryScreen.Bounds.Height if you want the primary screen's height. It's up to you to see what kind of size you want to maintain.
Related
I want to resize the row height of DataGridView based upon height of individual cells in a row.
For which I am using
dt_grid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
but I also want to limit it to a maximum size beyond which it shouldn't increase the row height.
I believe it is not supported by DataGridView. You can either set AutoSizeRowsMode as you did, or have the content in one line and truncatenated at the end of column(s) width.
However, there's a workaround you can use to achive a very similar effect you're asking for. Let's say you have a Description column, which causes you a trouble. Then, if you set the column to AllCells, you can truncatenate it in the source, i.e. in SQL this way:
SELECT ID, CASE
WHEN LEN(Description) > 150 -- check length
THEN CONCAT(SUBSTRING(Description,1,150),'...') -- truncatenate, if needed
ELSE Description -- else keep it as is
END
FROM MyTable;
You won't get the long text precisely finishing to a pixel, but it will be totally fine and not intereferring with user experience.
I have one problem. When ever I try to print my datagridview it always splits in half, so what I wanna do is to find a way to set length of column name adapt to data inside the column or to set that data adapts to column name. Its not the best explanation but take a look at the pictures:
This is what I have now:
This is what I want:
So basically I want the name of the column and data inside the rows to be visible but to take as minimum space as they can, depending on what length is bigger, If column name is bigger, adapt to that, if data length is bigger, adapt to that. Any solution for my problem?
Set the following property:
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
See here the documentation for DataGridViewAutoSizeColumnsMode member AllCells:
The column widths adjust to fit the contents of all cells in the
columns, including header cells.
Is there any way to disable resizing of the width of a DataGridView rowheader, without setting AllowUserToResizeRows to False?
I've found row.HeaderCell.Resizable but it's readonly.
Use the DataGridView.RowHeadersWidthSizeMode Property:
Gets or sets a value indicating whether the width of the row headers
is adjustable and whether it can be adjusted by the user or is
automatically adjusted to fit the contents of the headers.
myGrid.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
Yes you can do this.. using the below code -
yourDataGridView.Columns[0].Resizable = DataGridViewTriState.False;
now you can not change the width of the first column in datagridview
I have a DataGridView and I want only a specific column to resize when the window is resized.
Currently, I am resizing the columns which need to be fixed on form_Load() and form_Resize()
dataGrid1.Columns[0].Width = 100;
dataGrid1.Columns[1].Width = 100;
dataGrid1.Columns[3].Width = 100;
Is there a better way to do this?
There is an AutoSizeMode property on the DataGridViewColumn class.
I'd imagine you want to set it to DataGridViewAutoSizeColumnMode.AllCells on columns 0, 1 and 3, and to .Fill on column 2. (It looks like .None means it won't even adjust to the cell contents, which I don't think is what you want.)
I am quite a new to Winforms and recently I got a situation. My client prefer to have each record show as one line and want to use the slide bar if cell has larger content. Moreover, he also wants to stretch the width of "Name" column if total width of all columns is less than width of DataGridView.
I have already set RowsDefaultCellStyle.WrapMode and ColumnHeadersDefaultCellStyle.WrapMode to false along with DataGridViewAutoSizeColumnsMode.AllCells. But it doesn't stretch "Name" column even though I set it manually.
I think you have the right idea, but there is a slight quirk with how Fill works. Here is sample code that will almost do what you want, which it sounds like you pretty much already have:
public Form1()
{
InitializeComponent();
this.dataGridView1.RowsDefaultCellStyle.WrapMode = DataGridViewTriState.False;
this.dataGridView1.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False;
this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
this.dataGridViewColumnName.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
this.dataGridViewColumnName.MinimumWidth = 60;
}
The one catch here is that I set the MinimumWidth on the Name column, that's because there is no DataGridViewAutoSizeColumnMode like AllCellsFillRemaining. If you don't set a MinimumWidth on a column set to Fill it may be resized down to a few pixels if all the other columns crowd it out. To truly get the behavior you're after I believe you would need to override the auto size behavior.