This is a migrated thread and some comments may be shown as answers.

RowIndex when calling AddNew

3 Answers 121 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 24 Oct 2012, 08:19 AM
Hi,

When I click the "Click here to add a new row" text on a grid, the new row is overlaid on the text and I insert values in place. The RowIndex of the new row is -1, and this allows me to differentiate between an insert and an edit,

If I call Rows.AddNew though it looks like the row is inserted and then edited, which means it is assigned a RowIndex and I can't now check whether this is an insert or update.

In the BeginEdit event, I check the RowIndex to determine whether we are inserting or editing as some of the columns are Insert Only as :

Dim rowindex As Integer = e.RowIndex
            If rowindex > -1 Then
                If CustomCheckForInsertOnly Then
                    e.Cancel = True
                    Exit Sub
                End If
            End If

This works fine for a click initiated insert, but doesn't for a programmatic insert as the RowIndex is now assigned. 

Is there a way to simulate the "Click here to add a new row" behaviour when calling Rows.AddNew, or alternatively another way to differentiate between an insert and an update in BeginEdit?

Thanks,

Mark.

3 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 29 Oct 2012, 07:46 AM
Hello Mark,

Thank you for writing.

If I understand correctly, in the CellBeginEdit you want to be aware whether a new row or an existing one is edited. If so, I would suggest to check the row type in the event handler, so you will know whether you are in the new row or not:
Private Sub RadGridView1_CellBeginEdit(sender As Object, e As Telerik.WinControls.UI.GridViewCellCancelEventArgs) Handles RadGridView1.CellBeginEdit
       If TypeOf e.Row Is GridViewNewRowInfo Then
           'you are editing a cell in the new row
       End If
   End Sub

To get notified for inserting a row you can use the UserAddedRow event, which fires when the user adds row via the UI and the CollectionChanged event of the Rows collection which fires for every change in the Rows collection.

I hope that you find the provided information useful. If I did not understand your scenario correctly, please provide me with more details about it so I can point you to the right direction.
 
Greetings,
Stefan
the Telerik team
Q3’11 of RadControls for WinForms is available for download (see what's new). Get it today.
0
Mark
Top achievements
Rank 1
answered on 16 Nov 2012, 09:13 AM
Hi,

Thanks for getting back to me an sorry for the delay in responding.

I have updated the code as below and it works OK, however, I'm not too sure how to invoke the edit on the new cell. I currently have a custom behaviour on the grid trapping a keypress the does a manual row insert.

To do this, I  call
 
myGrid.Rows.AddNew()

which does add the new row but doesn't seem to invoke the BeginEdit. If I then manual click the new row to edit, it is no longer of type GridViewNewRowInfo. To get around this, I have tried to call BeginEdit after the AddNew 

myGrid.Rows.AddNew()
myGrid.BeginEdit()

The edit though seems to be invoked on the currently selected row in the grid and not the new row. How do I programatically invoke the edit on the new row?

Many thanks,

Mark


 
0
Stefan
Telerik team
answered on 20 Nov 2012, 11:14 AM
Hello Mark,

When the BeginEdit method is called, it will start editing of the CurrentCell. So, to start editing of a cell in the new row, you need to make the a cell in the new row current and then call the method, or you can just call the BeginEdit method of the desired cell. Here is how to do that for the first cell in the new row:
radGridView1.MasterView.TableAddNewRow.Cells[0].BeginEdit();

I hope that you find this information useful. Let me know if you need further assistance with this case.

Regards,
Stefan
the Telerik team
Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
Tags
GridView
Asked by
Mark
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Mark
Top achievements
Rank 1
Share this question
or