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.
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.
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.
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.