C # column sorting

  private void button2_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("AAA", typeof(string));//0
            dt.Columns.Add("BBB", typeof(string));//1
            dt.Columns.Add("CCC", typeof(string));//2
            dt.Columns.Add("DDD", typeof(string));//3
            dt.Columns.Add("EEE", typeof(string)); //4
            dt.Columns.Add("FFF", typeof(string));//5
            dt.Columns.Add("GGG", typeof(string));//6
            dt.Columns.Add("HHH", typeof(string));//7

            DataRow dr = dt.NewRow();
            dr["AAA"] = "000";
            dr["BBB"] = "111";
            dr["CCC"] = "222";
            dr["DDD"] = "333";
            dr["EEE" ] = " 444 " ; 
            DR [ " FFF " ] = " 555 " ; 
            DR [ " GGG " ] = " 666 " ; 
            DR [ " HHH " ] = " 7777 " ; 
            dt.Rows.Add (DR); 

            dt .Columns [ 0 ] .SetOrdinal ( . 3 ); // set the index of the column; the smaller index in the foregoing 
            dt.Columns [ . 3 ] .SetOrdinal ( 0 );//Set the index of the column; the smaller index in the foregoing 
   
            // dt.Columns [. 1] .SetOrdinal (. 1); // Set the index of the column; small index front
             // dt.Columns [2] .SetOrdinal (2 ); // set the index of the column; small index front
             // dt.Columns [. 5] .SetOrdinal (2); // set the index of the column; the smaller index in the foregoing 

            // for (int I = 0 ; I <dt.Columns.Count; I ++)
             // {
             //     dt.Columns [I] .SetOrdinal (0); // set the index of the column; the smaller index in the foregoing
             // } 

            BOOL in Flag = to false ; 
            List < String > Colums_list = get_ColumnsName ();
             for (int i = 0; i < dt.Columns.Count; i++)
            {
                for (int j = 0; j < Colums_list.Count; j++)
                {
                    if (dt.Columns[i].ColumnName == Colums_list[j])
                    {
                        dt.Columns[i].SetOrdinal(j); //设置该列的索引
                        break;
                    }
                }
            }

        }
        public List<string> get_ColumnsName()
        {
            List<string> Column_list = new List<string>();
            Column_list.Add("HHH");
            Column_list.Add("FFF");
            Column_list.Add("CCC");
            Column_list.Add("BBB");
            Column_list.Add("EEE");
            Column_list.Add("DDD");
            return Column_list;
        }
Of 0 to 7, when 
the first one is automatically set to 0; 7th automatically set to 6; 

of 0 to 3, when 
the first one is automatically set to 0; 3rd automatically set to 2 
// First get the original ordering

 

 

Guess you like

Origin www.cnblogs.com/enych/p/11883869.html