Manually populating and finely tweaking a DataGridView
Yet another newbie question...
I've been playing with a DGV. Let's say for my question here, I want a grid that is 10x10. I would like the font in each cell to be Arial 10 regular. Only one character will be in each cell (I'll use "M"). I would LIKE the cells to be 17 pixels by 17 pixels. But, if I manually force the column width to be 17, I get about half of my M and three dots "M...". I'm thinking this is a padding thing but as far as I can tell, everything is set to 0. See code and then more comments below:
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With DataGridView1
.ColumnHeadersVisible = False
.RowHeadersVisible = False
.AllowUserToResizeColumns = False
.AllowUserToResizeRows = False
.ColumnCount = 10
.RowCount = 10
'.RowTemplate.Height = 17 'this doesn't seem to do anything
.EditMode = DataGridViewEditMode.EditProgrammatically
.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells)
.DefaultCellStyle.Font = New Font("Arial", 10, FontStyle.Regular)
.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
.DefaultCellStyle.SelectionBackColor = Color.White
.DefaultCellStyle.SelectionForeColor = Color.Black
End With
Dim iR, iC As Integer
For iR = 0 To 9
For iC = 0 To 9
DataGridView1(iC, iR).Value = "M"
Next
Next
Dim rowinstance As DataGridViewRow = DataGridView1.Rows(0) 'How do I reference all rows?
rowinstance.Height = 17
Dim colinstance As DataGridViewColumn = DataGridView1.Columns(0) 'How do I reference all columns?
colinstance.Width = 17 'if I make this less than 20, it messes up the contents.
MsgBox("Rows: " & DataGridView1.Rows(1).Height)
MsgBox("Columns: " & DataGridView1.Columns(1).Width)
End Sub
Question 1: How can I reclaim some of what seems to be lost space around the cell contents so I don't have to make them bigger than they need to be? I'd like them to be 17x17 with the single character centered within.
Question 2: Do I have to manually set the height and width of each row and column? I'm thinking the RowTemplate setting but can't seem to get it to work.
FYI: This is a new project with a DGV dragged onto the form. All changes have been made programatically.
Re: Manually populating and finely tweaking a DataGridView
I copied and paste your code as is... No modification whatsoever. Columns width = 21, row height = 20. All the M's displayed neatly in center on the cells. No "M..."
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it. - Abraham Lincoln -
Re: Manually populating and finely tweaking a DataGridView
Thanks for checking that out for me. As far as I know, I have a standard installation of VB 2008. Is there any kind of default global setting that either you or I might have in place that is making us get different results? Something else like installed fonts? (Although, I'm sure Ariel is pretty standard)
Re: Manually populating and finely tweaking a DataGridView
This is what I got... I'm using VS 2008 professional version, but I don't think the version would matter. If anything matters, it more likely the .net framework updates, and I have all the latest updates installed.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it. - Abraham Lincoln -
Re: Manually populating and finely tweaking a DataGridView
Okay, I've figured out the internal cell padding bit.
Code:
DataGridView1.DefaultCellStyle.Padding = New Padding(0, 0, 0, 0)
Although, this does leave a bit of space still between the cell borders and the actual contents.
And, I've just decided to loop through each row and column and set the height and width.
Code:
Dim iR, iC As Integer
For iR = 0 To 9
DataGridView1.Rows(iR).Height = 17
Next
For iC = 0 To 9
DataGridView1.Columns(iC).Width = 17
Next
So my next question would be how do I tell it not to put the "..." at the end of text that is too long. I understand why it's there, but can I override it?