473,473 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

No. Columns in DataGrid

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
7 1435
Doug,

Do you use datagridstyles in your datagrid?

Cor
Nov 21 '05 #2
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
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
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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Billy Jacobs | last post by:
I am using a datagrid to display some data. I need to create 2 header rows for this grid with columns of varying spans. In html it would be the following. <Table> <tr> <td colspan=8>Official...
3
by: Diego TERCERO | last post by:
Hi... I'm working on a tool for editing text resources for a family of software product my company produces. These text resources are found in a SQL Server database, in a table called...
2
by: Kevin | last post by:
Hi Al Can someone tell me how to hide colums in a datagrid. I have a one datagrid that is a master and child and I would like to hide specific columns, how do I do this TI Kevin
1
by: Amber | last post by:
The DataGrid allows you to make columns visible or invisible on demand - even edit and other special columns. This article will show you how it is done. Some developers have reported problems...
1
by: J.B | last post by:
I have a datagrid that will display different datasets, (it runs different sprocs based on a value in the querystring) but it will always return 6 columns. The 5th and 6th columns are always Date...
6
by: John Ruiz | last post by:
Greetings, I originally posted this to microsoft.public.dotnet.framework.aspnet.datagridcontrol two weeks ago, but no one was able to answer. I am unable to dynamically add columns to a...
3
by: Allen K | last post by:
Hi, I'm programmatically creating a HyperLink column for my datagrd ( in addition to Bound Columns created through the VS.NET Visual Interface ). However, when a Postback occurs, this column...
8
by: Hanson | last post by:
I have a datagrid control in an aspx page. The datagrid columns will be dynamically generated according to the pre-page's search condition, I want to hide some columns in the datagrid. is that...
2
by: CSL | last post by:
I am using the DataGrid in a Windows Application, how can I adjust the widths of each column individually.
0
by: tafpin | last post by:
I have an application with a datagrid. In the IDE I have 2 template columns. The first has an image button and the second contains a link button. According to the results that I get back I must...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.