Im a little confused on the difference between a property and a method. When I think of this, I use the cells default values for my logic and how they change
Default value of a cell is:
Color = blank
Width = 8.43
--If we Set(change) the color to red, then this is referencing a property
--If we auto fit the cells width to say 10(Change the cells width), then this is a method
Either way, the values are being "Changed" from the default settings to a new one. How is it then, that a property and method are not the same??
I understand this logic is wrong. I guess im just trying to think of a better to way to think about it..
Thanks,
Related
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
I'm trying to retrieve the current color of a DataGridView cell while iterating through the Grid. I am explicitly setting the BackColor for both RowsDefaultCellStyle and AlternatingRowsDefaultCellStyle in the Form Load event.
I'm trying to get the cell BackColor per this question, however, at run time, while iterating through cells in a row, in the Immediate Window this: ?dgvemployees.Rows(rowIndex).Cells(i + 1).Style.BackColor.ToString returns "Color [Empty]" every time - even if I change the indexes to get another cell that I know has the default color set.
Am I missing something or not doing something right?
This page on the Cell Style says:
The DataGridView control displays its cells using the styles indicated
by the cell InheritedStyle property, which inherits styles from other
properties of type DataGridViewCellStyle. The styles specified through
the Style property override the styles specified through all other
cell-style properties, but do not necessarily indicate all the styles
that contribute to the cell's appearance.
Basically, you are trying to access the specific cell back color when it hasn't been set. Even though you have set the back color of the rows, it can be overridden at the cell level.
Thankfully, Microsoft has given us a nice way to find the inherited style of the cell, which will get you the grid-level setting for these cells (unless something further down the chain has overridden that).
?dgvemployees.Rows(1).Cells(2).InheritedStyle.BackColor.ToString()
If I had something else that could override this value and cause problems, then I think I'd be left doing a modulus on the row count to see if it was an alternating row or not.
I have a custom coloured background that the user can choose, this however means when they change the colour, the label is invisible because it is the same as the background and I do not want this. The code I have is: opacityLabel.textColor = bgView.backgroundColor; In this bit of code the colour is the same at the start but doesn't change when I adjust the background colour, I also need the colour to continually change with the background colour, I was wondering what I could replace with "=" to make the label do the opposite colour. I can't do "!=" because that means is not equal to rather than opposite! If you can't do it this way could you provide me with a way of doing it?
The first thing you'll need to do is calculate the opposite colour to the backgroundColor, and then assign it to the textColor property.
To do this you'll need to use the getRed:green:blue:alpha: method on backgroundColor. This will give you values between 0.0 and 1.0 for each component of your colour.
Then use the UIColor class method colorWithRed:green:blue:alpha: to create a new UIColor, but use (1.0 - component value) for each of the components.
Then finally assign your new colour to opacityLabel.textColor
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?
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