locked
datatable.select() filteration doesn't work on updated datatable RRS feed

  • Question

  • hi, i'm using cf 2 sp2. 
    i have a datatable and i use datatable.select to filter it by one of its columns, and it filters fine.
    but if i add new rows to the table and then i flter the table, the new rows are not getting into the datarow[] created by the select method.
    i don't understand why.
    if i check dt[0][0] value, it has the new data, but if i check row[0][0] it still has the old value.
    example of what i use:

     

    private DataSet dsCalls;   
    private DataTable dt;  
    ...//inserting data from csv file  
     
    dataRow row = new row();  
    ...//setting the row fields.  
     
     
    dt.Rows.InsertAt(row,i); // insert one row in the ith place  
     
     
    DataRow[] rows = dt.Select("filteration conditon...");  
     

     

    now dt[0] will be different from rows[0].  
    do i need to run a method to indicate that the datatable is updated?
    it seems select is working on the older datatable somehow.
    what do i miss?
    i use datagrid to view the table if it matters.

    thanks.



    • Edited by ronenfe Tuesday, December 30, 2008 10:54 AM
    Tuesday, December 30, 2008 12:20 AM

Answers

  • ok, i pinpointed the problem, it's not that it doesn't filter the new lines, it's that it doesn't kepp the order of the original rowtables.

    so to make it right, ( it took me a lot of time to make it work) you have to do that:
                dtFiltered.Clear();  
                DataRow[] rows = dt.Select("filterexpression...");  
     
                foreach (DataRow dr in rows)  
                    dtFiltered.ImportRow(dr);  
     
                DataView dv = new DataView(dtFiltered);  
                dv.Sort = "Time DESC";  
                dtFiltered = dv.ToTable();  
                this.dataGridCalls.DataSource = dtFiltered
    this willl filter the table and then sort it by datetime descending. don't forget to make sure the time column is of type datetime before you do it, you can do it using:

    dtFiltered.Columns["Time"].DataType = typeof(DateTime);

    • Marked as answer by ronenfe Friday, January 2, 2009 8:30 PM
    Thursday, January 1, 2009 3:19 PM

All replies

  • There is overloaded Select method that allows to specify sorting and rows state. You need to specify CurrentRows for the rows state parameter and Select method should use all the rows from the DataTable. If that does not work for some reason, you can call AcceptChanged method on your DataTable and then call Select.
    Val Mazur (MVP) http://www.xporttools.net
    Tuesday, December 30, 2008 10:50 AM
  • ok, i pinpointed the problem, it's not that it doesn't filter the new lines, it's that it doesn't kepp the order of the original rowtables.

    so to make it right, ( it took me a lot of time to make it work) you have to do that:
                dtFiltered.Clear();  
                DataRow[] rows = dt.Select("filterexpression...");  
     
                foreach (DataRow dr in rows)  
                    dtFiltered.ImportRow(dr);  
     
                DataView dv = new DataView(dtFiltered);  
                dv.Sort = "Time DESC";  
                dtFiltered = dv.ToTable();  
                this.dataGridCalls.DataSource = dtFiltered
    this willl filter the table and then sort it by datetime descending. don't forget to make sure the time column is of type datetime before you do it, you can do it using:

    dtFiltered.Columns["Time"].DataType = typeof(DateTime);

    • Marked as answer by ronenfe Friday, January 2, 2009 8:30 PM
    Thursday, January 1, 2009 3:19 PM