preskh
asked on
Automatically resize datagrid column width
How to automatically resize a datgrid's column width to the length of the longest item in a column. How to apply it to this code ?
Dim dTable As New DataTable
dTable.Columns.Add("Name")
dTable.Columns.Add("Inform ation")
Dim myDatatView As New DataView(dTable)
DataGrid1.DataSource = myDatatView
Dim dRow As DataRow
dRow = dTable.NewRow
dRow(0) = "aa"
dRow(1) = "bb"
dTable.Rows.Add(dRow)
dRow = dTable.NewRow
dRow(0) = "123aaaaaaaaaaaaaaaaaaaaa4 56"
dRow(1) = "123bbbbbbbb456"
dTable.Rows.Add(dRow)
Dim dTable As New DataTable
dTable.Columns.Add("Name")
dTable.Columns.Add("Inform
Dim myDatatView As New DataView(dTable)
DataGrid1.DataSource = myDatatView
Dim dRow As DataRow
dRow = dTable.NewRow
dRow(0) = "aa"
dRow(1) = "bb"
dTable.Rows.Add(dRow)
dRow = dTable.NewRow
dRow(0) = "123aaaaaaaaaaaaaaaaaaaaa4
dRow(1) = "123bbbbbbbb456"
dTable.Rows.Add(dRow)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>emoreau I converted the VB 6 code given in your link to VB .Net code
It is surely not VB6! It is C#. You can download the VB.Net equivalent using the "VB" link at the end of the description.
>>and I get this error in the "numRows = CType(dataGrid1.DataSource , DataTable).Rows.Count" line
>>An unhandled exception of type 'System.InvalidCastExcepti on' occurred in WindowsApplication1.exe
What is your DataSource set to? If if is a DataView, then you have to cast it as a DataView instead of a DataTable.
It is surely not VB6! It is C#. You can download the VB.Net equivalent using the "VB" link at the end of the description.
>>and I get this error in the "numRows = CType(dataGrid1.DataSource
>>An unhandled exception of type 'System.InvalidCastExcepti
What is your DataSource set to? If if is a DataView, then you have to cast it as a DataView instead of a DataTable.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
emoreau I converted the VB 6 code given in your link to VB .Net code and I get this error in the "numRows = CType(dataGrid1.DataSource
An unhandled exception of type 'System.InvalidCastExcepti
Additional information: Specified cast is not valid.
Also should I change the 'customers' in the line "dataGrid1.TableStyles("cu