Dailycode.info

Short solution for short problems

How to rebind a Select on a datatable?

If you have for example a data grid that binds to a dataset or data table and you perform a Select(“”) on the table, it can happen that you lose your bindings and the row is shown but the values not:
  
The code I used was like this:

grdProjectOverview.DataSource = dt.Select("ProjectEndDate > '" + DateTime.Now.ToShortDateString() + "'");

The system finds the correct number of rows and displays them, but the values are not in there. I solved it by using a dataview like this:

grdProjectOverview.DataSource = new DataView(dt, "ProjectEndDate > '" + DateTime.Now.ToShortDateString() + "'", String.Empty, DataViewRowState.CurrentRows);

Now the binding works and the values are shown. It  can really be a pain to get this one solved. Unfortunately as always I do not have time to dig into this deeper. So just the quick solution:
   
 

Copying rows from DataTable A to DataTable B

It's amazing how often we need to copy data from one DataTable to another. This is how you can the job done:

// PRESUPPOSITION: Already instantiated dataTableA
 
DataTable dataTableB = dataTableA.Clone();
dataTableB.TableName = "CloneOfA";
 
// Copy records from dataTableA to dataTableB
foreach (DataRow dr in dataTableA.Rows){    dataTableB.ImportRow(dr);}