如何排序网格视图数据月和年明智 [英] How to sort grid view Data Month and year wise

查看:60
本文介绍了如何排序网格视图数据月和年明智的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何对月份和年份的datagridview数据进行排序。我有两个组合框一个月和第二个年份和一个按钮名称搜索。当点击搜索按钮时,它应该根据在组合框上选择的值对datagridview数据进行排序。

例如我选择月份为1月,年份为2013​​,所以它应该显示数据2013年1月在datagridview。

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.

推荐答案

最简单的方法是使用BindingSource [ ^ ] - 您指定BindingSource的DataDource,然后将DataGridView.DataSource设置为BindingSource。

然后设置Filter属性以选择显示的记录: http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.filter(v = vs.110)的.aspx [ ^ ]



我的代码中的一个例子:

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;
        }

    // 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:

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 + "'" : "";
        }
    }


这篇关于如何排序网格视图数据月和年明智的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