The easiest way is to use a
BindingSource[
^] - you sepecify the DataDource of the BindingSource, then set the DataGridView.DataSource to the BindingSource.
You then set the Filter property to select the displayed records:
http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.filter(v=vs.110).aspx[
^]
An example from my code:
private static void FillCalculated(List<CalculatedRepEntry> fullData, List<repEntry> repData, DataGridView dgv)
{
MovingAverage aveAll = new MovingAverage();
MovingAverage ave30 = new MovingAverage(30);
MovingAverage ave10 = new MovingAverage(10);
int lastRep = int.MinValue;
foreach (RepEntry rep in repData)
{
fullData.Add(new CalculatedRepEntry(rep, lastRep, aveAll, ave30, ave10));
lastRep = rep.RBasis;
}
DataTable dt = fullData.ToDataTable();
DataView dv = new DataView(dt);
BindingSource bs = new BindingSource();
bs.DataSource = dv;
dgv.DataSource = bs;
dgv.Tag = bs;
dgv.Columns["Est1Mav"].DefaultCellStyle.Format = "dd/MM/yyyy";
dgv.Columns["Est1M30Av"].DefaultCellStyle.Format = "dd/MM/yyyy";
dgv.Columns["Est1M10Av"].DefaultCellStyle.Format = "dd/MM/yyyy";
FormatNumericColumn(dgv, "RBasis");
FormatNumericColumn(dgv, "Delta");
FormatNumericColumn(dgv, "Average");
FormatNumericColumn(dgv, "Av30Day");
FormatNumericColumn(dgv, "Av10Day");
}
Then to filter by date:
private void dtpFromDate_ValueChanged(object sender, EventArgs e)
{
DateTime limit = dtpFromDate.Value.Date;
BindingSource bs = dgvRBasis.Tag as BindingSource;
if (bs != null)
{
bs.Filter = DateTime.Now.Date > limit ? "Date > '" + limit + "'" : "";
}
}