foreach (DataRow dr in ds1.Tables[0].Rows)
            {
                //对应每行的主键值
                string value = dr[ds1.Tables[0].PrimaryKey[0]].ToString();
            }楼主试下这样可不可以

解决方案 »

  1.   

    直接运行的错误提示:
    System.IndexOutOfRangeException: 索引超出了数组界限。
    在 FineUIDemo.AllocIn.Grid1_RowCommand(Object sender, GridCommandEventArgs e) 位置 E:\testDP\EmptyProjectNet20\CK_AllocIn.aspx.cs:行号 149
      

  2.   

    这个问题有这么难吗,获取主键办不到,可不可以删除DS集的方式,来删除其对应数据库表项呢。我试过下面的,也是不行
    ds1.Tables[0].clear();
    myQA.Update();
      

  3.   

    如果知道主键是哪一个,可以把ds1.Tables[0].PrimaryKey[0]换成主键的对应列名
      

  4.   

    比如,主键为AllocInDetID
    那就可以写成
    string value = dr["AllocInDetID"].ToString();
      

  5.   


    就是呀,我就觉得这个问题没有那么复杂的。你这个我下班回家试试。
    疑问:
    dr["AllocInDetID"]. 这个值是一个整形值吗,
      

  6.   

    dr["AllocInDetID"]这是个object,要整形值的话,用Convert.ToInt32(dr["AllocInDetID"])
      

  7.   


    哥,试过了,还是不行呢。
                        DataSet ds1;
                        string strWhere = "CK_AllocInID = " + id;                    ds1 = m_bllCK_AllocInDet.GetList(strWhere);
                        int cnt = ds1.Tables[0].Rows.Count;
                        foreach (DataRow dr in ds1.Tables[0].Rows)
                        {
                            int keyid = Convert.ToInt32(dr["AllocInDetID"]);
                            m_bllCK_AllocInDet.Delete(keyid);
                        }报错如下:System.ArgumentException: 列“AllocInDetID”不属于表 ds。
    在 System.Data.DataRow.GetDataColumn(String columnName)
    在 System.Data.DataRow.get_Item(String columnName)
    在 FineUIDemo.AllocIn.Grid1_RowCommand(Object sender, GridCommandEventArgs e) 位置 E:\testDP\EmptyProjectNet20\CK_AllocIn.aspx.cs:行号 148
      

  8.   

    调试看下ds1.Tables[0]里有哪些列列“AllocInDetID”不属于表,那肯定是表里的列名楼主写错了
      

  9.   

    DataSet ds1;
                        string strWhere = "CK_AllocInID = " + id;
     
                        ds1 = m_bllCK_AllocInDet.GetList(strWhere);
                        int cnt = ds1.Tables[0].Rows.Count;
                        foreach (DataRow dr in ds1.Tables[0].Rows)
                        {
                            int keyid = Convert.ToInt32(dr["AllocInDetID"]);
                            m_bllCK_AllocInDet.Delete(keyid);
                        }可以看出来你Where条件里面是用CK_AllocInID过滤ID,因为你后面加的那个字段是ID,但是你取值的时候取的是AllocInDetID,肯定错了,你应该取CK_AllocInID才对
      

  10.   

    或者说,直接取你上面定义的  id ,就是where  条件后面跟的那个
      

  11.   

    楼上的,楼主是要用主表的主键值去从表里取数据,所以应该不是“CK_AllocInID”
    如果楼主2楼的截图上显示的就是从表的数据,那就改成Convert.ToInt32(dr["CK_AllocInDetID"]) 
      

  12.   

    感谢 lyq8376 兄弟你的答案是对的,呵,我拿错了字段名。。也感谢a01589的热情回复。。
      

  13.   

    不好意思,没看到lyq8376 兄弟 12楼的回复。应该是你最先指出来的。。