Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to sort datagridview data month and year wise. Where i have two combobox one of month and second for year and a button name "Search". when click on search button it should sort datagridview data according to the value which are selected on combobox.
For example i select month as "January" and year as "2013" so it should show me the data of January 2013 in datagridview.
Posted

1 solution

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:
C#
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;
        }

    // Convert to DataTable for DataView (there is no DataView constructor that works with a List)
    DataTable dt = fullData.ToDataTable();
    DataView dv = new DataView(dt);
    BindingSource bs = new BindingSource();
    bs.DataSource = dv;
    dgv.DataSource = bs;
    dgv.Tag = bs;           // Save the BindingSource for filter declaration.
    // And prep the display column formats.
    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:
C#
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 + "'" : "";
        }
    }
 
Share this answer
 
v3
Comments
Member 10445097 26-Feb-14 11:07am    
its an DESKTOP APPLICATION not a asp page..:(
OriginalGriff 26-Feb-14 11:09am    
Yes...so?
Did you think DataGridView was a web component?
OriginalGriff 26-Feb-14 11:38am    
Ah. I see what you are thinking...
Just because the example on MSDN under the Filter property doesn't *exactly* fit your code, doesn't mean it doesn't work! .NET is pretty well integrated, and what works for a Web control will very often work for a desktop component as well.

See the updated answer, and you'll see what I mean.

Member 10445097 27-Feb-14 1:11am    
hey Paul i had try'ed this its not working cloud u please help me..by getting month name from one combobox and year from second combobox and value dhuld get filter on Button Click Event..
OriginalGriff 27-Feb-14 3:39am    
What did you try? Show the code, and tell me what is doing that you don;t expect, or not doing that you do.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900