我如何在C#中将DataTabe绑定到GridView [英] How can i bind DataTabe to GridView in C#

查看:61
本文介绍了我如何在C#中将DataTabe绑定到GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview,我想将gridview的所有值都保存到数据表中.
请帮我解决上述问题

谢谢

I m having a gridview and i want to get all the values of the gridview to the datatable
pls help me for this above question

Thanks

推荐答案

它可能会有所不同,具体取决于GridView的数据源.
试试:
It may vary depending upon the DataSource of your GridView.
Try:
//If DataSource of GridView1 is a DataTable
DataTable dt = (DataTable)GridView1.DataSource;

//If DataSource of GridView1 is a DataView
DataView dv = (DataView)GridView1.DataSource; 



如果要在移植数据时进行一些更改,则必须遍历.但是,您不能将GridViewRow转换为DataRow.您必须先手动创建列,然后遍历所有行.在此循环中,您将需要遍历该行的所有列,并将其复制到正在创建的DataRow中.



If you want to make some changes while porting the data, you have to loop through. But, you cannot convert GridViewRow to DataRow. You have to manually create columns first, then loop through all the rows. Within this loop, you would need to loop through all the columns of the row and copy it to the DataRow that you are creating.

//This is just a Psuedocode
DataTable dt = new DataTable();
//Add columns here
dt.Columns.Add(); //or something similar
//go on till all the columns are added
//loop and second loop
foreach(GridViewRow gvr in GridView1.Rows)
{
   DataRow dr = dt.NewRow();
   
   //second loop - looping all columns
   for(int i = 0; i < gvr.Rows.Count; ++i)
   {
      dr[i] = gvr.Cells[i].Text;
   }
} 


尝试以下代码:我相信它将对您有所帮助

Sqlconnection con =新的Sqlconnection("con string");
字符串查询=从<表名>中选择*,其中<条件>";

SqlDataAdapter da =新的SqlDataAdater(Query,Con);
DataSet ds = new DataSet();
da.fill(ds,"dx-army");

GridView1.dataSource = ds.Tables ["dx-army"];
GridView1.dataBind();


如果您得到答复,那么请不要忘记投票给我....


谢谢

DX-ARMY
Try this code:I am sure it will help you

Sqlconnection con=new Sqlconnection("con string");
String Query="Select * from <Table name> where <Condition>";

SqlDataAdapter da=new SqlDataAdater(Query,Con);
DataSet ds=new DataSet();
da.fill(ds,"dx-army");

GridView1.dataSource=ds.Tables["dx-army"];
GridView1.dataBind();


If you got answer then please do not forget to vote me....


thanks

DX-ARMY


这篇关于我如何在C#中将DataTabe绑定到GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