Can';将C#DataGridView列作为整数进行t排序

Can';将C#DataGridView列作为整数进行t排序,c#,windows-forms-designer,C#,Windows Forms Designer,我的windows窗体应用程序中有一个DataGridView gridFilas,有两列,第二列始终包含可以转换为整数的字符串。当我单击对其进行排序时,它被排序为字符串,结果如下: 1,11,2,22 但我需要将其排序为整数,比如: 1,2,11,22 我已经尝试了的所有答案,但没有一个有效,顺便说一句,接受的答案无效,因为SortCompare事件没有因为以下原因被触发 因此,到目前为止,我尝试添加一个ColumnHeaderMouseClick,并使用以下内容对其进行排序: private

我的windows窗体应用程序中有一个
DataGridView gridFilas
,有两列,第二列始终包含可以转换为整数的字符串。当我单击对其进行排序时,它被排序为字符串,结果如下:

1,11,2,22

但我需要将其排序为整数,比如:

1,2,11,22

我已经尝试了的所有答案,但没有一个有效,顺便说一句,接受的答案无效,因为
SortCompare
事件没有因为以下原因被触发

因此,到目前为止,我尝试添加一个
ColumnHeaderMouseClick
,并使用以下内容对其进行排序:

private void gridFilas_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{

    // this for loop has been added in a vain hope of converting all elements to integer to see if it works...
    for (int i = 0; i < gridFilas.Rows.Count; i++)
    {
        string v = gridFilas.Rows[i].Cells[1].Value.ToString();
        gridFilas.Rows[i].Cells[1].Value = Convert.ToInt32(v);
    }

    if (queuesSortedAscending)
    {
        gridFilas.Sort(gridFilas.Columns["Tamanho_Fila"], System.ComponentModel.ListSortDirection.Descending);
    }
    else
    {
        gridFilas.Sort(gridFilas.Columns["Tamanho_Fila"], System.ComponentModel.ListSortDirection.Ascending);
    }
    queuesSortedAscending = !queuesSortedAscending;

}
每当我向数据源添加新行时,我保证它被解析为int:

DataRow qlinha = dsComponentes.Tables["Queue"].NewRow();
qlinha["Nome_Fila"] = process;
qlinha["Tamanho_Fila"] = Int32.Parse(status);
dsComponentes.Tables["Queue"].Rows.Add(qlinha);
我还尝试预先更改列的数据类型:

dsComponentes.Tables["Queue"].Columns["Tamanho_Fila"].DataType = typeof(int);

所以,我不知道还能做什么,我只需要把它排序为整数而不是字符串。欢迎使用任何解决方案。

您从不显示要绑定到的
数据表的创建位置(
dscomponents.Tables[“Queue”]
),但添加该列时,您应该能够指示它是
int

var dataTable=new dataTable();
dataTable.Columns.Add(“ColumnName”,typeof(int));
这将导致绑定的
DataGridView
将该列作为整数排序

如果使用设计器创建
数据集
,则Columns Collection Editor中似乎也有一个
数据类型
。您可以在该列上将类型设置为
System.Int32
,它将按照您的预期进行排序

使用系统;
使用System.Collections.Generic;
使用系统数据;
使用System.Linq;
使用System.Threading.Tasks;
使用System.Windows.Forms;
命名空间WindowsFormsApp1
{
静态类程序
{
/// 
///应用程序的主要入口点。
/// 
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var frm=新表单()
{
Text=“数据网格视图表单”,
Name=“DataGridViewForm”,
尺寸=新系统图纸尺寸(400400)
};
var dataGridView1=新的DataGridView();
var dataTable=新的dataTable();
dataGridView1.DataSource=dataTable;
dataGridView1.Dock=DockStyle.Fill;
dataTable.Columns.Add(“Id”,typeof(int));
Add(“IdAsString”,typeof(string));
var r1=dataTable.NewRow();
r1[“Id”]=1;
r1[“IdAsString”]=“1”;
dataTable.Rows.Add(r1);
var r2=dataTable.NewRow();
r2[“Id”]=11;
r2[“IdAsString”]=“11”;
dataTable.Rows.Add(r2);
var r3=dataTable.NewRow();
r3[“Id”]=2;
r3[“IdAsString”]=“2”;
dataTable.Rows.Add(r3);
var r4=dataTable.NewRow();
r4[“Id”]=22;
r4[“IdAsString”]=“22”;
dataTable.Rows.Add(r4);
frm.Controls.Add(dataGridView1);
应用程序运行(frm);
}
}
}

