Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

column/row count in a datagrid on a winform

Status
Not open for further replies.

Plato2

Programmer
Dec 6, 2002
192
US
how can I get the number of columns and rows in a datagrid, loadded from an xml file?
 
Assuming the user has not deleted or added any rows using the datagrid, you can access the grid's datasource and get the values:
Code:
Dim dt As DataTable = CType(dg.DataSource, DataTable)

MessageBox.Show("Rows: " & dt.Rows.Count.ToString)

MessageBox.Show("Columns: " & dt.Columns.Count.ToString)
If the user has added or deleted, you need to get the data view that the grid is displaying, which is now different to the data source because rows have been modified:
Code:
Dim cm As CurrencyManager = CType(dg.BindingContext(dg.DataSource, dg.DataMember), CurrencyManager)

Dim dv As DataView = CType(cm.List, DataView)

MessageBox.Show("Rows: " & dv.Count.ToString)

MessageBox.Show("Columns: " & dv.Table.Columns.Count.ToString)
 
Oops, ignore the second part of my post - that doesn't apply to XML loaded data. Sorry...
 
I got an error on that line - Invalid Casing

Dim dt As DataTable = CType(DataGrid1.DataSource, DataTable)
 
Not doing very well, am I ?

I guess you're binding a dataset to the grid, rather than a datatable, so you need to change the code to:
Code:
Dim dt As DataTable = CType(dg.DataSource, DataSet).Tables(0)
assuming you have only one table in your XML structure.
 
you could always cheat and loop through the datagrid...

using an try/catch loop... not the prettiest way but it will work


Dim boolTest As Boolean = True
Dim DatagridCounter As Integer = 0

DatagridCounter = 0
Do While boolTest
Try
'something that increments the rowindex
myDG.Item(DataGridCounter, 0) = 0
Catch
boolTest = False
End Try
DatagridCounter += 1
Loop
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top