No. Columns in DataGrid

D

Doug Bell

Hi,

How can I determine the number of columns in a DataGrid?

What I am trying to do is to build a function that will resize the last
column to fill the remaining space in the DataGrid.
Something like:?

Dim intCols As Integer = DataGrid1.DataSource.Columns.Count
Dim intUsedWidth as Integer=0
Dim i as Integer = 0
Dim intTargetWidth as Integer = DataGrid1.Width

For Each ctrl AS Control In Me.DataGrid1
If TypeOf ctrl Is VScrollBar then
If ctrl.Visible then
intTargetWidth =intTargetWidth - ctrl.Width
EndIf
EndIf
Next

Do While i< intCols
intUsedWidth=intUsedWidth +
DataGrid1.TableStyles(st).GridColumnStyles(i).Width
i=i+1
loop
If intUsedWidth<intTargetWidth then
DataGrid1.TableStyles(st).GridColumnStyles(i).Width=intTargetWidth -
intUsedWidth
endif

Thanks

Doug
 
K

Ken Tucker [MVP]

Hi,

http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q871q

Ken
-------------------------
Hi,

How can I determine the number of columns in a DataGrid?

What I am trying to do is to build a function that will resize the last
column to fill the remaining space in the DataGrid.
Something like:?

Dim intCols As Integer = DataGrid1.DataSource.Columns.Count
Dim intUsedWidth as Integer=0
Dim i as Integer = 0
Dim intTargetWidth as Integer = DataGrid1.Width

For Each ctrl AS Control In Me.DataGrid1
If TypeOf ctrl Is VScrollBar then
If ctrl.Visible then
intTargetWidth =intTargetWidth - ctrl.Width
EndIf
EndIf
Next

Do While i< intCols
intUsedWidth=intUsedWidth +
DataGrid1.TableStyles(st).GridColumnStyles(i).Width
i=i+1
loop
If intUsedWidth<intTargetWidth then
DataGrid1.TableStyles(st).GridColumnStyles(i).Width=intTargetWidth -
intUsedWidth
endif

Thanks

Doug
 
D

Doug Bell

Yes Cor,
This DataGrid has 2 DataGrid Styles based on 2 DataViews (2 DataTables)

I was trying to build some code that could be re-usable.

Doug
 
D

Doug Bell

Thanks Ken,
I had already seen this site, which is very useful, However I downloaded the
VB but could not get it to work with my DataGrid.

It determines the number of columns with:
dataTable1 = CType(Me.dataGrid1.DataSource, DataTable)

numCols = dataTable1.Columns.Count

I get an unhandled exception on the first line, of type
'System.InvalidCastException'
'Specified 'ast is not valid'

My DataGrid is based on DataViews (with filter and sort) not directly on a
DataTable.
So I guess, I am really trying to determine the number of columns in the
particular DataView.
Problem is that the DataView doesn't have a columns property.


Also in that example, it used
scrollBarWidth = IIf(Me.dataGrid1.VerticalScrollBarVisible,
SystemInformation.VerticalScrollBarWidth, 0)

To return the width of the Vertical ScrollBar. I could not get a
VerticalScrollBar property so used your code.

Doug
 
D

Doug Bell

Ken and Cor thanks for your help.

I solved it using:

dv1=CType(.DataSource, DataView)
intNumCols=dv1.Table.Columns.Count

st=dv1.Table.TableName.ToString 'My DataViews are give the Table Name

'This is a bit of a problem as the code can not be universal

Do while i <intNumCols
intUsedWidth=intUsedWidth + .TableStyles(st).GridColumnStyle(i).Width
i=i+1
Loop

If intUsedWidth<intTargetWidth then
.TableStyles(st).GridColumnStyles(i).Width=intTargetWidth-intUsedWidth
endif

Thanks

Doug
 
G

Guest

This might work
NoCols = .TableStyles("mappingname").GridColumnStylesCollection.Count

If it doesn't, then you might have to create a TableStyle then the DataGrid
will fill in a column style for each column;

Dim tab As DataGridTableStyle = New DataGridTableStyle
' Set DataGridTableStyle.MappingName property to table in the source to map
to.
tab.MappingName="xxxxxx"
' Add it to the datagrid's TableStyles collection
..Me.TableStyles.Add(tso)

NoCols = .TableStyles("mappingname").GridColumnStylesCollection.Count
 
C

Cor Ligthert

Dough,

I did not give an answer however it would probably have been the same as
from Dennis

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top