0

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. 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

user685590
  • 2,464
  • 5
  • 30
  • 42

2 Answers2

0

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?

stuartd
  • 70,509
  • 14
  • 132
  • 163
  • Humm, O.K , I have been looking at that and other posts on setting the valueType of the DataGridViewColumn but none are really making sense. Mostly I've only used the GUI and .rows to get what I need. I tried dataGridView1.Columns["002160Position"].ValueType = typeof(Int32); which seems like it would work , but throws!. What am I doing/thinking wrong?. – user685590 May 19 '11 at 15:05
0

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);
            }
        }
    }
}
Community
  • 1
  • 1
Jay Riggs
  • 53,046
  • 9
  • 139
  • 151