VB.NET Datagrid Problem

S

simchajoy2000

Hi,

I have a form containing a datagrid where users can enter in their
specified information for each row and column. Behind the scenes I
have a sub which handles datagrid.CurrentCellChanged because I need to
be able to execute some code whenever a user clicks in a different
cell of the datagrid. Inside this sub I place the datagrid.datasource
into a data table so that I can work with it's contents. However, I
am getting "Out of Range" errors sometimes when I try to execute this
code:

Dim index As Integer = dgRentPool.CurrentRowIndex
Dim tbl As DataTable = dgRentPool.DataSource
Dim row As DataRow

row = tbl.Rows(index)

It appears that occasionally even though the datagrid recognizes that
the current row is "1" for example, the number of rows contained in
the datagrid.datasource is only 1 instead of 2.

Does anyone have any ideas on what is causing this error and how I can
resolve it??

Thanks!!

Joy
 
G

Guest

August 9, 2004

You are getting that because the array of rows is zero-based.
Try using this statement instead:

row = tbl.Rows(index - 1)

All arrays in Visual Studio are zero-based. Have a nice day!



Joseph MCP
 
J

Joy Labadie

Thank you but when I try the code you suggested:

row = tbl.Rows(index - 1)

I get an "Out of Bounds" error as well because the following statement:

Dim index As Integer = dgRentPool.CurrentRowIndex

returns the value of 0 for the first row in the datagrid. Any other
suggestions??

Joy
 
G

Guest

August 9, 2004

This is just a theory, but try stepping through the code and
making sure that when you assign the datatable to the source,
that the table really is populated with the information. If you
assigned the datagrid's datasource property with a dataset, then
you might try this code instead...

dim tbl as datatable = Dataset.Tables(Table#) ' Or .(TableName)

Just a theory, but I would see if the table was populated, before
calling the row = ..... line.


Joseph MCP
 
J

Joy Labadie

In my form's New() sub I do the following:

Dim tblRentPool As DataTable = New DataTable
Dim row As DataRow

tblRentPool.Columns.Add("Months")
row = tblRentPool.NewRow()
tblRentPool.Rows.Add(row)

dgRentPool.DataSource = tblRentPool

I want the user to be able to enter in the information for the datagrid
at run-time. By default a datagrid creates a row beneath the current
row the user is working on so the user can continue populating the
datagrid when they finish filling out the current row.

However, when the user then clicks on this new row provided by the
datagrid and it calls the datagrid.CurrentCellChanged sub, it is not
able to find the corresponding row in the datagrid.datasource. So it
appears the datagrid is not updating the data table associated with the
datasource when it creates this new row visually in the datagrid.

Isn't the datagrid.datasource supposed to keep track of this
information?

Joy
 

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

Top