空datatable的datarow居然不为空

datatable的定义:
            DataTable shopbox = new DataTable();
            //DataColumn[] dc = new DataColumn[1];
            shopbox.Columns.Add("productid", typeof(int));
            shopbox.Columns.Add("productname", typeof(string));
            shopbox.Columns.Add("pricebuy", typeof(int));
            shopbox.Columns.Add("productnumber", typeof(int));
            shopbox.Columns.Add("subtotal", typeof(int));
            //dc[0] = shopbox.Columns["productid"];
            //shopbox.PrimaryKey = dc;
            UniqueConstraint uc = new UniqueConstraint(shopbox.Columns["productid"], true);
            shopbox.Constraints.Add(uc);
            Session["shopbox"] = shopbox;
想datatable里插入数据
       DataTable dt = (DataTable)Session["shopbox"];
        //try
        //{
          GridViewRow gRow = GridView1.SelectedRow;
          string tempid = gRow.Cells[0].Text.ToString(); 
          int chayeid = Convert.ToInt16(tempid) ;
          DataRow Dr = dt.Rows.Find(chayeid);
          bool check1 = true;
          if (Dr==null)
            {
               check1 = false;
               string temp1 = gRow.Cells[3].Text;
               int dontindex = temp1.IndexOf(".");
               string temp2 = temp1.Substring(1,dontindex-1);
               int temp3 = int.Parse(temp2);
               DataRow drtemp = dt.NewRow();
               drtemp["productid"] = gRow.Cells[0].Text;
               drtemp["productname"] = gRow.Cells[1].Text;
               drtemp["pricebuy"] = temp3;
               drtemp["productnumber"] = 1;
               drtemp["subtotal"] = temp3;
               dt.Rows.Add(drtemp);
               TextBox1.Text = "1";
             }
          else
           {
               if (check1)
               {
                   Dr["productnumber"] = (int)(Dr["productnumber"]) + 1;
                   Dr["subtotal"] = (int)(Dr["subtotal"]) + (int)(Dr["pricebuy"]);
                   Dr.AcceptChanges();
                   dt.AcceptChanges();
               }
            }
           Session["shopbox"] = dt;
        //}
        //catch { Response.Write("something is wrong"); }
调试的时候,第一次插入数据居然认为Dr!=null   而且 Dr["productnumber"]=1
实在没搞懂,空表里怎么来的数据
posted on 2007-04-10 10:27  漫步人生  阅读(881)  评论(0编辑  收藏  举报