witdh of Dynamically generated BoundedC olum
-
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
-
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
-
Hi try this code
Dim bc As New BoundColumn bc.ItemStyle.Width = New Unit(100)
bye timcyspetHi, 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
-
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
I wonder if it is possible...! It'll create zig zag table, i neither see nor tried ever UTSAV
-
I wonder if it is possible...! It'll create zig zag table, i neither see nor tried ever UTSAV
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.
-
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
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 -
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 timcyspetHi, 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
-
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
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 -
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 timcyspetHi, 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
-
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
-
Hi if it is non string column then pls ignore it it will automatically adject that bye timcyspet
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