datatable随机排序

来源:互联网 发布:不成乎名 编辑:程序博客网 时间:2024/04/25 01:05
这个是在我的一个小程序中用到的,先帖上来,呵呵
  1.                     int tmp;
  2.                     int allCount = Phones.dtResult.Rows.Count;
  3.                     Random rand = new Random();
  4.                     string[] drtmp = new string[3];
  5.                     for (int i = 0; i < allCount; i++)
  6.                     {
  7.                         tmp = rand.Next(0, allCount - 1);
  8.                         drtmp[0] = Phones.dtResult.Rows[i][0].ToString();
  9.                         drtmp[1] = Phones.dtResult.Rows[i][1].ToString();
  10.                         drtmp[2] = Phones.dtResult.Rows[i][2].ToString();
  11.                         Phones.dtResult.Rows[i][0] = Phones.dtResult.Rows[tmp][0].ToString();
  12.                         Phones.dtResult.Rows[i][1] = Phones.dtResult.Rows[tmp][1].ToString();
  13.                         Phones.dtResult.Rows[i][2] = Phones.dtResult.Rows[tmp][2].ToString();
  14.                         Phones.dtResult.Rows[tmp][0] = drtmp[0].ToString();
  15.                         Phones.dtResult.Rows[tmp][1] = drtmp[1].ToString();
  16.                         Phones.dtResult.Rows[tmp][2] = drtmp[2].ToString();
  17.                     }