Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hello everyone..!!
I have a gridview which is databinded by datasource.
i want to convert that datasource's content into datatable.

i tried this
 
GridView grd = new GridView();
        grd.DataSource = SqlDataSource1;
        grd.DataBind();
 
        DataTable dt = new DataTable();
       dt =(DataTable)grd.DataSource;

 
but its not working..!!
 
any suggestion/links are welcomed..!!
Posted 20-Nov-12 0:59am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

if all this is not working then use the most Cost consuming method:
 
1) create a datatable and add columns as in gridview
DataTable dt = new DataTable();
dt.Columns.Add("col1");.....
2) apply for loop on gridview rows and add repective datarow in datatable
for(int i=0;i<gridview.rowc.count;i++)>
{
DataRow dr =dt.NewRow();
dr["col1"]= gridview.rows[i]["col1"].text;
dt.rows.add(dr);
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Why you are casting like that directly give sqldatasource1 to dt (here you need to convert datasource to datatable).
  Permalink  
Comments
vaibhav mahajan at 20-Nov-12 7:12am
   
its not happening..!!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

BindingSource bs = (BindingSource )grid.DataSource;

DataTable dt = (DataTable ) bs.DataSource;
 
Sorry, BindingSource is only available on Windows Forms.
 
You can try:
 
bool firstTime = true;
    System.Data.DataTable dt;
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
            if (this.firstTime)
            {
                System.Data.DataView dv =
                     (e.Row.DataItem as System.Data.DataRowView).DataView;
                this.dt = dv.ToTable();
                this.Label1.Text = dt.Rows.Count.ToString();
                this.firstTime = false;
            }
    }
 
Reference
  Permalink  
v2
Comments
vaibhav mahajan at 20-Nov-12 7:09am
   
namespace?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

i got it ......
    DataTable dt = new DataTable();
       
 
        DataSourceSelectArguments args = new DataSourceSelectArguments();
 
        DataView dv = new DataView();
        dv = (DataView)SqlDataSource1.Select(args);
 
        dt = dv.ToTable();
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 354
1 Richard MacCutchan 320
2 OriginalGriff 255
3 Vinay Mistry 168
4 Jared Sanow 145
0 Sergey Alexandrovich Kryukov 5,939
1 OriginalGriff 4,885
2 CPallini 2,473
3 Abhinav S 1,507
4 Richard MacCutchan 1,437


Advertise | Privacy | Mobile
Web04 | 2.8.140814.1 | Last Updated 21 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100