This is a migrated thread and some comments may be shown as answers.

Keeping track of edited rows

3 Answers 236 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ajith
Top achievements
Rank 1
Ajith asked on 24 Nov 2017, 07:05 AM

I might be a newbie to Telerik Winform controls.

I have a rad grid view displaying from a data source. for each row there are two buttons i.e approve and reject. I need to selectively approve or reject each row. On Click of a save button i have to save them back to database.

I have Boolean field in table to which value is based on approve or reject button. The default value is false. I want only to save the database rows that i have modified that too on save button click.Can I have any flag to selectively get only the rows modified at radgrid so that i can retrieve them and save them on save button click.

I worked on it did not work out, in My version  because  every save would save each and every rows if was modified on rad data grid or not.

I'd be thankful if someone helps me in this issue.

3 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 24 Nov 2017, 01:26 PM
Hello, Ajith,

Thank you for writing.  

When RadGridView is in bound mode each change of the row will update the DataBoundItem accordingly. It is desired behavior. The following help article demonstrates a sample approach how to save the changes to the database. In this case, all modified rows will be saved to the database. There is no flag that allows selective storing to the database. The possible solution that I can suggest is to store the previous cell's value when editing a cell. Thus, when you click "Reject" you can restore the old value to the respective cell. Thus, all rows that are modified and rejected will be stored with the previous value as if no change has been performed.

I hope this information helps. Should you have further questions I would be glad to help.

Regards,
Dess
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Ajith
Top achievements
Rank 1
answered on 25 Nov 2017, 07:40 AM
It would be great if you can provide sample code for that
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 27 Nov 2017, 12:20 PM
Hello, Ajith,    

Thank you for writing back. 
 
Here is demonstrated a sample approach how to store the initial cell values and restore them when you click the "Reject" button:
public RadForm1()
{
    InitializeComponent();
 
    DataTable dt = new DataTable();
    dt.Columns.Add("Id", typeof(int));
    dt.Columns.Add("Name", typeof(string));
    for (int i = 0; i < 10; i++)
    {
        dt.Rows.Add(i, "Item" + i);
    }
 
    this.radGridView1.DataSource = dt;
 
    GridViewCommandColumn commandColumn = new GridViewCommandColumn("Reject");
    commandColumn.UseDefaultText = true;
    commandColumn.DefaultText = "Reject";
    radGridView1.MasterTemplate.Columns.Add(commandColumn);
 
    this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
    this.radGridView1.CellValidating += radGridView1_CellValidating;
    this.radGridView1.CommandCellClick += radGridView1_CommandCellClick;
}
 
private void radGridView1_CommandCellClick(object sender, GridViewCellEventArgs e)
{
    foreach (GridViewCellInfo cell in e.Row.Cells)
    {
        if (cell.Tag!=null)
        {
            cell.Value = cell.Tag;
            cell.Tag = null;
        }
    }
}
 
private void radGridView1_CellValidating(object sender, CellValidatingEventArgs e)
{
    if (e.ActiveEditor != null && e.Row.Cells[e.Column.Name].Tag == null)
    {
        //store the initial value
        e.Row.Cells[e.Column.Name].Tag = e.Row.Cells[e.Column.Name].Value;
    }
}

The attached gif file illustrates the implemented behavior. Note that this is just a sample approach and it may not cover all possible cases. Feel free to modify it in a way which suits your requirement best.
 
I hope this information helps. If you have any additional questions, please let me know. 

 Regards,
Dess
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
GridView
Asked by
Ajith
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Ajith
Top achievements
Rank 1
Share this question
or