By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,184 Members | 1,500 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,184 IT Pros & Developers. It's quick & easy.

No. Columns in DataGrid

P: n/a
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).Widt h
i=i+1
loop
If intUsedWidth<intTargetWidth then
DataGrid1.TableStyles(st).GridColumnStyles(i).Widt h=intTargetWidth -
intUsedWidth
endif

Thanks

Doug
Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Doug,

Do you use datagridstyles in your datagrid?

Cor
Nov 21 '05 #2

P: n/a
Hi,

http://www.syncfusion.com/FAQ/WinFor...c44c.asp#q871q

Ken
-------------------------
"Doug Bell" <dug@bigpond> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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).Widt h
i=i+1
loop
If intUsedWidth<intTargetWidth then
DataGrid1.TableStyles(st).GridColumnStyles(i).Widt h=intTargetWidth -
intUsedWidth
endif

Thanks

Doug

Nov 21 '05 #3

P: n/a
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

"Cor Ligthert" <no************@planet.nl> wrote in message
news:uE**************@TK2MSFTNGP10.phx.gbl...
Doug,

Do you use datagridstyles in your datagrid?

Cor

Nov 21 '05 #4

P: n/a
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

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:OH**************@TK2MSFTNGP14.phx.gbl...
Hi,

http://www.syncfusion.com/FAQ/WinFor...c44c.asp#q871q

Ken
-------------------------
"Doug Bell" <dug@bigpond> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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).Widt h
i=i+1
loop
If intUsedWidth<intTargetWidth then
DataGrid1.TableStyles(st).GridColumnStyles(i).Widt h=intTargetWidth -
intUsedWidth
endif

Thanks

Doug

Nov 21 '05 #5

P: n/a
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=intTarg etWidth-intUsedWidth
endif

Thanks

Doug

"Doug Bell" <dug@bigpond> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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).Widt h
i=i+1
loop
If intUsedWidth<intTargetWidth then
DataGrid1.TableStyles(st).GridColumnStyles(i).Widt h=intTargetWidth -
intUsedWidth
endif

Thanks

Doug

Nov 21 '05 #6

P: n/a
This might work
NoCols = .TableStyles("mappingname").GridColumnStylesCollec tion.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").GridColumnStylesCollec tion.Count

Doug Bell" wrote:
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=intTarg etWidth-intUsedWidth
endif

Thanks

Doug

"Doug Bell" <dug@bigpond> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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).Widt h
i=i+1
loop
If intUsedWidth<intTargetWidth then
DataGrid1.TableStyles(st).GridColumnStyles(i).Widt h=intTargetWidth -
intUsedWidth
endif

Thanks

Doug


Nov 21 '05 #7

P: n/a

Dough,

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

Cor

"Doug Bell" <dug@bigpond>
..
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

"Cor Ligthert" <no************@planet.nl> wrote in message
news:uE**************@TK2MSFTNGP10.phx.gbl...
Doug,

Do you use datagridstyles in your datagrid?

Cor


Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.