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:
   
 

Retrieving DataItems on IndexChanged event

Recently I was looking for a way to retrieve a particular column based on it's column name (instead of fixed cellnumber) when a row in a GridView has been selected. I came up with the following which does the trick:

protected void gvGeneratedReports_SelectedIndexChanged(object sender, EventArgs e)
{
    GridView gv = (GridView)sender;
    DataRowView rowView = (DataRowView)gv.SelectedRow.DataItem;
    string outputType = rowView["OutputType"].ToString();
}