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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/785799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 01:04:24  来源:igfitidea点击:

How can I export a GridView.DataSource to a datatable or dataset?

c#asp.netgridview

提问by Penguen

How can I export GridView.DataSourceto datatable or dataset?

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

采纳答案by Marco Vinicio Gomez G.

You should convert first DataSourcein BindingSource, look example

你应该先转换DataSourceBindingSource,例如看

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

With the data of tCxCyou can do anything.

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

回答by Kon

Assuming your DataSource is of type DataTable, you can just do this:

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

myGridView.DataSource as DataTable

回答by Tacoman667

Personally I would go with:

我个人会选择:

DataTable tbl = Gridview1.DataSource as DataTable;

This would allow you to test for null as this results in either DataTable object or null. Casting it as a DataTable using (DataTable)Gridview1.DataSource would cause a crashing error in case the DataSource is actually a DataSet or even some kind of collection.

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

Supporting Documentation: MSDN Documentation on "as"

支持文档:关于“as”的 MSDN 文档

回答by Joseph

Ambu,

安布,

I was having the same issue as you, and this is the code I used to figure it out. Although, I don't use the footer row section for my purposes, I did include it in this code.

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

    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);
    }

回答by sav

This comes in late but was quite helpful. I am Just posting for future reference

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

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

回答by RSB

I have used below line of code and it works, Try this

我使用了下面的代码行并且它有效,试试这个

DataTable dt =  dataSource.Tables[0];

回答by 劉鎮瑲

If you do gridview.bind()at:

如果你gridview.bind()在:

if(!IsPostBack)

{

//your gridview bind code here...

}

Then you can use DataTable dt = Gridview1.DataSource as DataTable;in function to retrieve datatable.

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

But I bind the datatable to gridview when i click button, and recording to Microsoft document:

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

HTTP is a stateless protocol. This means that a Web server treats each HTTP request for a page as an independent request. The server retains no knowledge of variable values that were used during previous requests.

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

If you have same condition, then i will recommend you to use Sessionto persist the value.

如果您有相同的情况,那么我会建议您使用Session来保持该值。

Session["oldData"]=Gridview1.DataSource;

After that you can recall the value when the page postback again.

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

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

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

参考资料: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/

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