GridView Column Width Issue C# - c#

I am trying to set the width of a column in a Gridview in C# and it is not working at all. I have tried to set the header style and item style via the following method:
GridView1.Columns[0].ItemStyle.Width = Unit.Pixel(200);
and similarly for the header style. None of this seems to be working. I see many forums online that people are having a similar issue but after looking for approx 1h30m I still could not find one that also had a solution.
There is also one stipulation - all of the columns in my grid are dynamic so it must be something that I can set programatically - not in markup.
I appreciate any help on this issue!
EDIT: with the help of Jon and through some other research I believe the issue is because I have defined no columns in the markup. However, I have not figured out how to resolve this. I cannot place columns in the markup because the table is generated using repeaters on the back end. If anyone has any insight into this please let me know.

I had the same problem. I realized that because I was databinding my grid to a datasource, and because my gridview had the property of autosizing the columns, there is no way to set the width of the column afterwards. If you change that property to false, you should be able to programatically resize them.
EDIT: this is the markup you need somewhere inside your GridView markup:
<Columns>
<asp:BoundField DataField="your_database_column" />
<asp:BoundField DataField="your_next_database_column" />
...
</Columns>

Just setting GridView1.Columns[0].ItemStyle.Width = Unit.Pixel(100); ought to do it. The corresponding td should contain style="width:100px" when you do so. The header element's width need not be modified, as it's done so automatically.
Also set AutoGenerateColumns="false" in your Gridview declaration.

Related

Title and CheckBox in the headers in GridViewCheckBoxColumn telerik c#

I use telerik on c# wpf.
I have a GridViewCheckBoxColumn column in my table. I need the title of this column to contain CheckBox(which by default are to highlight the entire column) and name. Аnd so that the checkbox in the header marked all the elements of the table. How can I do it?
This is the expected outcome:
https://i.stack.imgur.com/Qwm1k.png
I think you have to use GridViewSelectColumn.
*`<telerik:GridViewSelectColum/>`*
Add to 'Header' property to this xaml code.
Good luck
I'm Chuffed I can help you, ZakharovV
<telerik:RadGridView.Columns>
<telerik:GridViewSelectColumn>
<telerik:GridViewSelectColumn.Header>
<CheckBox>My Header</CheckBox>
</telerik:GridViewSelectColumn.Header>
</telerik:GridViewSelectColumn>
</telerik:RadGridView.Columns>
"GridViewSelectColumn.Header" is important
Good Luck

How to do paging in datagrid asp.net?

I can't get paging to work in my DataGrid even though I set AllowPaging="true" and AllowCustomPaging="true"
It was working earlier but now I don't know what happened.
What do I need to change in order to make it work?
I had to set datagrid1.VirtualItemCount, as explained on msdn – DataGrid.VirtualItemCount, to the actual number of items in my data source since CustomPaging does not work without it.
So I added
datagrid1.VirtualItemCount = 200;
and it worked.

remove gridview selector column

I'm sure the answer to this question is incredibly obvious, but for the life of me I can't figure out how to remove the "selector" column from the default winforms gridview. (The column the red arrow is pointing at).
I've tried programmatically removing the column gridview.Columns[0].Remove but that just removed my first data column. It doesn't show up in the "column collection" also, and I've played with all the settings that looked somewhat promising.
Thanks for the help!
Set the RowHeadersVisible to False in the GridView (it's a property)

Get width of column/cell in GridView when 'width' property on column/cell is not set

I have a GridView, in which the width of each column is dynamic and changes depending on the dataset. I need to get the width of each column for use in my PDF exporter, but the column/cells width is always '0', presumably because I haven't set the width property.
Is there a way around this?
I don’t know of a way to do this purely in the code behind, if someone else does I would love to hear about it.
What does occur to me as one possible way to accomplish this would be to add a hidden field to the page and then use JavaScript to populate the hidden field with the rendered column widths. This is assuming that you are displaying a standard ASPX page and giving the users an option to export to PDF. Your export code can then use the value of the hidden field to build the PDF layout. Doing it this way would be a bit of a kludge but it should work.
Hoping someone else has a better option…
Try using following gridview's property:
Double griwColWidth = this.Column1.ActualWidth.Value;

c# DataSet To DataGrid

I would like to convert my dataset which contains one table into a datagrid in order to get the width of each column to add many groups title with the correct width just above.
I've tried " mydatagrid.ItemsSource = mydataset.Table[0].defaultview;" and it works properly
except this instruction doesn't fill any columns in my datagrid so i can't get any width of any columns.
If anyone have an idea, thanks a lot.
Have you set AutoGenerateColumns = true?
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.autogeneratecolumns.aspx
Your question sounds a bit strange. You DO know, that DataGrid is a web control, yes?
First of all, there are two DataGrid controls: one in System.Windows.Forms namespace for Windows Forms and the other in System.Web.UI.WebControls for Web.
In either case, DataGrid is a control that shows a data from a data source in a grid. In order to show the data, you have to bind it to a control.
This is quote from DataGrid article:
"To display a table in the System.Windows.Forms.DataGrid at run time, use the SetDataBinding method to set the DataSource and DataMember properties to a valid data source."
dataGrid1.SetDataBinding(SuppliersProducts, "Suppliers");
So i'm back with a solution.
My columns were empty cause my code was define before the event "Loaded" happen so now everything is perfect.
Thanks for precision about the datagrid.

Categories