| |

VerySource

 Forgot password?
 Register
Search
View: 1176|Reply: 8

Does anyone know the problem, how to convert the data source in GridView to DataTable

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-7 12:00:01
| Show all posts |Read mode
How to convert the data source in the GridView to a DataTable.

 There are two ways to get the data source of GridView, one is through the DataSource,
One is obtained through DataSourceID, such as
 Way 1
    this.GridView1.DataSource = this.ObjectDataSource1;
    this.GridView1.DataBind ();
 Way 2
   this.GridView1.DataSourceID = "ObjectDataSource1";
   this.GridView1.DataBind ();

Note: There may be many types of data sources in the GridView, which may be DataSet, DataTable,
DataView, DataReader, SqlDataSource, ObjectDataSource.

If the data source is a DataTable or something, that's easy to use.
this.GridView1.DataSource as DataTable is ok.,
But if it is a data source control such as ObjectDataSource, this will not work. Does anyone know how to complete the above requirements?
Reply

Use magic Report

0

Threads

64

Posts

45.00

Credits

Newbie

Rank: 1

Credits
45.00

 China

Post time: 2020-5-25 11:15:01
| Show all posts
The most stupid way is to traverse the GV, take out the data of each row and column, and put it into a DataTable
Reply

Use magic Report

0

Threads

8

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-5-25 15:00:01
| Show all posts
I don't know what it means. I feel that it is enough to use the previous one. I haven't met it in the back.
Reply

Use magic Report

1

Threads

31

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-5-26 20:15:01
| Show all posts
As the landlord said, when the data source of GridView is indeed impossible to convert to DataTable,
The possible approach is probably as follows:

object data = GridView.DataSource;
DataTable dataTable = null;
if (data is DataSet)
{
DataSet ds = data as DataSet;
if (ds! = null&&ds.Tables.Count == 1)
{
dataTable = ds.Tables [0];
}
}
else if (data is DataView)
{
DataView dv = data as DataView;
dataTable = dv.Table;
}
else if (data is DataTable)
{
dataTable = data as DataTable;
}
I don't know how to convert other types to DataTable type.
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-5-27 13:00:02
| Show all posts
The previous friend said that traversing the GridView, of course not, I need to take out the data source in the GridView, and you traverse
GridView only takes the data source of the current page. .

The idea of ​​the common people is my idea, but if the data source is a control such as ObjectDataSource, it will not work, does any master know how to solve this problem? ? ?
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-5-27 23:30:01
| Show all posts
This is also a stupid way,

Recheck the database

   DataTable dt = ((DataView) ((ObjectDataSource) this.GridView1.DataSource) .Select ()). Table;
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-5-29 01:15:01
| Show all posts
Haha, dude, you can get results by repeating a database query. I have also thought about it, but the efficiency is not very good. Connecting to the database and consuming a lot of resources. Do you have any good ways? Continue to discuss
Reply

Use magic Report

0

Threads

22

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-5-30 18:00:02
| Show all posts
See if there is a copy method for these types. Like what. Copy or something
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-28 06:45:02
| Show all posts
I also encountered this problem. GV is bound to ObjectDataSource but the following business needs to be converted to DataTable to solve..
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list