Sfdatagrid view.removeAt doesn't work


I am reloading the data source if the row id is empty, but it is not deleted from the view after it is deleted from the database
and selection is not changed after row is deleted


  public void DataRowDelete(Syncfusion.WinForms.DataGrid.SfDataGrid _dtg, Action _action, Form _owner)
        {
            if (_dtg.SelectedItems.Count > 0)
            {
                var selectedItem = _dtg.SelectedItem as DataRowView;
                int _Dgdeletingrowindex = _dtg.SelectedIndex;
                var dataRow = (selectedItem as DataRowView).Row;
                int SelectedRowId;
                if (dataRow[0] == DBNull.Value)
                {
                    _dtg.SelectedItem= _dtg.View.Records[isdeletingnowindex - 2].Data as DataRowView;

                    DataGridRefresh(_dtg, _action);



                    var selectedItemRef = _dtg.View.Records[isdeletingnowindex-2].Data as DataRowView;
                 
                    var dataRowRef = (selectedItemRef as DataRowView).Row;
                    SelectedRowId = Convert.ToInt32(dataRowRef[0]);
                    _Dgdeletingrowindex = isdeletingnowindex; //Filter row 1

                }
                else
                {
                    SelectedRowId = Convert.ToInt32(dataRow[0]);
                }

                var res = MessageBoxController.ShowQuestionMessage("Are you sure you want to delete this line?");
                if ((DialogResult)res == DialogResult.Yes)
                {
                    using (ProgressManager progressManager = new ProgressManager())
                    {

                        progressManager.StartProgress(_owner);


                        Dictionary p = new Dictionary();
                        p.Add("rowid", SelectedRowId);
                        sql.ExecuteDataTable(DeleteProcName, p);
                        _dtg.CurrentCell.EndEdit(true);
                        _dtg.View.RemoveAt(_Dgdeletingrowindex-2); //Filter row  1
                    
                        progressManager.CloseProgress();
                    }
                }
               

            }


        }

and rowstyle doesn't work after remove row

 private void _dtg_QueryRowStyle(object sender, QueryRowStyleEventArgs e)
        {
            if (e.RowType == RowType.DefaultRow && e.RowIndex % 2 == 0)
                e.Style.BackColor = Color.WhiteSmoke;
        }



5 Replies

AR Amal Raj U Syncfusion Team June 7, 2018 12:26 PM UTC

Hi Eren, 

Thanks for using Syncfusion products. 

Query 
Response 



I am reloading the data source if the row id is empty, but it is not deleted from the view after it is deleted from the database  
and selection is not changed after row is deleted 
By Default, SfDataGrid.View.RemoveAt() method can be used to remove a record from the view and we have ensured that this is working fine at our end and tested in the below attached sample. 

We are little bit unclear about your requirement and your provided code example is not clear since some of the variable and methods are not included.  

Could you please let us know, issue experiencing is whether the record is not removed using View.RemoveAt or you expect the selection should remain after row is deleted?  

This will be very helpful for us to sort out the issue at the earliest. 





















rowstyle doesn't work after remove row 
The reported behavior of RowStyle event customization after a row deleted is a default behavior. 

 

Here, the above color change occurs due to change in row index of the rows below the deleted row. At first, the alternative rows will be rendered in WhiteSmoke color. But when a row is deleted from grid, the row index of the below rows will be changed. (i.e. Row index of the below rows would be changed from odd to even and even to odd). 

Since the row index of below rows are changed now, WhiteSmoke color is changed to current even rows also. 

To have alternate row colors even when deleting rows, please use the below code example. 

Code Example: 
this.sfDataGrid1.QueryRowStyle += SfDataGrid1_QueryRowStyle; 

private void SfDataGrid1_QueryRowStyle(object sender, QueryRowStyleEventArgs e) 
    if (e.RowType == RowType.DefaultRow && e.RowIndex % 2 == 0) 
        e.Style.BackColor = Color.WhiteSmoke; 
    else 
        e.Style.BackColor = SystemColors.Window; 
} 
 
 
 

Sample Location: 

Regards, 
Amal Raj U. 



