none
Problem updating a dataTable RRS feed

  • Question

  • I have an in memory DataTable which I want to keep up to date with changes on a datagrid I am using linq to update the dataTable
    I am using an ID number to find the row to change and I only need to change 1 column, 1 row at a time as the user makes changes
    I want to store the changes in the DataTable and save all changes at once

    My problem is that if I make 1 change everything is fine
    but when I change the second row on the Grid the ID in the datatable of the first row becomes the same id as the second row so when I collect the rows to change there are two in my DataView
    my code is simple
    the global declaration for the table
    DataTable iStockTable = new DataTable();
    private void dgDetail_CellEditEnding(object sender, Microsoft.Windows.Controls.DataGridCellEditEndingEventArgs e)
            {
                TextBox txt = e.EditingElement as TextBox;
                string strTxt = txt.Text.ToString();//get the changed value
                int cur = (int)dgDetail.SelectedValue;//get the id
                cls_iTypes.StocktoChangeV = cur;
                cls_iTypes.CurrentItemV = int.Parse(strTxt);
     
                upDateDT(cur, cls_iTypes.CurrentItemV);
       
            }

     

     private void upDateDT(int UDRow, double UDValue)
            {
                     EnumerableRowCollection<DataRow> currViewLinq = from iStockTbl in iStockTable.AsEnumerable()
                                                                where iStockTbl.Field<int>("ID") == UDRow
                                                                select iStockTbl;

                    
                DataView currView = currViewLinq.AsDataView();

                currView.AllowEdit = true;
                currView[0]["StockHolding"] = UDValue;
                currView.AllowEdit = false;
                currView.Dispose();
             }
    The resuls before any changes
    ID Value
    1    Val1
    2    Val2
    the results after the second row would look like this
    ID Value
    2    Val1
    2    Val2


    jnc
    Friday, January 1, 2010 6:54 PM

Answers

  • Yes I am using WPF for my front end but apart from binding variances I do not believe that is the issue.

    So, my vision was to load the data into an in memory datatable bind the datatable to the datagrid update the datatable from the datagrid then save the datatable at the end of each session/category change, simple?! Then I got into my problem when the last Datatable ID changed to the current ID - making two rows with the same ID


    So, I have a couple of comments:

    1) The whole binding to the datagrid *is* a WPF issue, since there would definitely be differences in binding a WPF form vs a WinForm. I don't know if that's contributing to the problem or not (hence my suggestion to post in the WPF forum).

    2) I think the problem probably comes down to this line:
    int cur = (int)dgDetail.SelectedValue;//get the id
    Are you certain that the value returned here is the ID you want? Again, that's a WPF issue, since this is a WPF grid. Have you traced this in the debugger?

    3) If #2 above *does* set the ID into your cur variable correctly, then that would indicate that's something is not right with your LINQ statement. I'm not really much of a LINQ expert, so again this is something I cannot help you with. So, you may also want to take a look at the LINQ forums: http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6a58c359-f38e-4303-a58a-7fb0d5b2e2c4/


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by jnc Tuesday, January 12, 2010 2:17 PM
    Tuesday, January 12, 2010 1:21 PM

