Results 1 to 8 of 8

Thread: [RESOLVED] datagridview column formatting

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Resolved [RESOLVED] datagridview column formatting

    hi guru's Im trying to format datagridview column to a checkbox as its column is assigned to msaccess YES/NO data type.

    vb1 Code:
    1. DataGridView.Columns(2).DefaultCellStyle.Format = "Bit"

    AND

    vb2 Code:
    1. DataGridView.Columns(2).DefaultCellStyle.Format = "Boolean"

    is there anyway to do this. thanks.

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: datagridview column formatting

    Use a DataGridViewCheckBoxColumn

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: datagridview column formatting

    If you simply populate a DataTable from your database and bind that to the grid then it will use a check box column automatically. Any particular reason you're not doing that?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: datagridview column formatting

    Quote Originally Posted by jmcilhinney View Post
    If you simply populate a DataTable from your database and bind that to the grid then it will use a check box column automatically. Any particular reason you're not doing that?

    I agree unless there something wrong in the code.


    Code:
                With _grid
                    .DataSource = Nothing
    
                    .DataSource = dtDataGrid
    
                    'hide column 0 
                    .Columns(0).Visible = False
    
                    .SelectionMode = DataGridViewSelectionMode.FullRowSelect
                    .Cursor = Cursors.Hand
    
                    If _readonly Then
                        .ReadOnly = True
                        .AllowUserToAddRows = False
                    End If
                End With
    Last edited by jlbantang; Jul 8th, 2009 at 07:34 AM.

  5. #5
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: datagridview column formatting

    otherwise just create a checkboxcolumn and bind it to the appropriate column:

    Code:
    Dim checkboxcolumn As New DataGridViewCheckBoxColumn
    
    checkboxcolumn.HeaderText = "Test"
                checkboxcolumn.DataPropertyName = "TestColumn"
                checkboxcolumn.DisplayIndex = 3
    
                With dgvTest
                    .Columns.Add(checkboxcolumn)
                end with

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: datagridview column formatting

    Nitesh: the code work indeed as it ADD a column with checkbox format but it doesnt solve the issue. I just want the column to be formatted as checkbox.

    i did it like this:
    Code:
            Dim checkboxcolumn As New DataGridViewCheckBoxColumn
    
            borrowerloanDataGridView.Columns(2).DefaultCellStyle = checkboxcolumn
    obviously it has convertion error
    Last edited by jlbantang; Jul 9th, 2009 at 04:43 AM.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: datagridview column formatting

    There's no such thing as "formatting a column as checkbox". If you want a column of check boxes then you use a DataGridViewCheckBoxColumn, plain and simple. You can't use some other type of column, e.g. a DataGridViewTextBoxColumn, and then somehow change its type to display check boxes. Each different type of column is designed to host a different type of control. If you want check boxes you use a DataGridViewCheckBoxColumn.

    The DefaultCellStyle has absolutely nothing to do with this. The control hosted in the cells of a column is nothing to do with format.

    There's also no reason for you to have any code for this. You should be adding the column in the designer. If you need to bind it to a column or property in the grid's DataSource then you need to set the column's DataPropertyName.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: datagridview column formatting

    Right, and i found out it was TOTALLY a WRONG column passed in sql-select.

    OMG!!!

    Thanks anyway.

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