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


XML
<asp:GridView ID="GridView1" runat="server"
            >
            <Columns>
             <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkb" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                </Columns>
        </asp:GridView>



Cs

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


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

C#
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:
C#
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)
            {
                usersDataTable.Rows.Add(user.ID,
                    String.Format("{0} {1}", user.FirstName, user.LastName),
                    (user.Center == null ? "DGASPC" : user.Center.Name),
                    (user.Service == null ? string.Empty : user.Service.Name),
                    user.UserName,
                    (user.Email == null ? string.Empty : user.Email));
            }
            //
            return usersDataTable;
        }
   
<asp:gridview id="GridView1" runat="server" xmlns:asp="#unknown">
>
<columns> <asp:templatefield>
<itemtemplate>
<asp:checkbox id="chkb" runat="server" text="<%Eval(" your="" row="" name")&gt;"="">







use
' 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