All replies

  • It would be a whole lot easier if you used databinding. You can either bind directly to your DataTable, or use a BindingSource. Here are examples of both methodologies:

    Binding to a DataTable:

    // databind the grid
    dgDetail.DataSource = iStockTable;
     
    // databind the TextBox
    txtText.DataBindings.Add("Text", iStockTable, "StockHolding");

    Binding to a BindingSource:
    // create the BindingSource
    BindingSource bsStock = new BindingSource();
    bsStock.DataSource = iStockTable;
     
    // databind the grid
    dgDetail.DataSource = bsStock;
     
    // databind the TextBox
    txtText.DataBindings.Add("Text", bsStock, "StockHolding");

    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Saturday, January 2, 2010 5:38 AM
  • Hello,

     

    Welcome to ADO.NET DataSet forum!

     

    I agree with Bonnie that the BindingSource can make the problem easier to solve.  However, after I researched your sample codes, I found that you are using WPF in .NET 4.0 Beta 2 or Silverlight 3, correct?   Because it is a newly introduced event, http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridcelleditendingeventargs(VS.100).aspx.

     

    To be honest, I am not quite familiar with WPF or SL3 programming, but I know we can also use databinding in WPF or SL3.  Especially in .NET 4.0, the databinding is improved a lot in WPF. 

     

    To solve the problem, could you please provide us with more detailed information?  

     

    Happy New Year!

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, January 4, 2010 2:50 AM
    Moderator
  • thank, you, sorry I did not reply earlier I am not getting alerts and I was tied up with other stuff, I will have a look and let you know
    jnc
    Tuesday, January 5, 2010 5:38 PM
  • thank, you, sorry I did not reply earlier I am not getting alerts

    you are correct I am using wpf (Blend3) as far as I have found I treat data as I normally would but maybe not, I will follow Bonnie's guide and report back
    jnc
    Tuesday, January 5, 2010 5:57 PM
  • I am actually binding using this in my original data load routine

     

    this.dgDetail.DataContext = iStockTable;
    Binding bind = new Binding();
    dgDetail.SetBinding(
    ListView.ItemsSourceProperty, bind);


    Unfortunatly in WPF I dont have DataSource I have DataContext
    and I dont have BindingSource (or Windows.Forms) I have Binding

    But you have given me some ideas I will have a play


    jnc
    Tuesday, January 5, 2010 6:29 PM
  • Hello,

     

    Thank you for your following up.  I am looking forward to your testing results.   If you need any further assistance, please feel free to let me know.  Although I am not a WPF expert, I can consult my colleagues.  J

     

    Also, thanks to Bonnie for your contribution on this case!

     

    Have a nice day!

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, January 6, 2010 9:05 AM
    Moderator
  • I'm not really up-to-speed either on WPF, so forget what I said about BindingSource and DataSource!

    I think the point, whether you use WPF or WinForms, is to use the same binding for both your grid and your other controls (such as TextBoxes). That's about as far as I can go with advice. ;0)

    There is a WPF forum here, I don't know if you knew about it or not: http://social.msdn.microsoft.com/Forums/en-US/wpf/threads


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Wednesday, January 6, 2010 6:59 PM
  • Thanks again both of you,

    I don't think that WPF is relevant in this case as my problem is in the apparant retention of a row in a round trip.

    I will have a poke around and if I find anything I will let you know otherwise I do have another method I can use
    jnc
    Thursday, January 7, 2010 9:30 AM
  • Thank you very much!  If you have some solution, please share it here to benefit more community members.  :)
     

    Have a nice day!

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, January 7, 2010 12:15 PM
    Moderator
  • Hello,


    How is the problem now?  If you have some solutions, could you please share it here?  

    Have a nice day!
     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, January 11, 2010 1:00 AM
    Moderator
  • Many thanks for your interest, I have given up for the moment, I was taking the opportunity of a quiet spell to explore and I now have to get back to more serious stuff.

    I will tell you what I was hoping to achieve though, and maybe you could make a suggestion?
    Yes I am using WPF for my front end but apart from binding variances I do not believe that is the issue.

    The application is a very simple stock taking sheet I load 8 columns, one of them concatenated into a datagrid.
    The user makes changes to just 2 columns one a boolean the other the stock value, a row value and page total value is calculated.
    When the user enters closing stock it needs to be a fast affair so I wanted to avoid any disk activity, just saving on demand or when the user changes categories or closes the app.

    So, my vision was to load the data into an in memory datatable bind the datatable to the datagrid update the datatable from the datagrid then save the datatable at the end of each session/category change, simple?! Then I got into my problem when the last Datatable ID changed to the current ID - making two rows with the same ID

    I am using Linq and C#


    jnc
    Monday, January 11, 2010 9:59 AM
  • Hello,

     

    Thanks for following up.  I can understand the your scenario.  However, I am not quite good at WPF programming.  If it is convenient for you, could you please send me a demo project for further investigation?   My mail address is v-micsun @ microsoft.com. (remove the spaces besides @)

     

    Hope you have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, January 12, 2010 7:24 AM
    Moderator
  • Yes I am using WPF for my front end but apart from binding variances I do not believe that is the issue.

    So, my vision was to load the data into an in memory datatable bind the datatable to the datagrid update the datatable from the datagrid then save the datatable at the end of each session/category change, simple?! Then I got into my problem when the last Datatable ID changed to the current ID - making two rows with the same ID


    So, I have a couple of comments:

    1) The whole binding to the datagrid *is* a WPF issue, since there would definitely be differences in binding a WPF form vs a WinForm. I don't know if that's contributing to the problem or not (hence my suggestion to post in the WPF forum).

    2) I think the problem probably comes down to this line:
    int cur = (int)dgDetail.SelectedValue;//get the id
    Are you certain that the value returned here is the ID you want? Again, that's a WPF issue, since this is a WPF grid. Have you traced this in the debugger?

    3) If #2 above *does* set the ID into your cur variable correctly, then that would indicate that's something is not right with your LINQ statement. I'm not really much of a LINQ expert, so again this is something I cannot help you with. So, you may also want to take a look at the LINQ forums: http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6a58c359-f38e-4303-a58a-7fb0d5b2e2c4/


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by jnc Tuesday, January 12, 2010 2:17 PM
    Tuesday, January 12, 2010 1:21 PM
  • Well, never mind what you do or do not have experience in, your instinct appears to be correct I removed all references to SelectedValue (both in C# and in xaml) and I now have just the correct row(s) updated,

    I do not know the reason, that it works is all that matters for now.


    so, despite your protestations I did post in the correct place

    Thanks

    jnc
    Tuesday, January 12, 2010 2:17 PM
  • hahaha ... well, I think I'm just good at tracking down stuff and making a good guess as to what might be the problem. I should have been a detective instead of a developer. ;0)

    Glad I could point you in the right direction, even though I didn't really know if my assumptions were any good or not. =0)
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Tuesday, January 12, 2010 11:41 PM
  • It's cool, Bonnie!  :)

    Have a nice day! 

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, January 13, 2010 1:25 AM
    Moderator