Remove multiple Selected Rows when using Linq

Hello,
I have a Winforms application and I need to remove multiple selected rows on database table.
The SfDataGrid is populated with Linq query.

public void LoadGrid()
{
     var query = from x in ctx.TableName select x).ToList();
     sfDataGrid.DataSource = query;
}

Then, when user selects one or multiple rows, the Delete button is Visible and when User clicks on it, it removes all rows.
So far, I have code to remove only one row, but need to allow removing multiple selected rows...

Here's the code for 1 row remove...

public void RemoveRow()
{
     var remove = ctx.TableName.Where(s => s.Id == SelectedRowID).FirstOrDefault();
     try{
               ctx.SaveChanges();
          }
     catch(Exception ex)
          {
               MessageBox.Show(ex.ToString());
          }
}

Now, to remove multiple rows and I'm a bit confused on how can I achieve this!

Can someone help me here?

Thanks.

5 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team August 25, 2020 03:16 AM UTC

Hi Carlos,

Thank you for contacting Syncfusion support.

 
Currently, we are analyzing your requirement of “Remove multiple Selected Rows when using Linq in SfDataGrid”. We will validate and update you the details on or before August 26, 2020. 
 
We appreciate your patience until then. 
 
Regards, 
Vijayarasan S




VS Vijayarasan Sivanandham Syncfusion Team August 27, 2020 03:53 AM UTC

Hi Carlos,

Thank you for your patience.

Solution 1:

Based on provided code snippet you are using FirstOrDefault method in remove data row. In this case FirstOrDefault method only returns the one record. So, your requirement can be achieved by calling ToList Method for removing multiple records. Please refer the below code snippet,
 
public void RemoveRow() 
{ 
     var remove = ctx.TableName.Where(s => s.Id == SelectedRowID).ToList (); 
     try 
     { 
               ctx.SaveChanges(); 
     } 
     catch(Exception ex) 
     { 
               MessageBox.Show(ex.ToString()); 
     } 
} 
 
Solution 2:

Your requirement can be achieved by get the selected records in SfDataGrid.SelectedItems. Please refer the below code snippet, 
public void RemoveRow() 
{ 
       var selectedItems = this.sfDataGrid1.SelectedItems; 
        
       foreach (var selected in selectedItems) 
       { 
                //SelectedItems Collection in SfDataGrid.SelectedItems  
                //Customization code for removing multiple data row  
       } 
 } 

We hope this helps. Please let us know, if you require further assistance on this.

Regards,
Vijayarasan S




CF Carlos Ferreira August 27, 2020 10:08 PM UTC

Sorry but that didn't help and I think this was my fault I didn't explained myself the better way...

Let's get this step by step:

1. First, SfDataGrid Selection Mode = Extended
2. When user selects a cell or row, I get the Id of clicked Row...

private void data_CellClick(object sender, Syncfusion.WinForms.DataGrid.Events.CellClickEventArgs e)
        {
            int rowIndex = e.DataRow.RowIndex;
            int columnIndex = data.TableControl.ResolveToGridVisibleColumnIndex(0);
            if (columnIndex < 0)
                return;
            var mappingName = data.Columns[columnIndex].MappingName;
            var recordIndex = data.TableControl.ResolveToRecordIndex(rowIndex);
            if (recordIndex < 0)
                return;
            if (data.View.TopLevelGroup != null)
            {
                var record = data.View.TopLevelGroup.DisplayElements[recordIndex];
                if (!record.IsRecords)
                    return;
                var data = (record as RecordEntry).Data;
                SelectedRow = Convert.ToInt32(data.GetType().GetProperty(mappingName).GetValue(data, null));
            }
            else
            {
                var record1 = data.View.Records.GetItemAt(recordIndex);
                SelectedRow = Convert.ToInt32(record1.GetType().GetProperty(mappingName).GetValue(record1, null));
            }
BtnDelete.Visible = true;
        }

3. Only when User clicks one or more rows, the delete button is visible.
4. When user clicks delete button, I'll check if selected rows is 1 or more and then, delete one or more lines, but it's on the multiple lines where I'm getting some issues...

List<int> list = new List<int>();
        private void BtnDelete_Click(object sender, EventArgs e)
        {
            if(dataTiposReq.SelectedItems.Count == 1)
            {
                var delete = ctx.TipoRequisito.FirstOrDefault(s => s.Id == SelectedRow);
                try
                {
                    ctx.SaveChanges();
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            else
            {
                foreach(var row in dataTiposReq.SelectedItems)
                {
                    //How to get first column value from each multi selected rows
                    list.Add(someId);
                }
                foreach(var item in list)
                {
                    var delete = ctx.TipoRequisito.Add(s => s.Id == item).FirstOrDefault();
                    try
                    {
                        ctx.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
            }

If I get how to manage the multiple Row and get the first column value (Id) from each selected row, then I can delete each of selected row...

Hope this explain helps.

Thanks.


VS Vijayarasan Sivanandham Syncfusion Team August 30, 2020 04:35 PM UTC

Hi Carlos,

Thank you for contacting Syncfusion support.

Currently, we are analyzing your requirement of “Remove multiple Selected Rows when using Linq”. We will validate and update you the details on or before September 01, 2020. 
 
We appreciate your patience until then. 
 
Regards, 
Vijayarasan S




VS Vijayarasan Sivanandham Syncfusion Team September 2, 2020 03:31 AM UTC

Hi Carlos,

Thank you for your Patience.

Based on provided information we have prepared the sample for achieve your requirement.
 
We hope this helps. Please let us know, if you require further assistance on this.

Regards,
Vijayarasan S
 


Marked as answer
Loader.
Up arrow icon