【问题标题】:How can I export a GridView.DataSource to a datatable or dataset?如何将 GridView.DataSource 导出到数据表或数据集?
【发布时间】:2010-10-21 14:39:43
【问题描述】:

如何将GridView.DataSource 导出到数据表或数据集?

【问题讨论】:

标签: c# asp.net gridview


【解决方案1】:

假设您的 DataSource 是 DataTable 类型,您可以这样做:

myGridView.DataSource as DataTable

【讨论】:

  • 这是这个问题的最佳答案,谢谢
  • 我设置了datagridview.DataSource = dataTable,但是当我想通过这种方式从DataSource 读取它时,通过消息抛出无效的强制转换异常:Unable to cast object of type 'System.Windows.Forms.BindingSource' to type 'System.Data.DataTable'.
【解决方案2】:

你应该先将DataSource转换成BindingSource,看例子

BindingSource bs = (BindingSource)dgrid.DataSource; // Se convierte el DataSource 
DataTable tCxC = (DataTable) bs.DataSource;

有了tCxC的数据,你可以做任何事。

【讨论】:

    【解决方案3】:

    我个人会选择:

    DataTable tbl = Gridview1.DataSource as DataTable;
    

    这将允许您测试 null,因为这会导致 DataTable 对象或 null。使用 (DataTable)Gridview1.DataSource 将其转换为 DataTable 会导致崩溃错误,以防 DataSource 实际上是 DataSet 甚至是某种集合。

    支持文档:MSDN Documentation on "as"

    【讨论】:

    • 虽然这没有产生任何错误,但当我将鼠标悬停以检查我的数据表的值时,即使我的 gridview 显示填充的数据,它也会将其显示为 null。 DataTable dt = gvJobSearchEngine.DataSource as DataTable;
    【解决方案4】:

    安布,

    我遇到了和你一样的问题,这是我用来解决问题的代码。虽然,我没有出于我的目的使用页脚行部分,但我确实将它包含在此代码中。

        DataTable dt = new DataTable();
    
        // add the columns to the datatable            
        if (GridView1.HeaderRow != null)
        {
    
            for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
            {
                dt.Columns.Add(GridView1.HeaderRow.Cells[i].Text);
            }
        }
    
        //  add each of the data rows to the table
        foreach (GridViewRow row in GridView1.Rows)
        {
            DataRow dr;
            dr = dt.NewRow();
    
            for (int i = 0; i < row.Cells.Count; i++)
            {
                dr[i] = row.Cells[i].Text.Replace("&nbsp;","");
            }
            dt.Rows.Add(dr);
        }
    
        //  add the footer row to the table
        if (GridView1.FooterRow != null)
        {
            DataRow dr;
            dr = dt.NewRow();
    
            for (int i = 0; i < GridView1.FooterRow.Cells.Count; i++)
            {
                dr[i] = GridView1.FooterRow.Cells[i].Text.Replace("&nbsp;","");
            }
            dt.Rows.Add(dr);
        }
    

    【讨论】:

      【解决方案5】:

      如果您在 gridview.bind() 处发送:

      if(!IsPostBack)
      
      {
      
      //your gridview bind code here...
      
      }
      

      然后你可以在函数中使用DataTable dt = Gridview1.DataSource as DataTable;来检索数据表。

      但是当我单击按钮时,我将数据表绑定到 gridview,并记录到 Microsoft 文档:

      HTTP 是一种无状态协议。这意味着 Web 服务器将每个 对页面的 HTTP 请求作为独立请求。服务器保留 不知道之前使用的变量值 请求。

      如果你有同样的条件,那么我会推荐你​​使用Session来保持值。

      Session["oldData"]=Gridview1.DataSource;
      

      之后您可以在页面再次回发时调用该值。

      DataTable dt=(DataTable)Session["oldData"];
      

      参考资料: https://msdn.microsoft.com/en-us/library/ms178581(v=vs.110).aspx#Anchor_0

      https://www.c-sharpcorner.com/UploadFile/225740/introduction-of-session-in-Asp-Net/

      【讨论】:

      • @John Waclawski 我遇到了和你一样的问题。我找到了这个解决方案,希望对你有帮助。
      【解决方案6】:

      我已经使用了下面的代码行,它可以工作,试试这个

      DataTable dt =  dataSource.Tables[0];
      

      【讨论】:

        【解决方案7】:

        这来晚了,但很有帮助。我只是发布以供将来参考

        DataTable dt = new DataTable();
        Data.DataView dv = default(Data.DataView);
        dv = (Data.DataView)ds.Select(DataSourceSelectArguments.Empty);
        dt = dv.ToTable();
        

        【讨论】:

        • Data.DataView 在我尝试您的代码时没有任何智能感知弹出窗口。
        猜你喜欢
        • 2020-11-20
        • 1970-01-01
        • 1970-01-01
        • 2010-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多