你的数据源不要用MyDataGrid数据源用datagrid的数据源就可以了

解决方案 »

  1.   

    Dim numbercols As Integer = MyDataGrid.Items.Item(0).Cells.Count
    取的只是当前页的items数。用DataSet.yourTable.Rows.Count
      

  2.   

    public void ToExcel(System.Web.UI.Control ctl)  
    {
    HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.txt");
    HttpContext.Current.Response.Charset = "gb2312";
    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
    HttpContext.Current.Response.ContentType = "application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
    // HttpContext.Current.Response.ContentType ="application/Microsoft Excel";
    // ctl.Page.EnableViewState = false;
    System.IO.StringWriter tw = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
    ctl.RenderControl(hw);
    HttpContext.Current.Response.Write(tw.ToString());
    HttpContext.Current.Response.End();
    ctl.Visible = false;
    }