Results 1 to 5 of 5

Thread: [RESOLVED] The problem with name and size column of DGV

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    Resolved [RESOLVED] The problem with name and size column of DGV

    I using this code to load data from db m s access
    and another code of DGV to give name to column and size
    how can I give name and size to column of DGV directly from the window properties of DGV in the list of project?
    by the way i tried from column collection by put the same name of coulmn in code but didn't work.

    Code:
    Public Sub LoadAllDa()
            conn.Open()
            Dim dp As New OleDbDataAdapter("select Mate,Knd1,Knd2 from Table1 ", conn)
            Dim tbl As New DataTable
            dp.Fill(tbl)
            Form1.DataGridView1.DataSource = tbl
            conn.Close()
        End Sub
    
    I call it
     LoadAllDa()
     DGV1()
    
    Public Sub DGV1()
                     
                .DataGridView1.Columns(0).HeaderText = "Material"
                .DataGridView1.Columns(0).Width = 165
                .DataGridView1.Columns(1).HeaderText = "Kind1"
                .DataGridView1.Columns(1).Width = 130
                .DataGridView1.Columns(2).HeaderText = "Kind2"
                .DataGridView1.Columns(2).Width = 130
     End Sub
    Attached Images Attached Images  
    Last edited by nader; Jul 11th, 2020 at 06:17 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: The problem with name and size column of DGV

    Just as you could not set properties of a control at design time if you did not add the control at design time, you cannot set properties of a column if you do not add that column at design time. Once you have added the grid, select it and open the Properties window, select the Columns property and click the (...) button to open the Edit Columns dialogue. There you can add a columns of the appropriate type and configure them just as you would a control. Be sure to set the DataPropertyName of each grid column, to specify what data source column they should bind to. If you don't, new columns will be added automatically when you bind. The DataPropertyName should be set to a String containing the name of the data source property/column, i.e. "Mate", "Knd1" and "Knd2" in your case.

  3. #3
    Lively Member
    Join Date
    Jan 2020
    Posts
    120

    Re: The problem with name and size column of DGV

    Hello,@nader

    Please try this code,To The problem with name and size column of DGV

    You could set the width of the abbrev column to a fixed pixel width, then set the width of the description column to the width of the DataGridView, minus the sum of the widths of the other columns and some extra margin (if you want to prevent a horizontal scrollbar from appearing on the DataGridView):

    Code:
    dataGridView1.Columns[1].Width = 108;  // or whatever width works well for abbrev
    dataGridView1.Columns[2].Width = 
        dataGridView1.Width 
        - dataGridView1.Columns[0].Width 
        - dataGridView1.Columns[1].Width 
        - 72;  // this is an extra "margin" number of pixels
    If you wanted the description column to always take up the "remainder" of the width of the DataGridView, you could put something like the above code in a Resize event handler of the DataGridView.

    I hope this information will be useful for you.
    Thank you.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: The problem with name and size column of DGV

    Quote Originally Posted by Prahlad View Post
    Hello,@nader

    Please try this code,To The problem with name and size column of DGV

    You could set the width of the abbrev column to a fixed pixel width, then set the width of the description column to the width of the DataGridView, minus the sum of the widths of the other columns and some extra margin (if you want to prevent a horizontal scrollbar from appearing on the DataGridView):

    Code:
    dataGridView1.Columns[1].Width = 108;  // or whatever width works well for abbrev
    dataGridView1.Columns[2].Width = 
        dataGridView1.Width 
        - dataGridView1.Columns[0].Width 
        - dataGridView1.Columns[1].Width 
        - 72;  // this is an extra "margin" number of pixels
    If you wanted the description column to always take up the "remainder" of the width of the DataGridView, you could put something like the above code in a Resize event handler of the DataGridView.

    I hope this information will be useful for you.
    Thank you.
    The question specifically asked how to set column properties at design time. Also, if you want the last column to fill the rest of the available space then you should set its AutoSizeMode to Fill.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    Re: [RESOLVED] The problem with name and size column of DGV

    thank you for help

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