ER Eren June 10, 2018 03:36 PM UTC

Thank you i solved most of issues.

These are the methods I use. my problem is after the row is deleted, new row not automatically selected. I've used the following methods to solve this problem but after delete three row it's not working.  

  private void SfDataGridController_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)

        {



            if (Newrowadding)

            {

                Newrowadding = AddNewRow(e);



            }

            if (e.Action == NotifyCollectionChangedAction.Remove)

            {

                //dtgrid.SelectedIndex = dtgrid.TableControl.ResolveToRecordIndex(dtgrid.SelectedIndex)+1;

            }


        }

 public void DataRowDelete()
        {

            if (dtgrid.SelectedItem != null)
            {



                System.Data.DataRow dataRow = (dtgrid.SelectedItem as DataRowView).Row;
                if (!AreAllColumnsEmpty(dataRow))

                {
                    int SelectedRowId = Convert.ToInt32(dataRow[0]);


                    
                   
                    var res = MessageBoxController.ShowQuestionMessage("Bu satırı silmek istediğinizden emin misiniz?");
                    if ((DialogResult)res == DialogResult.Yes)
                    {

                        using (ProgressManager progmanager = new ProgressManager())
                        {
                            progmanager.StartProgress(Ownerfrm);

                            int _Dgdeletingrowindex = dtgrid.TableControl.ResolveToRecordIndex(dtgrid.SelectedIndex);
                      
                            Dictionary p = new Dictionary();
                            p.Add("rowid", SelectedRowId);
                            sql.ExecuteDataTable(DeleteProcName, p);

                            dtgrid.View.RemoveAt(_Dgdeletingrowindex);

                            progmanager.CloseProgress();

                        }


                    }
                    else
                    {
                        dtgrid.CurrentCell.EndEdit();
                    }
                }
            }
        }



AR Amal Raj U Syncfusion Team June 11, 2018 08:36 AM UTC

Hi Eren, 

Thanks for your update. 

We have modified your code to achieve the reported scenario of selection after deleting a row and we have processed the selection with SelectedItem property with CurrentCell index. Please refer to the attached sample in the below location. 

Sample Location: 

Regards, 
Amal Raj U. 



ER Eren June 11, 2018 03:14 PM UTC

Thank you. I've run your code example but I have to add the code below now it's work but if ı reflesh the datasource it's trow exception because selected item is null

I solved this problem second method

if (e.Action == NotifyCollectionChangedAction.Remove)
            {
                var recordsCount = dtgrid.View.Records.Count;
                if (recordsCount <= 0) return;
        
                var resolvedRowIndex = dtgrid.TableControl.ResolveToRecordIndex(dtgrid.CurrentCell.RowIndex);

                dtgrid.ClearSelection();

                if (resolvedRowIndex < recordsCount)
                    dtgrid.SelectedItem = dtgrid.View.Records[resolvedRowIndex];
                else
                    dtgrid.SelectedItem = dtgrid.View.Records[recordsCount - 1];
            }

 public void DataGridRefresh(Syncfusion.WinForms.DataGrid.SfDataGrid _dtg)
        {
            using (ProgressManager progressManager = new ProgressManager())
            {
                SqlManager sql = new SqlManager();
                progressManager.StartProgress(Ownerfrm);
                _dtg.SelectedItem =_dtg.View.Records.GetItemAt( _dtg.TableControl.ResolveToRecordIndex(_dtg.SelectedIndex));
                _dtg.CurrentCell.EndEdit(true);
                Dictionary p = new Dictionary();

                _dtg.DataSource = sql.ExecuteDataTable(GetProcName, p);
                (_dtg.View as CollectionViewAdv).CollectionChanged += SfDataGridController_CollectionChanged;
                GridSettings();




                progressManager.CloseProgress();
            }


        }


MA Mohanram Anbukkarasu Syncfusion Team June 12, 2018 08:48 AM UTC

Hi Eren,  
 
Thanks for your update. 
 
We are glad to know that you have resolved your query. Please let us know, If you need any further assistance. 
 
Regards, 
Mohanram A. 


Loader.
Up arrow icon