我写了一个为DataTable中的时间一列以冒泡法排序,有A,B,temp,如果A>B,那么,temp=A,A=B,B=temp;但是,在进行到A=B时,temp也一起变成了B,请问名位应该如何解决啊?

解决方案 »

  1.   

    DataRow a = Table1.Rows[0];
    DataRow temp = a;
    DataRow b = temp;//上面3行实际上是同一行
    把引用类型赋值给一个变量,获得的是这个对象本身
      

  2.   

    this.dt = (DataTable)ViewState["dt"];
    int i,j;
    DataRow temp = dt.NewRow();
    j=1; 
    while((j<dt.Rows.Count)) 

             for(i=0;i<dt.Rows.Count-j;i++) 
             { 
         if(dt.Rows[i]["checkdate"].ToString() == "")
        {
    this.dt.Rows[i]["checkdate"] = Convert.ToDateTime("1900-01-01 01:01:01");
        }
        if(dt.Rows[i+1]["checkdate"].ToString() == "")
       {
            this.dt.Rows[i+1]["checkdate"] = Convert.ToDateTime("1900-01-01 01:01:01");
       }
       if(Convert.ToDateTime(dt.Rows[i]["checkdate"]).CompareTo(Convert.ToDateTime(dt.Rows[i+1]["checkdate"])) < 0) 
       { 
    temp=dt.Rows[i];
    dt.Rows[i]["bianhao"]=dt.Rows[i+1]["bianhao"]; 
    dt.Rows[i]["registerdate"]=dt.Rows[i+1]["registerdate"]; 

    dt.Rows[i+1]["bianhao"]=temp["bianhao"]; 
    dt.Rows[i+1]["registerdate"]=temp["registerdate"]; 
       } 

    j++; 
          }
          this.DataGrid1.DataSource = dt;
          this.DataGrid1.DataBind();
    }这是代码!~~~~~~~~~~
      

  3.   

    不用temp的话写入下代码
    a=a+b
    b=a-b
    a=a-b
      

  4.   

    实不理解为什么兄弟这么想?DATAGrid中有按照某字段排序的SortException这个可以实现你想要的效果吧?,如果实现不了,那把DataTable.Defaultview来操作,有RowFilter
      

  5.   

    没办法才这样做的,因为我在这个部分的代码不止这一点,我怕贴出来把大家吓坏了,所以在这里贴的是剪了一部分的代码.我原本也是用DataGrid自带的排序功能的,可是不知道为什么,我改了好多次,它每次排的时间都排的乱七八糟,所以才出此下策!~
      

  6.   

    temp=dt.Rows[i];改成
    temp=dt.NewRow()
    temp["bianhao"]=dt.Rows[i]["bianhao"]; 
    temp["registerdate"]=dt.Rows[i]["registerdate"]; 
    试试看。temp=dt.Rows[i];实际上,temp和dt.Rows[i]应该是一个东西,是统一行,改一个,另外一个自然跟着改了。
      

  7.   

    回复:skywind_jk(天风) 
         谢谢,此问题已经解决了,是因为要强制把dt.row.DataType设置为dateTime型的,光用convert.to()有时不是很管用.谢谢您的关注!~
      

  8.   

    此问题已结决于
    http://community.csdn.net/Expert/TopicView.asp?id=5226556