none
filter a datatable and update columns RRS feed

  • Question

  • <!-- /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} @page Section1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1 {page:Section1;} -->


    I want to find a datarow in the datatable, and update some columns, I have some code but not sure on the rest or if i could use linq here, also , would updates made this way be saved to the orig datatable? :


      string expression = "temp _Id='{0}'" ;

            string filter = string .Empty;

            dt = (DataTable )Session[Table];

            try

            {

                foreach (DetailDataContract detial in insertDetails)

                {

                    //Filter datatable

                    filter = string .Format(expression, detial.TempId);

                    row =dt.Select(filter);

                   

                    //Update status

                    if (row != null )

                    {

                        DataRow r =  row.First();

                                            r[0]=detial.ErrorId

                                            r[1]=detial.ErrMsg

                    }

                }

            }


    McC
    Tuesday, December 15, 2009 5:55 PM

Answers

  • You should also use an EndEdit():

                   if (row != null )
                   {
                        DataRow r =  row.First();

                                            r[0]=detial.ErrorId
                                            r[1]=detial.ErrMsg

                                            r.EndEdit();
                    }

    As far as your other question, will this automatically update the database, no it won't. But now you DataSet will have changes in it, and you can update your database yourself. It depends on how you got your data to begin with, but you'd use either a DataAdapter or TableAdapter. How to update the database is probably a topic for another post.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Wednesday, December 16, 2009 3:08 PM