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

Question on Adding New Rows

5 Answers 320 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Melissa
Top achievements
Rank 1
Melissa asked on 04 May 2012, 05:54 PM
Hello,

I have a gridview where I set AllowAddNewRow=true so that I can click where it says "Click here to add new row" when the grid is in edit mode.

The issue that I'm having is that after I click to create a new row and fill in the columns, I click somewhere else on the form (not on the grid) which appears to add the new row to the grid, but it also starts creating a 2nd new row. Having the start of the 2nd new row doesn't happen when I click on the grid itself (that adds the new row and brings back the "Click here to add new row" message).

Is there a way to stop the grid from creating this 2nd new row automatically? What I'd like to be able to do is click to create a new row and fill in the information for that row, click anywhere on my form to trigger the new row getting added to the grid, have the grid's selected row point towards the new row, and have the "Click here to add a new row" be displayed on the grid again.

I'm sure there's a way to do this, but I'm fairly new to Telerik and not sure how to accomplish this behavior. Thank you for any assistance!

5 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 08 May 2012, 03:21 PM
Hello Melissa,

Thank you for this question. 

Actually, RadGridView does not add a new row until all changes are validated. This is just a visual representation and you can control it by calling the UpdateContentVisibility method of the GridNewRowElement. This can be done when handling the LostFocus event. Consider the following code snippet:
void radGridView1_LostFocus(object sender, EventArgs e)
{
    if (!radGridView1.ContainsFocus)
    {
        GridNewRowElement newRow = this.radGridView1.TableElement.GetRowElement(this.radGridView1.MasterView.TableAddNewRow) as GridNewRowElement;
        if (newRow != null)
        {
            newRow.UpdateContentVisibility(false);
        }
    }
}

I hope this helps.
 
All the best,
Jack
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Melissa
Top achievements
Rank 1
answered on 08 May 2012, 04:30 PM
Hi Jack,

Thanks for your code sample. I couldn't find a LostFocus event, but i put your code into the grid's Leave event and it seems to be working correctly.

Could you explain what triggers the UserAddedRow event to fire? It looks like my new row is getting validated, but it doesn't actually get added until I click back on the grid (I'm clicking on the new row to get that to be current too). Is there a way to make the UserAddedRow event happen without clicking back on the grid? I'd like to be able to make the UserAddedRow event happen after the new row passes the validation and have the newly added row set to be current at the same time. Is that possible without having to click on the grid again?

Thanks so much for your help!
0
Jack
Telerik team
answered on 11 May 2012, 02:05 PM
Hi Melissa,

The LostFocus event is implemented in the standard Control class which is part from the MS WinForms framework. It is available for all controls, however this event is not visible at design time. You can subscribe to it only with code. The Leave event is also suitable in this scenario.

The UserAddedRow event fires after validating and adding row to RadGridView. However, this event fires before changing the CurrentRow property. You can solve the described issue by handling both CurrentRowChanging and UserAddedRow events. Here is a sample:
GridViewRowInfo row;
 
void radGridView1_UserAddedRow(object sender, GridViewRowEventArgs e)
{
    this.radGridView1.CurrentRow = e.Row;
    row = e.Row;
}
 
void radGridView1_CurrentRowChanging(object sender, CurrentRowChangingEventArgs e)
{
    if (e.NewRow is GridViewNewRowInfo && e.CurrentRow == row && row != null)
    {
        e.Cancel = true;
        row = null;
    }
}

If you have other questions, I will be glad to help.

Regards,
Jack
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Melissa
Top achievements
Rank 1
answered on 11 May 2012, 04:46 PM

Hi Jack,

Thanks for your assistance. I almost have things working. I think the last issue I’m having involves when the UserAddedRow event fires. I added some screen shots to help me explain better.

 

The first screen shot is right after I’m done filling in all columns for the new row.

 

The second screen shot is right after I clicked on the middle grid on the form. It appears visually that the new row was added to the first grid (it passed validation), but the UserAddedRow event hasn’t fired yet.

 

The third screen shot is after I click back on the “click here to add a new row” row on the first grid. At that point the UserAddedRow event fires, the row that I created in the first screen shot is actually added, and it becomes the selected row.

 

Is there a way to get the results of the third screen shot (with the new row added to the collection and it being the selected row) when I click on the middle grid on the form? I’m not sure if I would be able to add something to a different event that would trigger the UserAddedRow for the first grid to fire, but as far as I can tell, it only gets called when I click on the first grid and not on a different section of the form. 

In my database I have a one to many relationship between the table that’s in the first grid and the table that’s in the middle grid. I just need to make sure that the row is completely added to the first grid before users start trying to add rows to the middle grid since otherwise they would be adding child rows when the parent doesn’t technically exist yet.

Thanks,
Melissa

0
Jack
Telerik team
answered on 16 May 2012, 04:55 PM
Hi Melissa,

This is an interesting case. In my tests the UserAddedRow event fires in this scenario. However, it is not easy to change the current row when handling it. I was able to find another option for you. 

You should create a custom event listener class an attach it to RadGridView. Here is the code: 
public class EventListener : IGridViewEventListener
{
    RadGridView grid;
    GridViewRowInfo addedRow;
 
    public EventListener(RadGridView grid)
    {
        this.grid = grid;
    }
 
    public GridEventType DesiredEvents
    {
        get { return GridEventType.UI; }
    }
 
    public EventListenerPriority Priority
    {
        get { return EventListenerPriority.Normal; }
    }
 
    public GridEventProcessMode DesiredProcessMode
    {
        get { return GridEventProcessMode.PostProcess; }
    }
 
    public GridViewEventResult PreProcessEvent(GridViewEvent eventData)
    {
        return null;
    }
 
    public GridViewEventResult ProcessEvent(GridViewEvent eventData)
    {
        return null;
    }
 
    public GridViewEventResult PostProcessEvent(GridViewEvent eventData)
    {
        if (eventData.Info.Id == KnownEvents.ViewChanged)
        {
            DataViewChangedEventArgs args = (DataViewChangedEventArgs)eventData.Arguments[0];
            if (args.Action == ViewChangedAction.Add)
            {
                addedRow = (GridViewRowInfo)args.NewItems[0];
            }
        }
        else if (eventData.Info.Id == KnownEvents.RowInvalidated && eventData.Sender is GridViewNewRowInfo && addedRow != null)
        {
            grid.CurrentRow = addedRow;
            addedRow = null;
        }
        return null;
    }
 
    public bool AnalyzeQueue(List<GridViewEvent> events)
    {
        return false;
    }
}

Use the following code to register the listener:
myEventListener = new EventListener(this.radGridView1);
this.radGridView1.MasterTemplate.SynchronizationService.AddListener(myEventListener);

Be sure to create a local variable with the same scope as RadGridView, because the SynchronizationService uses weak references to store its listeners.

We realize that this is not much intuitive and we will consider improving our API to cover this scenario.

All the best,
Jack
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Tags
GridView
Asked by
Melissa
Top achievements
Rank 1
Answers by
Jack
Telerik team
Melissa
Top achievements
Rank 1
Share this question
or