最简单的方法是使用nuget软件包:

它与标准的.net BindingList非常相似,只是它包含要排序的函数

  • 将BindingListView nuget导入到项目中
  • 使用Visual Studio designer创建表单
  • 使用工具箱将BindingSource添加到表单中
  • 转到此BindingSource的属性,选择DataSource,添加项目数据源,选择Object并添加要在DataGridView中显示的项类
  • 现在使用工具箱添加DataGridView
  • 在DataGridView的属性中,选择BindingSource作为数据源
现在,类的所有公共属性都将在DataGridView中显示为列。相应地编辑列:删除不使用的列

在我的示例中,我将对一系列人员进行排序:

class Person
{
    public int Id {get; set;}
    public string Name {get; set;}
    public DateTime BirthDate {get; set;}
}
去你的一年级。我们将从nuget包中添加BindingListView作为成员。 在构造函数中,我们将把它分配给分配给DataGridView的bindingsource

class Form
{
    // the BindingListView from the nuget package:
    private readonly BindingListView<Person> sortableBindingListView;

    // constructor
    public Form1()
    {
        InitializeComponent();

        // make sure there is a Components Container, that will dispose
        // the components upon disposal of the form
        if (this.components == null)
        {
            this.components = new System.ComponentModel.Container();
        }

        // construct the sortable BindingListView for Persons:
        this.sortableBindingListView = new BindingListView<Person>(this.components);

        // create some Persons:
        var persons = new Person[]
        {
            new Person{Id = 1, Name = "F", BirthDate = new DateTime(2000, 1, 1)},
            new Person{Id = 2, Name = "A", BirthDate = new DateTime(1998, 4, 7)},
            new Person{Id = 3, Name = "C", BirthDate = new DateTime(2011, 3, 8)},
            new Person{Id = 4, Name = "Z", BirthDate = new DateTime(1997, 2, 3)},
            new Person{Id = 5, Name = "K", BirthDate = new DateTime(2003, 9, 5)},
        };

        // Assign the DataSources:
        this.sortableBindingListView.DataSource = persons;
        this.dataGridView1.DataSource = this.sortableBindingListView;
    }
}
BindingListView的一个优点是过滤选项:

// show only items where Name not null:
this.SortableBindingSource.ApplyFilter(person => person.Name != null);

// remove the filter:
this.SortableBindingSource.RemoveFilter();

我不确定在应用或删除筛选器后是否需要刷新()。

第二列始终包含可转换为整数的字符串
DGV列已键入-使用整数列或使用自然排序的自定义分类器。关于这两个主题的大量文档和帖子
private Person SelectedPerson
{
    get {return ((ObjectView<Person>)this.SortableBindingSource.Current)?.Object; }
}

private void DisplayPersons (IEnumerable<Person> personsToDisplay)
{
    this.SortableBindingSource.DataSource = personsToDisplay.ToList();
    this.SortableBindingSource.Refresh(); // this will update the DataGridView
}

private IEnumerable<Person> DisplayedPersons
{
    get {return this.SortableBindingSource; }
    // BindingListview<T> implements IEnumerable<T>
}
// sort columnPropertyA in descending order:
this.SortableBindingSource.Sort(this.columnPropertyA.ListsortDirection.Descending);
// show only items where Name not null:
this.SortableBindingSource.ApplyFilter(person => person.Name != null);

// remove the filter:
this.SortableBindingSource.RemoveFilter();