开发者

Xml dataGridView Ordering Problem

开发者 https://www.devze.com 2023-03-06 20:52 出处:网络
I am reading in an XML file to a dataset then displayed on a dataGridView in VS. The xml file will be generated and assume I can\'t change it. When I display and try to order the position number colum

I am reading in an XML file to a dataset then displayed on a dataGridView in VS. The xml file will be generated and assume I can't change it. When I display and try to order the position number column by ascending order it will not properly order for example 1, 2, 12, 14 would be arranged 1,12,14,2 I presume if I stuck a 0 on the end of the single digits it would work.开发者_JAVA百科 But I was hoping there is a quick work around or option ?.

dataGridView Position 1 10 11 12 13 14 15 16 17 18 19 2


What you're looking for is implementing the Natural Sort Order for a column in a DataTable in your DataSet.

As you've noticed the native sorting options available for DataSets/DataTables are pretty limited. However, you can use the AsEnumerable().OrderBy method to specify a column in a DataTable to sort and you can pass in an IComparer that implements the natural sort order for the data in this column. I included code below that demonstrates; add a Button and a DataGridView to a Form and you should be able to test.

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


You want to set the ValueType of the DataGridViewColumn to a numeric datatype - currently it's being sorted as a string..

Edit: if you're assigning the dataset as a DataSource then you can't change the column's valuetype. Although you can't control the XML, perhaps you can change the column type in the dataset?

0

精彩评论

暂无评论...
验证码 换一张
取 消