Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Xml dataGridView排序问题_C#_Xml_Datagridview - Fatal编程技术网

C# Xml dataGridView排序问题

C# Xml dataGridView排序问题,c#,xml,datagridview,C#,Xml,Datagridview,我正在将XML文件读入一个数据集,然后显示在VS中的dataGridView上。将生成XML文件,并假设我无法更改它。当我显示并尝试按升序排列位置编号列时,它将无法正确排列,例如1、2、12、14将排列为1、12、14、2。我认为,如果我在单个数字的末尾插入0,它将起作用。但我希望有一个快速的解决方法或选择 dataGridView 位置 1. 10 11 12 13 14 15 16 17 18 19 2您想将设置为数字数据类型-当前它被排序为字符串 编辑:如果要将数据集指定为数据源,则不能更

我正在将XML文件读入一个数据集,然后显示在VS中的dataGridView上。将生成XML文件,并假设我无法更改它。当我显示并尝试按升序排列位置编号列时,它将无法正确排列,例如1、2、12、14将排列为1、12、14、2。我认为,如果我在单个数字的末尾插入0,它将起作用。但我希望有一个快速的解决方法或选择

dataGridView 位置 1. 10 11 12 13 14 15 16 17 18 19 2

您想将设置为数字数据类型-当前它被排序为字符串


编辑:如果要将数据集指定为数据源,则不能更改列的valuetype。尽管您无法控制XML,但也许可以更改数据集中的列类型?

您正在寻找的是在数据集中的DataTable中为列实现for

正如您所注意到的,可用于数据集/数据表的本机排序选项非常有限。但是,您可以使用
AsEnumerable().OrderBy
方法在DataTable中指定要排序的列,并且可以为此列中的数据传入一个。我在下面包含了代码,演示了:;在表单中添加一个按钮和一个DataGridView,您应该能够进行测试

using System;
using System.Collections.Generic;
using System.Data;
using System.Runtime.InteropServices;
using System.Security;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1() {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {
            var dt = new DataTable();
            dt.Columns.Add("Col1");
            dt.Columns.Add("Col2");
            dt.Rows.Add(new object[] { "row 1", "20" });
            dt.Rows.Add(new object[] { "row 2", "2" });
            dt.Rows.Add(new object[] { "row 3", "10" });
            dt.Rows.Add(new object[] { "row 4", "1" });

            var ds = new DataSet();
            ds.Tables.Add(dt);

            var query = ds.Tables[0].AsEnumerable().OrderBy(r => r.Field<string>("Col2"), new NaturalStringComparer());

            dataGridView1.DataSource = query.AsDataView();
        }

        //
        // Comparer for natural sort.
        //   https://stackoverflow.com/questions/248603/natural-sort-order-in-c
        //   ** see answers for warnings on this implementation **
        //
        [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
        private static extern int StrCmpLogicalW(string psz1, string psz2);

        [SuppressUnmanagedCodeSecurity]
        internal static class SafeNativeMethods
        {
            [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
            public static extern int StrCmpLogicalW(string psz1, string psz2);
        }

        public sealed class NaturalStringComparer : IComparer<string>
        {
            public int Compare(string a, string b) {
                return SafeNativeMethods.StrCmpLogicalW(a, b);
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统数据;
使用System.Runtime.InteropServices;
使用系统安全;
使用System.Windows.Forms;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
公共表格1(){
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e){
var dt=新数据表();
dt.列添加(“第1列”);
dt.列添加(“Col2”);
Add(新对象[]{“row1”,“20”});
Add(新对象[]{“row2”,“2”});
Add(新对象[]{“row3”,“10”});
Add(新对象[]{“row4”,“1”});
var ds=新数据集();
ds.Tables.Add(dt);
var query=ds.Tables[0].AsEnumerable().OrderBy(r=>r.Field(“Col2”),新的NaturalStringComparer());
dataGridView1.DataSource=query.AsDataView();
}
//
//自然排序的比较器。
//   https://stackoverflow.com/questions/248603/natural-sort-order-in-c
//**有关此实施的警告,请参阅答案**
//
[DllImport(“shlwapi.dll”,CharSet=CharSet.Unicode)]
私有静态外部内部结构(字符串psz1,字符串psz2);
[SuppressUnmanagedCodeSecurity]
内部静态类SafeNativeMethods
{
[DllImport(“shlwapi.dll”,CharSet=CharSet.Unicode)]
公共静态外部内部结构(字符串psz1,字符串psz2);
}
公共密封类自然林比较器:IComparer
{
公共整数比较(字符串a、字符串b){
返回安全方法。strcplulogicalw(a,b);
}
}
}
}

Humm,好吧,我一直在看关于设置DataGridViewColumn的valueType的文章和其他文章,但没有一篇真正有意义。大多数情况下,我只使用GUI和.rows来获取所需内容。我尝试了dataGridView1.Columns[“002160Position”].ValueType=typeof(Int32);这似乎是可行的,但它会抛出!。我做错了什么/想错了什么?。