select a record based on current selection

Hi guys,

Need some help please.

So, I have a sfDataGrid with an unbounded GridCheckBoxSelectorColumn as the first column. Now if a user selects a particular entry, I want automatically loop through the data in the same datagrid, and select other records based on a condition.

Currently I have this as my code:

        private void sfdgBaseFileData_SelectionChanging(object sender, Syncfusion.WinForms.DataGrid.Events.SelectionChangingEventArgs e)
        {
            if (sfdgBaseFileData.CurrentItem is DataRowView selectedItem)
            {
                var dataRow = (selectedItem as DataRowView).Row;
                if (dataRow != null)
                {
                    if (dataRow["CompareResults"].ToString() == "Value found")
                        e.Cancel = true;
                    else
                    {
                        if (Type == "Asset Structure")
                        {
                            if (dataRow["PARENT_LOCATIONID"].ToString() != null)
                            {
                                var recs = sfdgBaseFileData.View.Records;
                                foreach (var rec in recs)
                                {
                                    var drv = rec.Data as DataRowView;
                                    if (drv != null)
                                    {
                                        if (drv.Row["LOCATIONID"].ToString() == dataRow["PARENT_LOCATIONID"].ToString())
                                        {
                                            sfdgBaseFileData.SelectedItems.Add(rec);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

        }


When i step through, i can see the "sfdgBaseFileData.SelectedItems.Add(rec) is firing, but the screen does not update with the selection. Please help.

Thanks
Brad

8 Replies

BW Bradley Wheeler May 18, 2020 08:29 AM UTC

I have stepped through my code, and it seems to me like it is firing in the correct places, and even the "sfdgBaseFileData.SelectedItems.Add(rec);" command is firing, but the screen does not show that it has selected this record.

Thanks


VS Vijayarasan Sivanandham Syncfusion Team May 18, 2020 06:33 PM UTC

Hi Bradley Wheeler,

Thank you for contacting Syncfusion support.

We suspect that your enable the SelectionMode property value as Single So selection not applied. If you want to select the multiple row you need to set the SelectionMode value as Multiple in SfDataGrid. Please refer the below user documentation,

UG Link:
https://help.syncfusion.com/windowsforms/datagrid/selection#multiple-row-or-cell-selection 
If we misunderstood your requirement, please provide more information regarding the requirement. This would help us to proceed further.

Regards,
Vijayarasan S

 



BW Bradley Wheeler replied to Vijayarasan Sivanandham May 19, 2020 07:31 AM UTC

Hi Bradley Wheeler,

Thank you for contacting Syncfusion support.

We suspect that your enable the SelectionMode property value as Single So selection not applied. If you want to select the multiple row you need to set the SelectionMode value as Multiple in SfDataGrid. Please refer the below user documentation,

UG Link:
https://help.syncfusion.com/windowsforms/datagrid/selection#multiple-row-or-cell-selection 
If we misunderstood your requirement, please provide more information regarding the requirement. This would help us to proceed further.

Regards,
Vijayarasan S

 


Hi Vijay

No, I have taken that into consideration. My SelectionMode has been set to Multiple from the beginning. I also knows this works, as when I am in the form, I can manually select multiple rows with the click of my mouse. I just cannot do it programmatically. I see the rows i want to select are added to the array, but it is not showing on the form is not updating.

Thanks
Bradley


VS Vijayarasan Sivanandham Syncfusion Team May 20, 2020 01:27 PM UTC

Hi Bradly Wheeler,

Thanks for the update.

Your requirement can be achieved by Calling the SfDataGrid.BeginInvoke for SelectedItems in SfDataGrid. Please refer the below code snippet,
 
private void SfDataGrid1_SelectionChanging(object sender, Syncfusion.WinForms.DataGrid.Events.SelectionChangingEventArgs e) 
{            
            var records = sfDataGrid1.View.Records; 
             
            foreach (var record in records) 
            { 
                var dataRowView = record.Data as DataRowView; 
                if (dataRowView != null) 
                { 
                    if (dataRowView["Gender"].ToString() == "Male") 
                    { 
                        sfDataGrid1.BeginInvoke(new Action(() => 
                        { 
                            sfDataGrid1.SelectedItems.Add(record.Data); 
                        })); 
                    } 
 
                } 
            }           
} 
Regards,
Vijayarasan S
 



BW Bradley Wheeler May 21, 2020 09:40 AM UTC

Hi Vijay

I have tried the BeginInvoke, and now it selects, but instead of it selecting only the records it is suppose to select, it selects from the record i manually selected to some other random record on my sfDataGrid.

So, we almost there, but we missing something still. I thought maybe an EndInvoke is needed, and i tried to look on the help files and the forum on how to use this, and still cannot find something useful.

Thanks
Bradley


VS Vijayarasan Sivanandham Syncfusion Team May 22, 2020 05:51 PM UTC

Hi Bradley Wheeler,

Thanks for the update.

EndInvoke is not needed. As per your requirement select record based on some condition. We are unable to understand your query. What reason to select the record based on programmatically. Can you please provide the more information related to your query.
 
It will be helpful for us to check on it and provide you the solution at the earliest.

Regards,
Vijayarasan S

 



BW Bradley Wheeler May 24, 2020 07:06 AM UTC

Hi Vijay

There is a relationship within my data grid view, that links to itself. It is sort of like a treeview build, so if a user selects certain records, other records need to be selected to.

E.g. The user selects record at row 10. However, rows 4 and 6 has data that row 10 needs, so I want to programmatically select row 4 and 6 as well. Yet, row 1 and 2 has data that row 4 needs, so I need to select row 1 and 2 also.
So at the end of selecting row 10, we will have rows 1, 2, 4, 6 and 10 selected.

I have this working with forcing data into a cell on the dgv, but I cannot seem to get those rows selected.

Hope this makes more sense.


SA Saravanan Ayyanar Syncfusion Team May 25, 2020 02:07 PM UTC

Hi Bradley Wheeler, 
 
Thanks for your update. 
 
We have checked your reported scenario and prepared the sample. Please refer the below code snippet, 
 
private void SfDataGrid1_SelectionChanging(object sender, Syncfusion.WinForms.DataGrid.Events.SelectionChangingEventArgs e) 
        { 
            if (sfDataGrid1.CurrentItem is DataRowView selectedItem) 
            { 
                var dataRow = (selectedItem as DataRowView).Row; 
                if (dataRow != null) 
                { 
 
                    var records = sfDataGrid1.View.Records; 
 
                    foreach (var record in records) 
                    { 
                        var dataRowView = record.Data as DataRowView; 
                        if (dataRowView != null) 
                        { 
                            if (dataRowView["Country"].ToString() == dataRow["Country"].ToString()) 
                            { 
                                sfDataGrid1.BeginInvoke(new Action(() => 
                                { 
                                    sfDataGrid1.SelectedItems.Add(record.Data); 
                                }));                                
                            } 
                            if (dataRowView["ShipCity"].ToString() == "London") 
                            { 
                                sfDataGrid1.BeginInvoke(new Action(() => 
                                { 
                                    sfDataGrid1.SelectedItems.Add(record.Data); 
                                })); 
                            } 
 
                        } 
                    } 
                } 
            } 
        } 
 
Sample Link: 
 
Please let us know, if you require further assistance on this. 
 
Regards, 
Saravanan A. 


Loader.
Up arrow icon