This is a migrated thread and some comments may be shown as answers.

Save the entire grid

2 Answers 95 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kjell
Top achievements
Rank 1
Iron
Kjell asked on 02 Dec 2013, 06:51 AM
How do i save / I export the entire grid to the sql server  database containing about 1000 item?
Like export data to excel.........

2 Answers, 1 is accepted

Sort by
0
Kostadin
Telerik team
answered on 04 Dec 2013, 01:40 PM
Hello Kjell,

Thank you for contacting us.

I would recommend you to examine the following live example which demonstrates how to export RadGrid to excel. Additional information could be found under Related Resources tab.

Regards,
Kostadin
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Princy
Top achievements
Rank 2
answered on 09 Dec 2013, 02:07 PM
Hi Kjell,

I guess you want to copy the Grid data to SQL. Below is a sample code snippet that i have tried. Please create the Table and its columns in the SQL before Export.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" DataSourceID="SqlDataSource1"   AllowPaging="true" AllowSorting="true">
    <MasterTableView DataKeyNames="OrderID" >
        <Columns>
            <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="ShipCity" HeaderText="ShipCity" UniqueName="ShipCity" />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
<asp:Button ID="Button1" runat="server" Text="ExportToSQL" OnClick="Button1_Click" />

C#:
protected void Button1_Click(object sender, EventArgs e)
{
    RadGrid1.AllowPaging = false;
    RadGrid1.Rebind();
    DataTable dt = new DataTable("Example");// Give the Table Name here
    int columncount = 0;
 
    foreach (GridColumn column in RadGrid1.MasterTableView.Columns)
    {
        if (!string.IsNullOrEmpty(column.UniqueName) && !string.IsNullOrEmpty(column.HeaderText))
        {
            columncount++;
            dt.Columns.Add(column.UniqueName, typeof(string));
        }
    }
    DataRow dr;
    foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
    {
        dr = dt.NewRow();
 
        for (int i = 0; i < columncount; i++)
        {
            dr[i] = item[RadGrid1.MasterTableView.Columns[i].UniqueName].Text;
        }
        dt.Rows.Add(dr);
    }
    //Create your SQL connection string
    using (SqlConnection dbConnection = new SqlConnection("Data Source=192.168.5.139;Initial Catalog=Northwind_new;Persist Security Info=True;User ID=sa;Password=softinc;"))
    {
        dbConnection.Open();
        using (SqlBulkCopy s = new SqlBulkCopy(dbConnection))
        {
            s.DestinationTableName = dt.TableName;
            foreach (var column in dt.Columns)
            s.ColumnMappings.Add(column.ToString(), column.ToString());
            s.WriteToServer(dt);
        }
    }
    RadGrid1.AllowPaging = true;
    RadGrid1.Rebind();
}

Thanks,
Princy
Tags
Grid
Asked by
Kjell
Top achievements
Rank 1
Iron
Answers by
Kostadin
Telerik team
Princy
Top achievements
Rank 2
Share this question
or