[英]How can I export a GridView.DataSource to a datatable or dataset?
如何将GridView.DataSource
导出到datatable或dataset?
假设您的DataSource是DataTable类型,您可以这样做:
myGridView.DataSource as DataTable
You should convert first DataSource
in BindingSource
, look example 你应该在
BindingSource
转换第一个DataSource
,看看例子
BindingSource bs = (BindingSource)dgrid.DataSource; // Se convierte el DataSource
DataTable tCxC = (DataTable) bs.DataSource;
With the data of tCxC
you can do anything. 使用
tCxC
的数据,您可以做任何事情。
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. 这将允许您测试null,因为这会导致DataTable对象或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.
如果DataSource实际上是DataSet甚至是某种集合,则使用(DataTable)Gridview1.DataSource将其作为DataTable进行转换会导致崩溃错误。
Supporting Documentation: MSDN Documentation on "as" 支持文档: 关于“as”的MSDN文档
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(" ","");
}
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(" ","");
}
dt.Rows.Add(dr);
}
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;
然后你可以使用
DataTable dt = Gridview1.DataSource as DataTable;
in function to retrieve datatable. 在函数中检索数据表。
But I bind the datatable to gridview when i click button, and recording to Microsoft document: 但是,当我单击按钮并将录制到Microsoft文档时,我将数据表绑定到gridview:
HTTP is a stateless protocol.
HTTP是无状态协议。 This means that a Web server treats each HTTP request for a page as an independent request.
这意味着Web服务器将页面的每个HTTP请求视为独立请求。 The server retains no knowledge of variable values that were used during previous requests.
服务器不保留先前请求期间使用的变量值的知识。
If you have same condition, then i will recommend you to use Session
to 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/
我使用下面的代码行,它可以工作,试试这个
DataTable dt = dataSource.Tables[0];
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.