Results 1 to 9 of 9

Thread: Manually populating and finely tweaking a DataGridView

  1. #1

    Thread Starter
    New Member CASchryver's Avatar
    Join Date
    Jul 2009
    Location
    PA
    Posts
    9

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

    Thanks!

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Manually populating and finely tweaking a DataGridView

    I tested your code and didn't have the "M..." as you stated. All the M's are displayed in center of the cells just as intended.
    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 -

  3. #3

    Thread Starter
    New Member CASchryver's Avatar
    Join Date
    Jul 2009
    Location
    PA
    Posts
    9

    Re: Manually populating and finely tweaking a DataGridView

    How big are the cells? What is reporting from the pop-ups? In my example above, only the first column would be showing the "M..." text.

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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 -

  5. #5

    Thread Starter
    New Member CASchryver's Avatar
    Join Date
    Jul 2009
    Location
    PA
    Posts
    9

    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)

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

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

  7. #7

    Thread Starter
    New Member CASchryver's Avatar
    Join Date
    Jul 2009
    Location
    PA
    Posts
    9

    Re: Manually populating and finely tweaking a DataGridView

    I've updated my .NET as much as possible and still get the same results. Odd. However, I can work around that issue.

    How about my original two questions about the internal cell padding and the method to globally set the height and width of the cells? Anyone?

  8. #8

    Thread Starter
    New Member CASchryver's Avatar
    Join Date
    Jul 2009
    Location
    PA
    Posts
    9

    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?

  9. #9

    Thread Starter
    New Member CASchryver's Avatar
    Join Date
    Jul 2009
    Location
    PA
    Posts
    9

    Re: Manually populating and finely tweaking a DataGridView

    Wow, found that, too.
    Code:
    DataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True
    I don't think it actually gets rid of the "..." but tells it not to push things off to the left.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width