Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. resizing the width of columns in datagrid with datatable

resizing the width of columns in datagrid with datatable

Scheduled Pinned Locked Moved Visual Basic
helpquestion
9 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pravin H
    wrote on last edited by
    #1

    Hello I want to set the width of each columns in datagrid separately but not able to do that. I am using datagrid and datatable in my project the codes are as follows. Dim Mytable as new datatable Mytable.columns.add(“srno”, gettype, string)) Mytable.columns.add(“name”, gettype,( string)) Mytable.columns.add(“designation”, gettype,( string)) Mytable.columns.add(“remarks”, gettype,( string)) Datagrid1.datasourse = mytable datagrid1.preferredcoloumswidth = 74 Now my problem is all the coloumns are OF the same width, as I wanted different width of different column. As of now all the columns have the same width whereas I don’t need so much width for the SRNO and need more wider colums in REMARKS. Please suggest how can I solve this problem. Thanks alot

    D 1 Reply Last reply
    0
    • P Pravin H

      Hello I want to set the width of each columns in datagrid separately but not able to do that. I am using datagrid and datatable in my project the codes are as follows. Dim Mytable as new datatable Mytable.columns.add(“srno”, gettype, string)) Mytable.columns.add(“name”, gettype,( string)) Mytable.columns.add(“designation”, gettype,( string)) Mytable.columns.add(“remarks”, gettype,( string)) Datagrid1.datasourse = mytable datagrid1.preferredcoloumswidth = 74 Now my problem is all the coloumns are OF the same width, as I wanted different width of different column. As of now all the columns have the same width whereas I don’t need so much width for the SRNO and need more wider colums in REMARKS. Please suggest how can I solve this problem. Thanks alot

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You have to modify the DataGrids TableStyles collection to get at the column widths.

      Dim ts As DataGridTableStyle = DataGrid1.TableStyles("columnName")
      ts.Width = someValue

      RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      P 1 Reply Last reply
      0
      • D Dave Kreskowiak

        You have to modify the DataGrids TableStyles collection to get at the column widths.

        Dim ts As DataGridTableStyle = DataGrid1.TableStyles("columnName")
        ts.Width = someValue

        RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        P Offline
        P Offline
        Pravin H
        wrote on last edited by
        #3

        I tried the one you suggested Dim ts as datagridtablestyle = datagrid1.tablestyles(“REMARKS”) Ts.preferredcoloumns = 100 I HAD THIS ERRORS “Object reference not set to an instance of an object” Kindly check pls. Thanks alot for your time.

        D 1 Reply Last reply
        0
        • P Pravin H

          I tried the one you suggested Dim ts as datagridtablestyle = datagrid1.tablestyles(“REMARKS”) Ts.preferredcoloumns = 100 I HAD THIS ERRORS “Object reference not set to an instance of an object” Kindly check pls. Thanks alot for your time.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Sorry, I had rectal-cranial inversion...

          DataGrid1.DataSource = ds.Tables("MyTable")
          Dim ts As New DataGridTableStyle
          ts.MappingName = "MyTable"
          DataGrid1.TableStyles.Add(ts)
          ts.GridColumnStyles(0).Width = 15
          

          RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

          P 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Sorry, I had rectal-cranial inversion...

            DataGrid1.DataSource = ds.Tables("MyTable")
            Dim ts As New DataGridTableStyle
            ts.MappingName = "MyTable"
            DataGrid1.TableStyles.Add(ts)
            ts.GridColumnStyles(0).Width = 15
            

            RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            P Offline
            P Offline
            Pravin H
            wrote on last edited by
            #5

            Sorry but it still has erros I tried you codes but still I have one more error sending you my declarations DECLARATION Dim mytable As New DataTable Dim ds As New DataSet ON FORM LOAD mytable.Columns.Add(New DataColumn("ContactPerson", GetType(String))) mytable.Columns.Add(New DataColumn("DESIGNATION", GetType(String))) mytable.Columns.Add(New DataColumn("PHONENO", GetType(String))) mytable.Columns.Add(New DataColumn("EXTN", GetType(String))) mytable.Columns.Add(New DataColumn("CELLNO", GetType(String))) mytable.Columns.Add(New DataColumn("SALESMAN", GetType(String))) mytable.Columns.Add(New DataColumn("REMARKS", GetType(String))) Me.DataGrid1.DataSource = ds.Tables("mytable") Dim ts As New DataGridTableStyle ts.MappingName = "mytable" DataGrid1.TableStyles.Add(ts) Me.DataGrid1.DataSource = mytable ts.GridColumnStyles(0).Width = 100 ERROS IS HERE (index is out of range: MUST BE NEGATIVE AND LESS THAN THE SIZE OF THE COLLECTIONS) Thanks again for you help and you Suggestionss Thanks again Regards :(

            D 1 Reply Last reply
            0
            • P Pravin H

              Sorry but it still has erros I tried you codes but still I have one more error sending you my declarations DECLARATION Dim mytable As New DataTable Dim ds As New DataSet ON FORM LOAD mytable.Columns.Add(New DataColumn("ContactPerson", GetType(String))) mytable.Columns.Add(New DataColumn("DESIGNATION", GetType(String))) mytable.Columns.Add(New DataColumn("PHONENO", GetType(String))) mytable.Columns.Add(New DataColumn("EXTN", GetType(String))) mytable.Columns.Add(New DataColumn("CELLNO", GetType(String))) mytable.Columns.Add(New DataColumn("SALESMAN", GetType(String))) mytable.Columns.Add(New DataColumn("REMARKS", GetType(String))) Me.DataGrid1.DataSource = ds.Tables("mytable") Dim ts As New DataGridTableStyle ts.MappingName = "mytable" DataGrid1.TableStyles.Add(ts) Me.DataGrid1.DataSource = mytable ts.GridColumnStyles(0).Width = 100 ERROS IS HERE (index is out of range: MUST BE NEGATIVE AND LESS THAN THE SIZE OF THE COLLECTIONS) Thanks again for you help and you Suggestionss Thanks again Regards :(

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              It doesn't work because you reset the DataSource of the DataGrid, wiping out all the changes made to the TableStyles. Why are you adding all the Columns? You really don't need to unless your going to customize the look of them, which you're not doing in your code.

              ' I hope your DataSet actuall yhas a table in it called mytable.
              ' If not, this won't work. Change this to reflect the name of YOUR
              ' table in your DataSet.
              DataGrid1.DataSource = ds.Tables("mytable")
              Dim ts As New DataGridTableStyle
              ts.MappingName = "mytable" ' Change this to match the name of the table in your DataSet
              DataGrid1.TableStyles.Add(ts)
              ts.GridColumnStyles(0).Width = 100

              RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

              P 1 Reply Last reply
              0
              • D Dave Kreskowiak

                It doesn't work because you reset the DataSource of the DataGrid, wiping out all the changes made to the TableStyles. Why are you adding all the Columns? You really don't need to unless your going to customize the look of them, which you're not doing in your code.

                ' I hope your DataSet actuall yhas a table in it called mytable.
                ' If not, this won't work. Change this to reflect the name of YOUR
                ' table in your DataSet.
                DataGrid1.DataSource = ds.Tables("mytable")
                Dim ts As New DataGridTableStyle
                ts.MappingName = "mytable" ' Change this to match the name of the table in your DataSet
                DataGrid1.TableStyles.Add(ts)
                ts.GridColumnStyles(0).Width = 100

                RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                P Offline
                P Offline
                Pravin H
                wrote on last edited by
                #7

                Finally I was able to resize it, Thanks for your Help and Support The codes are as Under for reference Dim mytable As DataTable mytable = New DataTable("MAIN") mytable.Columns.Add(New DataColumn("ContactPerson",GetType(String))) mytable.Columns.Add(New DataColumn("DESIGNATION",GetType(String))) mytable.Columns.Add(New DataColumn("PHONENO",GetType(String))) DataGrid1.DataSource = mytable Dim TS As New DataGridTableStyle TS.MappingName = "MAIN" DataGrid1.TableStyles.Add(TS) TS.GridColumnStyles.Item(1).Width = 100 TS.GridColumnStyles.Item(0).Width = 100 Thanks Again for your time :-D

                D 1 Reply Last reply
                0
                • P Pravin H

                  Finally I was able to resize it, Thanks for your Help and Support The codes are as Under for reference Dim mytable As DataTable mytable = New DataTable("MAIN") mytable.Columns.Add(New DataColumn("ContactPerson",GetType(String))) mytable.Columns.Add(New DataColumn("DESIGNATION",GetType(String))) mytable.Columns.Add(New DataColumn("PHONENO",GetType(String))) DataGrid1.DataSource = mytable Dim TS As New DataGridTableStyle TS.MappingName = "MAIN" DataGrid1.TableStyles.Add(TS) TS.GridColumnStyles.Item(1).Width = 100 TS.GridColumnStyles.Item(0).Width = 100 Thanks Again for your time :-D

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  Pravin Hegde wrote:

                  The codes are as Under for reference

                  Precisely why you keep it in the forums! RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                  P 1 Reply Last reply
                  0
                  • D Dave Kreskowiak

                    Pravin Hegde wrote:

                    The codes are as Under for reference

                    Precisely why you keep it in the forums! RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                    P Offline
                    P Offline
                    Pravin H
                    wrote on last edited by
                    #9

                    So that if anyone has the same problem he can get an idea to how to do it !!! Just a helping hand like you which you always do.:cool: Thanks

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups