Click here to Skip to main content
14,930,671 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to bind the data of gridview to Datatable ,
source of grid view is from MS SQL Server 2008

Here is the aspx page

<asp:GridView ID="GridView1" runat="server"
                        <asp:CheckBox ID="chkb" runat="server" />


SqlDataAdapter da = new SqlDataAdapter("Select * from test", con);
DataSet ds = new DataSet();
da.Fill(ds, "test");
GridView1.DataSource = ds;

and i want to pass the data again to sql server to another table plz help me out
Updated 27-Mar-14 1:41am
Siva Hyderabad 27-Mar-14 8:42am
I think , You need back up the table ?yes?

DataTable tbl = GridView1.DataSource as DataTable;

use this code..
You should use EF to get your data from database, then you should implement a method like the next one to dynamic create a data table by doing also some processing on the given data like in the next example:
public static DataTable ListToDataTable(List<User> entityList)
            DataTable usersDataTable = new DataTable();
            usersDataTable.Columns.Add("ID", typeof(int));
            usersDataTable.Columns.Add("Person", typeof(string));
            usersDataTable.Columns.Add("Center", typeof(string));
            usersDataTable.Columns.Add("Service", typeof(string));
            usersDataTable.Columns.Add("UserName", typeof(string));
            usersDataTable.Columns.Add("Email", typeof(string));
            foreach (User user in entityList)
                    String.Format("{0} {1}", user.FirstName, user.LastName),
                    (user.Center == null ? "DGASPC" : user.Center.Name),
                    (user.Service == null ? string.Empty : user.Service.Name),
                    (user.Email == null ? string.Empty : user.Email));
            return usersDataTable;
<asp:gridview id="GridView1" runat="server" xmlns:asp="#unknown">
<columns> <asp:templatefield>
<asp:checkbox id="chkb" runat="server" text="<%Eval(" your="" row="" name")&gt;"="">

' so it will resolve%>]]>

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900