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
Related
so i'm bounding my DataGrid with some data from an sql query, when I show it everything looks fine, all the data/columns/rows are there.
Now what I want to do is set fixed width to certain columns ( Name, Email, Address) and if the text excedes that width, i want it to be hidden. I've seen a lot of ways to change the width in tables, but this doesn't work with DGV.
I also tried DataGridView1.Columns[1].Width() but DGV.Columns doesn't have a width option.
I'm not adding any code because its a general questions, if you have any examples, that would be great. Thanks
Columns does not have width property but Cells have it. you can set it at cell level.
TestGridView.Rows[0].Cells[0].Width
Use TestGridView.Rows[0].Cells[0].Text property to get value of the cell and you write logic to check if it exceeds the specified width.
I am creating a custom control in C#, and want to have a grid cell that contains a ListBox, which can be hidden or shown as desired. Hiding it is easy, I just set the Width to zero, however when I want to show it, I need to know the width that the ListBox would like to use.
I thought that DesiredSize.Width would give me this vale, but it's always zero, even after calling Measure(). Here is the code I'm using to show the ListBox...
_lb.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
_lb.Width = _lb.DesiredSize.Width;
Any ideas how I find the desired width?
If your ListBox is in the cell of a grid, give that grid column a name. Then in code, you can access the .ActualWidth property of that grid column and use that value to set the width of your ListBox.
That assumes of course that the width of your grid column is not set to Auto, because that would still give you a 0 value.
_lb.Width = myGridColumn.ActualWidth
You might need to subtract a little bit from the column width to make your control fit nicely.
EDIT
One thing that I've found is that the ListBox must have items added to it before it will return anything other than 0 when it is measured.
string myItem = "Don't ask for a bath in Athabaska";
_lb.Items.Add(myItem);
_lb.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
double width = _lb.DesiredSize.Width;
As long as the ListBox has already been added to the window/usercontrol/grid, the above code returns a value of 227.53 for the width variable; using my defaults for font family and size.
If the ListBox has not been added to the window, or it doesn't have any items in it, it will return 0 for the .DesiredSize.Width property.
Also, if the .Visibility property is set to Collapsed instead of Hidden, the width will be 0.
Don't set the width to 0 when starting. Leave the width alone initially, set the .Visibility to Hidden. It will render to the needed width, but won't be shown. Then you can measure it and start playing around with the width.
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.
I have a DataGrid that I'm binding to DataTable that got filled by a SQL query. I would like the columns of this grid to be auto-generated with a width of up to a certain number of pixels, but still be expandable by the user if they want it to be wider. Currently the ColumnWidth property on the datagrid is set to SizeToHeader (which doesn't seem to work as described anyway, since it's still sizing to cell content).
Is there any way to set a max generation width?
Setting MaxColumnWidth prevents the user from resizing the column larger than that width.
I've also tried hooking into AutoGeneratedColumns, but since the rows aren't loaded yet the ActualWidth properties just represent the MinWidth I've set. Is there an event that fires once the datagrid finishes loading its initial set of rows?
Iterate over the column collection and get the width for each column. If the width is greater than the threshold set it to your maximum width. You would want to do this after the control has initialized.
This article may be helpful: http://support.microsoft.com/kb/812422
Something like this may work:
foreach(var c in grid.Columns)
{
var width = c.Width;
if(width > threshold)
{
c.Width = threshold;
}
}
From the documentation the Width property set on an individual DataGridColumn takes precedence over the ColumnWidth property. You might be able to iterate over the DataGridColumns in the AutoGeneratedColumns event and set individual Widths and have them stick.
I needed to modify rtalbot's code slightly to get this to work for my WPF/.net 4.5 project ... actualwidth and datagridlength:
// fix extra wide columns
foreach (var c in dgMain.Columns)
{
var width = c.ActualWidth;
if (width > 200)
{
c.Width = new DataGridLength(200);
}
}
On my windows form, I need to programatically set the width of columns in the grid view. I am using the following code:
this.dgridv.Columns[columnName].Width = columnWidth;
The above stmt runs without any error. But, the column width remains unchanged. If I insert a breakpoint and check the value of width after the stmt runs, it is still 100, which I guess is the default value of datagrid column width.
Is there something else I need to do apart from this? Are there any values I need to set before changing the column width?
Any help or pointers on this are highly appreciated.
Thanks :)
Have you got the AutoSizeColumnsMode set to Fill?
If you have you'll need to set the FillWeight property instead. This isn't a simple width but the proportion of width / no. columns that this column takes up.
If you haven't resized the columns it will be 100.0.
If the column has been widened it will be > 100.0.
If the column has been shrunk it will be < 100.0.
Widening one column, by definition, shrinks the rest.
This worked:
this.dgridv.Columns[columnName].AutoSizeMode= DataGridViewAutoSizeColumnMode.None;
this.dgridv.Columns[columnName].Width = columnWidth;
To reset it back, I am using:
dgvCol.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
dataGridView1.Columns[index].Width = 150;
This line of code will set the column width programatically.