How can I center the heading in a column on a DataGridView? - vb.net

I have a strange problem and it's probably a simple fix, but after much research, I cannot seem to find a solution.
I have a DataGridView on which I'm trying to center the column headings, but the result is a left bias in the centering—almost like an indenting problem. I've seen a few posts on this issue on a site or two, but never a solution. Any thoughts?
Here's the statement I'm currently trying to use:
DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

The code you've posted is on the right track: you need to set the ColumnHeadersDefaultCellStyle property of your DataGridView control.
However, you need to create a new DataGridViewCellStyle class and assign that to the ColumnHeadersDefaultCellStyle property. You can't modify the Alignment property as your code sample shows unless you have assigned a DataGridViewCellStyle class to this property.
So, for example, the following code achieves perfectly centered column headings in a blank project:
Dim dgvColumnHeaderStyle As New DataGridViewCellStyle()
dgvColumnHeaderStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
myDataGridView.ColumnHeadersDefaultCellStyle = dgvColumnHeaderStyle
In the future, you may find it easier to do these types of things from the Designer. If you still need to do it yourself through code, you can check the *.Designer.vb file that is created to see how it was done.
EDIT: I just now noticed the slight offset you're referring to in the columns—it does indeed create a little extra padding to the right of each header. It's not a bug, though. There's a much simpler explanation.
Like a ListView, the DataGridView supports sorting by columns. Therefore, each column header reserves enough space to display the sort glyph (usually an arrow) when calculating center justification.
If you want the column headers to be perfectly centered, you'll need to disable sorting. Set the SortMode property for the column to "NonSortable". This should prevent space from being reserved for the sort glyph whenever the column text is center or right justified.

If you want to center or use any other alignment style of the Column Header text you can use this
dgvResults.Columns("ColumnName").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter

Related

VB .NET - Autosize datagridview row height

Problem Definition
So I am trying to do something that should be very basic; however, I can't seem to make it actually work. I am simply trying to make my datagridview autoresize every row height to the text entered. Am I missing something?
What I have tried
I have read through examples on SO and other sites and they all recommend a similar idea. So to make it simple here is exactly what I have done so far:
I created a new datagridview.
I clicked on columns > Add and use the default name and type (textbox)
I kept the Autosizemode of that column at None and DefaultCellStyle WrapMode to True.
I changed the datagridview's AutoSizeRowsMode = AllCells
From there I build my project and type some data in, but the column simply grows the column width and not the row height:
Am I missing a step somewhere? I purposely put every step I did because I feel like I am just missing something very simple...
go to the data grid view -->properties --> default cell style-change the wrap setting to true then use the autosizerow property as any mode like allcells or display cellslike

How to display the datagridview rowheaders on the right?

I have a datagridview which displays row numbers in the rowheaders on the left. One of the users has asked for them to be on the right instead. The display of row numbers is fine, it's the location of the rowheaders that's the issue here.
I have looked through various properties and can't find anything that switches the row header over to the other side.
I realise I could add another column to the right and format it to look like the rowheaders, but that seems like a clumsy workaround.
How about setting the RightToLeft property of the datagridview?
Me.DataGridView1.RightToLeft = Windows.Forms.RightToLeft.Yes
Bear in mind that this reverses the display order of the columns, which you would have to manage as well, if you want to give the appearance that the only thing to change is the position of the rowheader.

Text Align column in Listview: first column cannot be centered

