English 中文(简体)
Winform 数据网 Numeric Column Sorting
原标题:Winform DatagridView Numeric Column Sorting

我只用一个简单的数据GridView来掌握数据。

我在一栏中做了更正。 但是,在用这个错误的栏目下令时,它错误地命令它。 例如:

启动顺序可以是:

  • 0.56
  • 3.45
  • 500.89
  • 20078.90
  • 1.56
  • 100.29
  • 2.39

终止令如下:

  • 0.56
  • 100.29
  • 1.56
  • 20078.90
  • 2.39
  • 3.45
  • 500.89

你们可以看到,它从头几个开始。 然后以这种方式下达命令。

我认为,可能的话,我可以把这个栏列放在另一个“ColumnType”上,并且可以自动这样做。 但没有“数量”或“数量”栏。

我是在MSDN研究这一问题的,我可以找到我在数据GridView上可以使用的“ort”方法。 但是,这一解释比我的头部少一点,而例子中只字不提,因此,我看一下我为什么要改变事情。

任何帮助都会受到高度赞赏。

最佳回答

可以通过在数据GridView的SortCompare活动上添加一部手稿来解决这个问题,其代码如下:

private void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
{
    if (e.Column.Index == 0)
    {
        if (double.Parse(e.CellValue1.ToString()) > double.Parse(e.CellValue2.ToString()))
        {
            e.SortResult = 1;
        }
        else if (double.Parse(e.CellValue1.ToString()) < double.Parse(e.CellValue2.ToString()))
        {
            e.SortResult = -1;
        }             
        else
        {
            e.SortResult = 0;
        }
        e.Handled = true;
   }
}

从MSDN来看,对SortResult数值的描述如下:

Less than zero if the first cell will be sorted before the second cell; zero if the first cell and second cell have equivalent values; greater than zero if the second cell will be sorted before the first cell.

请注意,在我的测验中,唯一的数字栏是第一个(指数0),因此,我对栏目索引进行了核对。

此外,根据您的需要和数据,你可能希望改进我的代码,例如,如果你有非数字数据,则我的准则将成为一个例外。

您可能看到这一点,但here是同数据GridView分类定制的MSDN网页链接。 你说,他们只是处理案文。

问题回答

我有同样的问题。 我在前面提到戴维·大会堂时试图利用活动手。 我在界定数据GridView时使用了“估价财产”。 如今,这种做法是双重的,不需要习俗活动手法。

dataGridView1.Columns[int index].ValueType = typeof(double);

您也可以使用该栏的格式。

dataGridView2.Columns[int index].DefaultCellStyle.Format = string format;

异构体类型在比较功能方面有所建构,可用作SortCompare活动的结果。

    private void dataGridView_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
    {
        if (e.Column.Index == 0)
        {
            e.SortResult = int.Parse(e.CellValue1.ToString()).CompareTo(int.Parse(e.CellValue2.ToString()));
            e.Handled = true;
        }
    }

这当然假定你知道从数据GridView开始的类型。

Your database column type should be int or double or float, not varchar or something.... So you have to change your value-type in the database... You no need to write any code or something it directly sort when you click on the column Header...

它是按性质分类的。 您需要显示该栏的类型,以便了解哪些比较操作者可以申请。

(你需要打上你的数据集线,我认为这将奏效)

您的问题是<代码>数据网格表,正在通过图表分类。 引申string to float 当你将这个小组复制到<代码>数据网时。





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签