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

How refresh GrivView (linq2sql)?

6 Answers 224 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Robert Stuczynski
Top achievements
Rank 1
Robert Stuczynski asked on 05 Feb 2010, 01:42 PM
For more clear I recorded short video how it work www.8080.pl/download/dgv.wmv
I have radgridview with a datasource that is linking to a linq2sql:

TestowaBazaDataContext db = new TestowaBazaDataContext();  
 
      radGridView1.DataSource = db.tabelaZlecenias; 

When I insert a new record I want refresh the grid again:

private void radGridView1_RowsChanged(object sender, Telerik.WinControls.UI.GridViewCollectionChangedEventArgs e)  
    {  
      TestowaBazaDataContext db = new TestowaBazaDataContext();  
 
      if ((e.Action == Telerik.WinControls.Data.NotifyCollectionChangedAction.ItemChanged) &&  
          ((int) radGridView1.Rows[e.NewStartingIndex].Cells["idZlecenia"].Value == 0))  
      {  
        //new record  
        if (radGridView1.Rows[e.NewStartingIndex].Cells["Zlecenie"].Value != null)  
        {  
          var nowa = new tabelaZlecenia()  
                       {  
                         Zlecenie = (int) radGridView1.Rows[e.NewStartingIndex].Cells["Zlecenie"].Value  
                       };  
          db.tabelaZlecenias.InsertOnSubmit(nowa);  
          db.SubmitChanges();         
 
      TestowaBazaDataContext dbZ = new TestowaBazaDataContext();  
      radGridView1.DataSource = dbZ.tabelaZlecenias;
 
        }  
      }  
    } 
 
But I see only error.
If I call if from button's event and it work correctly:

    private void button1_Click(object sender, EventArgs e)  
    {  
      //I need refresh data by Linq in radGridView1  
      TestowaBazaDataContext dbZ = new TestowaBazaDataContext();  
      radGridView1.DataSource = dbZ.tabelaZlecenias;  
    } 

But I need automatic refresh after I added records. How and where I can do it?

6 Answers, 1 is accepted

Sort by
0
Dobry Zranchev
Telerik team
answered on 08 Feb 2010, 08:34 AM
Hello Robert Stuczynski,

Thank you for your question. We know about this issue. In some of future releases this will be solved. Currently, you can avoid it in this way:

this.radGridView1.MasterGridViewTemplate.Update(GridUINotifyAction.Reset);

This row will refresh RadGridView control. If you have some additional questions feel free to ask.

Regards,
Dobry Zranchev
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
0
Robert Stuczynski
Top achievements
Rank 1
answered on 08 Feb 2010, 09:16 AM
Thanks for your reply. But it not resolve my problem :(
I think it will be better if I attach example. It is ok? Or do you prefere see error?
Please create data base and is table tabelaZlecenia, it has only two field: idZlecenia, Zlecenie.
If you need more detali please ask.

Source is: http://www.8080.pl/wm/testter.zip
0
Dobry Zranchev
Telerik team
answered on 10 Feb 2010, 02:11 PM
Hello Robert Stuczynski,

Sorry that I did not understand your issue at first. Please consider this code snippet:

DataClasses1DataContext db;
public Form1()
{
  InitializeComponent();
  db = new DataClasses1DataContext();
 
  radGridView1.DataSource = db.tabelaZlecenias;
 
  this.radGridView1.CurrentRowChanged += new CurrentRowChangedEventHandler(radGridView1_CurrentRowChanged);
}
 
private bool submit = false;
void radGridView1_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e)
{
    this.submit = !(e.CurrentRow is GridViewNewRowInfo);
    if (this.submit)
    {
        this.submit = false;
        db.SubmitChanges();
        this.submit = true;
    }
}
 
private void radGridView1_RowsChanged(object sender, GridViewCollectionChangedEventArgs e)
{
    if (this.submit)
        db.SubmitChanges();
}

First you need just one data context. You have to subscribe to two events: RowChanged and RowsChanged. The code above shows how to submit changes in the existing data context. When you submit changes you have to get RowsChanged again and this is the reason that you should to set the flag to false. If you change an existing row, you need only RowsChanged.

Best wishes,
Dobry Zranchev
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
0
Robert Stuczynski
Top achievements
Rank 1
answered on 11 Feb 2010, 10:26 AM

I thing I will attach the whole code for you to better understand. When I launch my application I get the RadGrid. In the first row there is always the possibility to add a new rekord to the table in the database.
I add the record, and when I move the focus out of the insert box and MANUALY reset the dataContext for the Grid by clicking the button in my sample application the changes are reflected.
When I don't click the button the new row is added but the Id in the first column - which is databse generated - remain with the value 0. This indicates that the Grid has not refreshed.
So what I want to achieve is: I open the application, add a new value, and when I move the focus out of the box the new row is added to the databases and the dataContext is "refreshed" so I can the the Id which the database has assigned for that particular row.


Could you run my code ( http://www.8080.pl/wm/testter.zip ) and then check it and finaly put there whole source. Because I am confused and I am not sure that I correct understand you.
Thanks
0
Accepted
Dobry Zranchev
Telerik team
answered on 12 Feb 2010, 08:39 AM
Hi Robert Stuczynski,

We saw that this case is not covered in our example. You could subscribe to the event LostFocus of the grid and submit changes there. Look at the attached example. We hope that this solution will resolve the issue. If you have additional questions feel free to ask.

Regards,
Dobry Zranchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Robert Stuczynski
Top achievements
Rank 1
answered on 12 Feb 2010, 10:08 AM
Thank you very much for the great help! I see it work.
In addition, your approach is completely different, I did not know this.
It is much easier! I am a beginner and it is very helpful for me. Thanks
Tags
GridView
Asked by
Robert Stuczynski
Top achievements
Rank 1
Answers by
Dobry Zranchev
Telerik team
Robert Stuczynski
Top achievements
Rank 1
Share this question
or