how to delete selected data in sfdatagrid ?

hi everyone,

I have a sfDatagrid with a MySql database connection, and added a function called GridCheckBoxSelectorColumn and then placed it in column 1.

the code looks like this:

this.sfDatagrid1.Columns.Add(new GridCheckBoxSelectorColumn() { MappingName = "SelectorColumn", HeaderText = string.Empty, AllowCheckBoxOnHeader = true, Width = 34, CheckBoxSize = new Size(14, 14) });


and I want to delete data based on selected, how to achieve it ?


Thank you


6 Replies 1 reply marked as answer

DM Dhanasekar Mohanraj Syncfusion Team September 14, 2022 02:23 PM UTC

Hi Ridwan,

By default, when you are enabling the AllowDeleting property for SfDataGrid you can delete the record by selecting and clicking the Delete key. If you need to remove the record programmatically, it will be achievable by using the below ways,

Case 1: Using Remove method

var orderInfo = orderInfoCollection.OrdersListDetails.FirstOrDefault(order => order.OrderID == 10000);

orderInfoCollection.OrdersListDetails.Remove(orderInfo);


Case 2: Using the RemoveAt method

orderInfoCollection.OrdersListDetails.RemoveAt(1);


And if you need to delete all selected rows programmatically, it will be achievable by the DeleteSelectedRecords method shown below,

this.sfDataGrid.DeleteSelectedRecords();


For more information related to deleting the records, please refer to the below user guide documentation link,

UG Link: https://help.syncfusion.com/windowsforms/datagrid/datamanipulation#delete-row

Please have a look at this and revert us if you need further assistance with this.

Regards,
Dhanasekar M.

If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.



RI RIDWAN replied to Dhanasekar Mohanraj September 14, 2022 03:12 PM UTC

thanks in advance sir.


yes, I understand a bit about that because it's in the Documentation.

i mean, i have sfDatagrid linked with mysql, and what i want is to delete that data based on selected item, i have tried your suggestion, but its not working for database bound data.


in my learning project, with Telerik, deleting selected data works fine.

this is the code:


                    foreach (GridViewRowInfo row in DGV.Rows)

                    {

                        if (row.Cells[0].Value != null)

                        {

                            MySqlConnection mysqlconn = new MySqlConnection(mainconn);

                            cmd = new MySqlCommand("DELETE FROM tb_school WHERE student='" +

                            row.Cells[1].Value.ToString() + "' AND class='" + row.Cells[10].Value.ToString() + "' ", mysqlconn);

                            mysqlconn.Open();

                            cmd.ExecuteNonQuery();

                            mysqlconn.Close();

                        }

                    }

                    RadMessageBox.Show("Delete sucsessfuly", "Notification", MessageBoxButtons.OK,

                    RadMessageIcon.Info);


then how to achieve what I want using syncfusion ?

Thank you sir  :)



DM Dhanasekar Mohanraj Syncfusion Team September 15, 2022 04:35 PM UTC

Hi Ridwan,


We have checked the reported issue on our end. We regret to let you know that we are unable to reproduce the reported issue on our end. The selected records were deleted properly as expected. Here we have attached the tested sample and video demo for your reference. Please have a look at this, if you still facing the reported issue please modify the attached sample based on your scenario and share the video illustration for the reported issue. It will be helpful for us to check the cause of the issue and to provide a solution at the earliest. 


Regards,

Dhanasekar M.


Attachment: Sample__VideoDemo_bf374f3d.zip

Marked as answer

RI RIDWAN replied to Dhanasekar Mohanraj September 21, 2022 06:37 AM UTC

Turns out I was wrong with my previous question, it worked well as the solution you provided, so I combined that code with CommandBuilder to achieve a nice approach.


thanks for the answer sir. :)



WI will November 15, 2022 05:48 AM UTC

In the Sf Data Grid, you can delete the selected row by pressing the Delete key when the Sf Data Grid. Allow Deleting is set to true. Likewise, you can delete the selected cell value without entering into the edit mode when the delete key is pressed by deriving a new class from the Grid Selection Controller.


Regards,

Will



DM Dhanasekar Mohanraj Syncfusion Team November 16, 2022 02:04 PM UTC

This is also one of the possibilities to achieve the mentioned requirement.


Loader.
Up arrow icon