[SOLVED] Match DataSource with GetRowIndex()

Hi,

I have a DataSource which is filled with List<RecentElement>.
The GridGroupingControl has a ContextMenu (with a right click)

When the context menu is displayed,I need to retrieve he RecentElement object associated to right-click selected line.
But the index :
     int index = this.gridGroupingControl1.Table.CurrentRecord.GetRowIndex();
don't seems to match the DataSource.

- So, how can I get the RecentElement object ?
- What is the best way to pass RecentElement to context menu ToolStripMenuItem when user click on it ?

thanks,
Vincent

4 Replies

AR Arulpriya Ramalingam Syncfusion Team March 12, 2018 10:14 AM UTC

Hi Vincent,  
  
Thanks for contacting Syncfusion support.  
  
In order to get the data from the record, you can directly get the data as your collection by using the GetData() method of record when the context menu item is clicked. We have created a simple sample as per your requirement. Please make use of the below code and sample,  
  
Code example  
  
//Event triggering  
this.selectedDataToolStripMenuItem.Click += newSystem.EventHandler(this.selectedDataToolStripMenuItem_Click);  
  
//Event customization  
private void selectedDataToolStripMenuItem_Click(object sender, EventArgs e)  
{  
    if (this.gridGroupingControl1.Table.CurrentRecord != null)  
    {  
        Record record = this.gridGroupingControl1.Table.CurrentRecord;  
        RecentElement selectedData = record.GetData() as RecentElement;  
        MessageBox.Show("CategoryID : " + selectedData.CategoryID.ToString() + "\nCategoryName : " + selectedData.CategoryName.ToString() + "\nDescription : " + selectedData.Description.ToString() +"\nSampleData : " + selectedData.SampleData.ToString());  
    }  
}  
  
  
Note  
You can also get the data from SelectedRecords collection when the ListBoxSelectionMode is enabled to select the records and the data can be retrieved by using the GetData()method. Please refer to the below code,  
  
int count = this.gridGroupingControl1.Table.SelectedRecords.Count;  
if (count > 0)  
{  
    Record record = this.gridGroupingControl1.Table.SelectedRecords[count - 1].Record;  
}  
  
Regards,  
Arulpriya  



VD Vincent Duvernet March 13, 2018 08:29 PM UTC

nice, this works perfectly :)


VD Vincent Duvernet March 13, 2018 08:55 PM UTC

Just a question. I've added a Delete toolstrip item :
private void deleteElementToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.gridGroupingControl1.Table.CurrentRecord != null)
            {
                Record record = this.gridGroupingControl1.Table.CurrentRecord;
                record.Delete();
            }
        }
Which work in your sample but if we replace DataCollection by List<RecentElement>, it don't work anymore. Is this a normal way ?
Vincent


AR Arulpriya Ramalingam Syncfusion Team March 14, 2018 11:25 AM UTC

Hi Vincent, 
 
Thanks for the update. 
 
By default, the records will be deleted, updated or added in its source list which will not be reflected in the grid when the data source is List. In order to overcome this scenario, the grid can be reloaded manually whenever the changes are done in the source list by using the Reload() and Refresh(). Please refer to the below code, 
 
Code example 
 
//Event customization 
private void selectedDataToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
    if (this.gridGroupingControl1.Table.CurrentRecord != null) 
    { 
        Record record = this.gridGroupingControl1.Table.CurrentRecord; 
        record.Delete(); 
        //To update the changes in grid 
        this.gridGroupingControl1.Table.Reload(); 
        this.gridGroupingControl1.Refresh(); 
     } 
} 
 
 
Note 
To overcome this issue, we would suggest you to, use the BindingList collection which notifies the changes to the grid and update the changes by itself. So, please let us know that whether you have any concerns on using the BindingList as the data source.  
 
Regards, 
Arulpriya 


Loader.
Up arrow icon