I have a Listview in detail mode with 3 columns. I want to set the text align for the headers to "center". This works for the last two columns but not for the first. If I want to change it to "center" and click on "center", the field keeps being set to "left". Can I change this using the properties or do I need to program this?
Thanks.
According to the documentation:
Due to a limitation in the underlying control, this property has no effect on the first column in the ListView control, which is always aligned to the left. To work around this limitation in .NET Framework version 2.0, you can handle the ListView.DrawColumnHeader event and paint the column header yourself.
Another alternative workaround is to not use the first column at all and hide it by setting its width to zero.
I have got a simple solution: Add a new (not needed) first column. Change the alignment of the second column (your real first column) to right or center (can now be done in the designer). In Form-Load-Event remove the first (temporary) column. Voila - the textalignent now should be correct.
I have testet this behavior under Windows 7, 8.1 and 10. It should work.
greetings from germany
I realize this is an old post, but I just found another workaround. If you insert whitespaces just before the header text you are inserting or changing in your code (I know it's rough and you need to calculate how many spaces gets your header centered, but it works!) then you can alter the position of the first column's (0) header text as follows:
ListView2.Columns(0).Text = " " & ListView1.SelectedItems(0).Text.ToString

Vb.net - Setting a controls margin value

So, I'm adding a label programatically and I'm in need of altering the top margin a little bit to the value 8. I can't do that the obvious way, so what's wrong with my thinking?
Dim LabelAdapter As New Label
LabelAdapter.text = "Adapter"
LabelAdapter.Margin.Top = 8
This gives me the error "Expression is a value and therefore cannot be the target of an assignment".
Label.Margin returns a Padding object.
Since Padding is a structure, it will actually return a copy. You are changing the Top value of that copy, not of the actual control’s margin. Since that would have no noticeable effect, VB correctly prevents it.
You need to assign a whole new margin. In fact, the Margin property (or rather, the Padding class) is arguably broken since it doesn’t allow an easy way to change the individual values.
Unfortunately, we just have to live with it. So to change just the Top value, we need to write:
Dim old As Padding = LabelAdapter.Margin
LabelAdapter.Margin = New Padding(old.Left, 8, old.Right, old.Bottom)
Weird, huh?

MS Access Draw line around detail section that can grow

This really shouldn't be hard, I just can't figure out how to do it.
I am making a proposal report that needs to have a border around it. The problem is to get the vertical lines on the side. I can't figure out how to get a line to grow and shrink based on the height of the detail section.
I have used Crystal reports and sure wish Microsoft would learn a few things in regards to MS Access report writing!
I am very comfortable with VBA so have no fears there.
You were right, this isn't so hard. The trick is to use 2 variables, top and bottom. In the PageHeader_Format event you set top to Me.Height, and in the PageFooter_Format event you set the bottom to Me.Top - correction, where correction is a fixed amound you use to fix the right length. I´m not sure where this amount comes from, you just have to try a little bit.
In the Report_Page event you can then draw your line from top to bottom.
Another method that nobody has mentioned is the one using the Line method of the report, outlined in Knowledge Base article 210321. I've used this one for years, and it can be used to draw both lines and rectangles. It's quite handy for invoices that have variable height subreports for the invoice details, but need the vertical lines to change according to the height of the main report detail.
No VBA needed.
Make a dummy grouping that is unique to each detail. For that grouping, set footer to yes.
In your new group footer section that you just created, add your line.
In your detail section, select all the relevant fields that can grow and set Can Grow = Yes
Done!
Edit
Off-topic, I agree that Access Reports could learn a lesson or two from Crystal. But Crystal isn't perfect either. [/flamewar]
Try this one.
Right click on the detail bar and select properties. Set the special effect to “Sunken”. This will put kind of a border around the detail section that resizes with the detail section.
I have tried to get a line to dynamically resize but its catch 22, by the time you know the height of the section (In the On Print event) you cant make any changes!
use the page event coupled with me.line and scaleheight / scalewidth.
I draw a box around the whole page with the following. Play with it and see where you end up. It is very handy for making professional reports. If you want a line in a certain place on a report you can use the controls coordinates. like
me.line(Mycontrol.left,mycontrol.top) - (myothercontrol.left+myothercontrol.width, myother control.top + myothercontrol.height)
Private Sub Report_Page()
Me.Line (0, 0)-(scalewidth -30, scaleheight-30), 0, B
End Sub