DataSet ds = new DataSet();
ds = (DataSet)dgList.DataSource;
但是调试时发现:dgList.DataSource 是未定义的值。dgList是一个分页的DataGrid我如何能取到被绑定的数据源的内容呢?谢谢~~

解决方案 »

  1.   

    你要是绑定了,datasource是有值的啊
      

  2.   

    其实有很多对象可以成为datasource,比如list,比如table,比如array
    首先确定你是不是绑定过了,然后看你绑定的对象是什么类型的,然后再去强转看里边的值
      

  3.   

    public void BindGrid()
    {
    //将将M_EBOOK_SERIAL表中的数据绑定在在dgList上DbHand dbHand = new DbHand(); DbHand dbHand = new DbHand(); DataSet ds = new DataSet();
    DataSet dsCurEbook  = new DataSet(); string strSql = ""; string strCurBook = ""; strSql = " select ebook_no, bkc, ebook_serial,start_date, bkc_descr, hs_code ";
    strSql = strSql + " from m_ebook_serial ";
    strSql = strSql + " order by ebook_no, ebook_serial asc"; try
    {
    dbHand.OpenConnection(); ds = dbHand.ReadData( strSql, "ebook_list"); dgList.DataSource = ds.Tables["ebook_list"].DefaultView;
    this.dgList.DataBind();

    lblCurrentIndex.Text = "第 " + (dgList.CurrentPageIndex + 1).ToString() + " 页";
    lblPageCount.Text = "总共 " + dgList.PageCount.ToString() + " 页";
    }
    catch( System.Exception  ex )
    {
    lblMsg.Text = ex.Message.ToString().Trim();
    }
    finally
    {
    dbHand.CloseConnection();
    } }
      

  4.   

    然后,在点击某一行的更新按钮时,运行如下代码:
    //点击更新按钮,将这一行的数据读到文本框中,并从dgList中删除这一行数据 string strBookNo = e.Item.Cells[0].Text.ToString().Trim();
    string strBKC = e.Item.Cells[1].Text.ToString().Trim();
    txtBookNo.Text = e.Item.Cells[0].Text.ToString().Trim().Replace(" ","");
    txtEbookSerial.Text = e.Item.Cells[2].Text.ToString().Trim().Replace(" ","");
    txtBeginDate.Text =  e.Item.Cells[3].Text.ToString().Trim().Replace(" ","");
    txtBKC.Text = e.Item.Cells[1].Text.ToString().Trim().Replace(" ","");
    txtHSCODE.Text = e.Item.Cells[5].Text.ToString().Trim().Replace(" ","");
    txtBKCDescr.Text = e.Item.Cells[4].Text.ToString().Trim().Replace(" ",""); if ( ((Label)(e.Item.Cells[7].FindControl("lblUsed"))).Text.ToString().Trim().Replace(" ","") == "1" )
    {
    txtBKC.Enabled = false;
    txtBookNo.Enabled = false;
    }
    else
    {
    txtBKC.Enabled = true;
    txtBookNo.Enabled = true;
    }
    lblCurrentIndex.Text = "第 " + (dgList.CurrentPageIndex + 1).ToString() + " 页";
    lblPageCount.Text = "总共 " + dgList.PageCount.ToString() + " 页";
    int index = dgList.CurrentPageIndex * dgList.PageSize + e.Item.ItemIndex; //获取当前行号

    ds = (DataSet)dgList.DataSource;
                
    ds.Tables[0].Rows.Remove(ds.Tables[0].Rows[index]); //这样来移除数据
                                    
    lblCurrentIndex.Text = "第 " + (dgList.CurrentPageIndex + 1).ToString() + " 页";
    lblPageCount.Text = "总共 " + dgList.PageCount.ToString() + " 页";

    Focus.SetFocus(this,"txtBookNo");
    }
      

  5.   

     dgList.DataSource = ds.Tables["ebook_list"].DefaultView;
    ====================================================
    因为你DataSource是DefaultView;所以你取Datasource不能转成DataSet把下面这几句改成
    //ds = (DataSet)dgList.DataSource;
    //ds.Tables[0].Rows.Remove(ds.Tables[0].Rows[index]); //这样来移除数据
    DataView dv=dgList.DataSource as DataView;
    if(dv!=null)
    {
       DataTable dt=dv.Table;
       dt.Rows.Remove(dt.Rows[index]);
    }
      

  6.   

    lblCurrentIndex.Text = "第 " + (dgList.CurrentPageIndex + 1).ToString() + " 页";
    lblPageCount.Text = "总共 " + dgList.PageCount.ToString() + " 页";
    int index = dgList.CurrentPageIndex * dgList.PageSize + e.Item.ItemIndex; //获取当前行号

    DataView dv=dgList.DataSource as DataView;
    if(dv!=null)
    {
    DataTable dt=dv.Table;
    dt.Rows.Remove(dt.Rows[index]);//这样来移除数据
    }
     奇怪:
    调试时发现:(dgList.CurrentPageIndex + 1).ToString() 结果是“1”
                 dgList.PageCount.ToString() 结果是127,
     
    但是: dgList.DataSource <未定义的值> System.Object为什么啊?
      

  7.   

    dgList.DataSource = ds;
      

  8.   

    你应该先将DataSource存为Session
    DataSet ds = new DataSet();
    Session["ds"]=dgList.DataSource;
    ds =Session["ds"];