Error clicking datagrid after delete

G

Guest

Hi

I have a readonly datagrid bound to a relation and a button with the code
below in the click event:

Dim bm As BindingManagerBase =
Me.DataGrid1.BindingContext(Me.DataGrid1.DataSource, Me.DataGrid1.DataMember)
Dim dr As DataRow = CType(bm.Current, DataRowView).Row
Dim intPos As Integer

intPos = bm.Position
dr.Delete()
dr.EndEdit()
If intPos > bm.Count - 1 Then
bm.Position = bm.Count - 1
Else
bm.Position = intPos
End If

Me.DataGrid1.Select(bm.Position)
Try
Me.DataGrid1.Focus()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

It works fine for all datagrid rows except the last one, for which the
following error is thrown by the Me.DataGrid1.Focus() line. Also applied if
use clicks as well:

"index was outside the bounds of the array"

Importantly, the Messagebox in the error trap above is NOT HIT!

Any info. would be most appreciated.
 
K

Kevin Yu [MSFT]

Hi Terry,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that after you deleted the last row, an
"index was outside the bounds of the array" exception was thrown. If there
is any misunderstanding, please feel free to let me know.

Based on the code you have provided, it seems that the error might be thown
from the following line.

bm.Position = bm.Count - 1

When the last row is deleted, the bm.Count has to be 0. So assigning -1 to
bm.Position might throw that exception. I suggest you step through each
line to see if the exception was thrown by that line. If it is, please add
an if statement to check for the last row.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

Hi Kevin

No the error is not being caused by the line you suggest. I stated in my
last post which line is causing the error. If the code is simplified to:

Dim bm As BindingManagerBase =
Me.DataGrid1.BindingContext(Me.DataGrid1.DataSource, Me.DataGrid1.DataMember)
Dim dr As DataRow = CType(bm.Current, DataRowView).Row

dr.Delete()
dr.EndEdit()

Try
Me.DataGrid1.Focus()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try


Then the error still occurs on the *** Me.DataGrid1.Focus() *** line. Also,
the error is not trapped by the Try Catch.

ADDITIONAL INFO. - only occurs when DataGrid is set to either:
1. DataGrid1.ReadOnly = True or
2. CType(cm.List, DataView).AllowNew = False where cm is the DataGrid1
currencymanager.
i.e. when the Add New line is not there in the DataGrid.

On searching the net this is a fairly common issue, but none of the
solutions suggested work for me.

Regarding your suggestion that the bm.count = 0 when the last line is
deleted, I don't understand this and the code bears me out. When the last
line is deleted the bm.count decrements by 1.

I think the error is being caused by the datagrid code itself by a bug.
 
C

Cor Ligthert

TL,

Are you sure that Kevin is wrong.

Your code makes it AFAIK for me posible that when the Datagrid gets the
focus he has to set the first line before the last datarow.

I think that there will be an error in that case.

Cor
 
G

Guest

Hi Cor

I think there has been some misunderstanding. I don't think I made myself
clear enough.

By last line I mean say row 3 where the count is 4. Not the only row in the
datagrid.

In the previous code I can set the position to 0 after the delete but it
still makes no difference to the error being created as the datagrid gets the
focus.
 
C

Cor Ligthert

TL,

I did not try it, maybe can you do it yourself.

What happens when you change those two

intPos = bm.Position
dr.Delete()

to
dr.Delete
intPost = bm.Postition

There is something in my opinion not right with that.
However I would have to try it to see it completly, however therefore is
your code to mixed up with your database.

Cor
 
G

Guest

Hi Cor

Isn't it standard practice to read the position of the row to delete into a
local variable (intPos) before deleting it? Therefore it is possible to
position to the correct row follwoing the delete.

Anyway, if the DataGrid is ReadOnly=False then it all works fine. It's just
that I need it ReadOnly=True!
 
C

Cor Ligthert

TL,
Isn't it standard practice to read the position of the row to delete into
a
local variable (intPos) before deleting it? Therefore it is possible to
position to the correct row follwoing the delete.

Anyway, if the DataGrid is ReadOnly=False then it all works fine. It's
just
that I need it ReadOnly=True!

Why, when I want to select the last row,
than is it my whateverdatasource.count -1
And the first = 0

When I only want to postition is than it is setting the position from the cm
to that.

I do not make all those calculations.

Cor
 
G

Guest

Yes, conceeded this positioning code is redundant in ADO.NET. I just put it
in originally automatically, but it was not that that was causing the problem.

I have found was seems to have been causing the problem in the source code
of the custom DataGridBoolColumn class I introduced (to select an entire row
- from George Sheperd's Windows Forms FAQ). The error was not happening here
though I have have previously said. It was when the datagrid received the
focus. I have seen this problem on several forums with varying workarounds.
One was to make the datagrid readonly=false just for the delete which worked,
but below really solves it on this occation.

For anyone that is interested it's a simple matter of a minus instead of a
plus (was "source.List.Count + 1" now "source.List.Count - 1") in:

-------------------------------------------------------------------------
Public Class DataGridNoActiveCellColumn
Inherits DataGridTextBoxColumn
Private SelectedRow As Integer
Protected Overloads Overrides Sub Edit(ByVal source As
CurrencyManager, ByVal rowNum As Integer, ByVal bounds As Rectangle, ByVal
[readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As
Boolean)

' make sure selectedrow is valid
If (SelectedRow > -1) And (SelectedRow < source.List.Count - 1)
Then
Me.DataGridTableStyle.DataGrid.UnSelect(SelectedRow)
End If
SelectedRow = rowNum
Me.DataGridTableStyle.DataGrid.Select(SelectedRow)

End Sub
End Class
 
K

Kevin Yu [MSFT]

Hi Terry,

Nice to hear that you have found the workaround. Thanks for sharing your
experience with all the people here. If you have any questions, please feel
free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

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