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. Web Development
  3. ASP.NET
  4. witdh of Dynamically generated BoundedC olum

witdh of Dynamically generated BoundedC olum

Scheduled Pinned Locked Moved ASP.NET
question
11 Posts 4 Posters 0 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.
  • U Offline
    U Offline
    User 1804931
    wrote on last edited by
    #1

    Hi, I am creating BoundedColumns in a Datagrid dynamically, for this I am just passing the table object to a method which creates bounded columns. In this case how can I set the width of the BoundedColumns, because the default length of the each cell is equals to the length of the column headers text. How can I change this according to the maximum length of the text in that particular column? Regards, Chakravarthy.V

    T 1 Reply Last reply
    0
    • U User 1804931

      Hi, I am creating BoundedColumns in a Datagrid dynamically, for this I am just passing the table object to a method which creates bounded columns. In this case how can I set the width of the BoundedColumns, because the default length of the each cell is equals to the length of the column headers text. How can I change this according to the maximum length of the text in that particular column? Regards, Chakravarthy.V

      T Offline
      T Offline
      Timcyspet
      wrote on last edited by
      #2

      Hi try this code Dim bc As New BoundColumn bc.ItemStyle.Width = New Unit(100) bye timcyspet

      U 1 Reply Last reply
      0
      • T Timcyspet

        Hi try this code Dim bc As New BoundColumn bc.ItemStyle.Width = New Unit(100) bye timcyspet

        U Offline
        U Offline
        User 1804931
        wrote on last edited by
        #3

        Hi, It is not working, this code will set all the bounded columns with same width, but I want to set the each bound column width depending on the maximum text length of the that perticular column in the Database. Regards, Chakravarthy.V

        U T 2 Replies Last reply
        0
        • U User 1804931

          Hi, It is not working, this code will set all the bounded columns with same width, but I want to set the each bound column width depending on the maximum text length of the that perticular column in the Database. Regards, Chakravarthy.V

          U Offline
          U Offline
          utsav_verma
          wrote on last edited by
          #4

          I wonder if it is possible...! It'll create zig zag table, i neither see nor tried ever UTSAV

          L 1 Reply Last reply
          0
          • U utsav_verma

            I wonder if it is possible...! It'll create zig zag table, i neither see nor tried ever UTSAV

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Hi, You are mistaken, what I need is. Suppose there are 2 columns in table Emp i.e Empnumber, Empname. If the data is like is, Empnumber Empname 1 XYZ 2 ABC 12200 ABCDEFGHIJ In this case, the first boundColumn length is of length '12200' i.e all cells of employees column length is of width of '12200'. In the same way for empname also.

            1 Reply Last reply
            0
            • U User 1804931

              Hi, It is not working, this code will set all the bounded columns with same width, but I want to set the each bound column width depending on the maximum text length of the that perticular column in the Database. Regards, Chakravarthy.V

              T Offline
              T Offline
              Timcyspet
              wrote on last edited by
              #6

              Hi what send is a working code DataGrid1.AutoGenerateColumns = False **Dim bc As New BoundColumn bc.ItemStyle.Width = New Unit(200) Dim bc1 As New BoundColumn bc1.ItemStyle.Width = New Unit(100)** Dim con As New SqlClient.SqlConnection("connection string") Dim com As New SqlClient.SqlDataAdapter("Select top 10 loginid,password from users", con) Dim Ds As New DataSet com.Fill(Ds) bc.HeaderText = "Login Id" bc.DataField = "loginid" bc1.HeaderText = "Passord" bc1.DataField = "Password" DataGrid1.Columns.Add(bc) DataGrid1.Columns.Add(bc1) DataGrid1.DataSource = Ds DataGrid1.DataBind() bye timcyspet

              U 1 Reply Last reply
              0
              • T Timcyspet

                Hi what send is a working code DataGrid1.AutoGenerateColumns = False **Dim bc As New BoundColumn bc.ItemStyle.Width = New Unit(200) Dim bc1 As New BoundColumn bc1.ItemStyle.Width = New Unit(100)** Dim con As New SqlClient.SqlConnection("connection string") Dim com As New SqlClient.SqlDataAdapter("Select top 10 loginid,password from users", con) Dim Ds As New DataSet com.Fill(Ds) bc.HeaderText = "Login Id" bc.DataField = "loginid" bc1.HeaderText = "Passord" bc1.DataField = "Password" DataGrid1.Columns.Add(bc) DataGrid1.Columns.Add(bc1) DataGrid1.DataSource = Ds DataGrid1.DataBind() bye timcyspet

                U Offline
                U Offline
                User 1804931
                wrote on last edited by
                #7

                Hi, I tried the code that you send but it is not working, my actual code is like this. public void BoundColumns(DataTable table) { foreach(DataColumn c in table.Columns) { this.Columns.Add(CreateBoundColumn(c)); } } private BoundColumn CreateBoundColumn(DataColumn c) { BoundColumn column = new BoundColumn(); column.DataField = c.ColumnName; column.ItemStyle.Width = New Unit(200) column.HeaderText = c.ColumnName.Replace("_"," "); column.DataFormatString =setFormating(c); column.SortExpression=c.ColumnName; return column; } The problem with this code is all columns will have same length irrespecive of length. For ex the Employee id may have 5 char, Empname may have 50 char length, but in datagrid these two col will have same length and at design time we dont know the length of the columns. What i required is the record which is having maximum length in a perticular column, how to find this while databinding. Regards, Chakravarthy.V

                T 1 Reply Last reply
                0
                • U User 1804931

                  Hi, I tried the code that you send but it is not working, my actual code is like this. public void BoundColumns(DataTable table) { foreach(DataColumn c in table.Columns) { this.Columns.Add(CreateBoundColumn(c)); } } private BoundColumn CreateBoundColumn(DataColumn c) { BoundColumn column = new BoundColumn(); column.DataField = c.ColumnName; column.ItemStyle.Width = New Unit(200) column.HeaderText = c.ColumnName.Replace("_"," "); column.DataFormatString =setFormating(c); column.SortExpression=c.ColumnName; return column; } The problem with this code is all columns will have same length irrespecive of length. For ex the Employee id may have 5 char, Empname may have 50 char length, but in datagrid these two col will have same length and at design time we dont know the length of the columns. What i required is the record which is having maximum length in a perticular column, how to find this while databinding. Regards, Chakravarthy.V

                  T Offline
                  T Offline
                  Timcyspet
                  wrote on last edited by
                  #8

                  Hi try this Function dgbind() DataGrid1.AutoGenerateColumns = False Dim con As New SqlClient.SqlConnection("connection str") Dim com As New SqlClient.SqlDataAdapter("Select top 10 LoginId,Password,Category from users", con) Dim Ds As New DataSet com.Fill(Ds) Dim dc As DataColumn For Each dc In Ds.Tables(0).Columns dc.MaxLength = maxSize(dc.ColumnName, Ds.Tables(0)) DataGrid1.Columns.Add(createBC(dc)) Next DataGrid1.DataSource = Ds DataGrid1.DataBind() End Function Function maxSize(ByVal str As String, ByVal dt As DataTable) As Integer Dim row As DataRow Dim i As Integer = 0 For Each row In dt.Rows If i < Len(CStr(row.Item(str))) Then i = Len(CStr(row.Item(str))) End If Next Return i End Function Function createBC(ByVal dc As DataColumn) As BoundColumn Dim bc As New BoundColumn Response.Write(dc.ColumnName) bc.ItemStyle.Width = New Unit(dc.MaxLength) bc.DataField = dc.ColumnName Return bc End Function Bye timcyspet

                  U 1 Reply Last reply
                  0
                  • T Timcyspet

                    Hi try this Function dgbind() DataGrid1.AutoGenerateColumns = False Dim con As New SqlClient.SqlConnection("connection str") Dim com As New SqlClient.SqlDataAdapter("Select top 10 LoginId,Password,Category from users", con) Dim Ds As New DataSet com.Fill(Ds) Dim dc As DataColumn For Each dc In Ds.Tables(0).Columns dc.MaxLength = maxSize(dc.ColumnName, Ds.Tables(0)) DataGrid1.Columns.Add(createBC(dc)) Next DataGrid1.DataSource = Ds DataGrid1.DataBind() End Function Function maxSize(ByVal str As String, ByVal dt As DataTable) As Integer Dim row As DataRow Dim i As Integer = 0 For Each row In dt.Rows If i < Len(CStr(row.Item(str))) Then i = Len(CStr(row.Item(str))) End If Next Return i End Function Function createBC(ByVal dc As DataColumn) As BoundColumn Dim bc As New BoundColumn Response.Write(dc.ColumnName) bc.ItemStyle.Width = New Unit(dc.MaxLength) bc.DataField = dc.ColumnName Return bc End Function Bye timcyspet

                    U Offline
                    U Offline
                    User 1804931
                    wrote on last edited by
                    #9

                    Hi, Thanks for your reply, but I am getting the following error. MaxLength applies to string data type only. You cannot set Column 'EmployeeID' property MaxLength to be non-negative number. Regards, Chakravarthy.V

                    T 1 Reply Last reply
                    0
                    • U User 1804931

                      Hi, Thanks for your reply, but I am getting the following error. MaxLength applies to string data type only. You cannot set Column 'EmployeeID' property MaxLength to be non-negative number. Regards, Chakravarthy.V

                      T Offline
                      T Offline
                      Timcyspet
                      wrote on last edited by
                      #10

                      Hi if it is non string column then pls ignore it it will automatically adject that bye timcyspet

                      U 1 Reply Last reply
                      0
                      • T Timcyspet

                        Hi if it is non string column then pls ignore it it will automatically adject that bye timcyspet

                        U Offline
                        U Offline
                        User 1804931
                        wrote on last edited by
                        #11

                        Hi, Sorry for disturbing again, I excluded non string columns, still the text in the columns are not displaying properly i.e. text is not displaying in single line. . I didn’t understand this unusual behavior. Regards, Chakravrthy.v

                        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