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

Clearing RadGrid & Setting DataSource to dataTable

1 Answer 197 Views
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 1
Jonathan asked on 17 Oct 2016, 04:40 PM

Hello, I have a radGrid on my page that reads a SQL statement and returns the data in the grid upon first load. I have a button where users could import a tab delimited CSV file and display it to the grid. I believe that the process would go as: 1) Import CSV & store in dataTable 2)Clear radGrid original Data 3)set datasource of Radgrid to the newly made Datatable. Is this a efficient way of going about this? If so How would I clear the radGrid & set the radgrid to a new datasource?



 Protected Sub Upload(sender As Object, e As EventArgs)
        'Upload and save the file
        Dim csvPath As String = Server.MapPath("~/File") + Path.GetFileName(FileUpload1.PostedFile.FileName)

        Dim dt As New DataTable()
        dt.Columns.AddRange(New DataColumn(3) {New DataColumn("Category ID", GetType(String)), New DataColumn("Category Label", GetType(String)), New DataColumn("Attribute Label", GetType(String)), New DataColumn("Attribute Type Label", GetType(String))})

        Dim csvData As String = File.ReadAllText(csvPath)
        For Each row As String In csvData.Split(Environment.NewLine) 'How to split csv?'
            If Not String.IsNullOrEmpty(row) Then
                Dim i As Integer = 0
                For Each cell As String In row.Split(","c)
                    dt.Rows(dt.Rows.Count - 1)(i) = cell
                    i += 1
                'ReceiptGrid.DataSource = Nothing


            End If


        Me.ReceiptGrid.DataSource = Nothing
        'Dim consString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        'Using con As New SqlConnection(consString)
        '    Using sqlBulkCopy As New SqlBulkCopy(con)
        '        'Set the database table name
        '        sqlBulkCopy.DestinationTableName = "" 'What database to write it to?
        '        con.Open()
        '        sqlBulkCopy.WriteToServer(dt)
        '        con.Close()
        '    End Using
    End Sub




   <telerik:RadGrid ID="ReceiptGrid" runat="server" AllowFilteringByColumn="True" AllowMultiRowEdit="True" AllowPaging="True" DataSourceID="Attributes" Height="787px" Skin="Glow" Width="1000px" AllowAutomaticDeletes="True" AllowAutomaticInserts="True" AllowAutomaticUpdates="True" style="margin-left: 0px; margin-top: 4px;" HorizontalAlign="Justify" CellPadding="0" PageSize="18" RenderMode="Native">
        <GroupingSettings CollapseAllTooltip="Collapse all groups">
        <ExportSettings ExportOnlyData="True">
            <Excel Format="Xlsx" />
            <Csv EncloseDataWithQuotes="False" ColumnDelimiter="Tab" RowDelimiter="Comma" />
            <Selecting AllowRowSelect="True" />
        <MasterTableView AutoGenerateColumns="False" DataSourceID="Attributes" CommandItemDisplay="Top" PageSize="15">
            <RowIndicatorColumn Visible="False">
                <HeaderStyle Width="41px" />
            <ExpandCollapseColumn Created="True">
                <HeaderStyle Width="41px" />
            <CommandItemSettings ShowExportToExcelButton="True" ShowExportToCsvButton="True" />
                <telerik:GridBoundColumn DataField="CategoryID" FilterControlAltText="Filter CategoryID column" HeaderText="Category ID" SortExpression="CategoryID" UniqueName="CategoryID">
                <telerik:GridBoundColumn DataField="CategoryLabel" FilterControlAltText="Filter CategoryLabel column" HeaderText="Category Label" SortExpression="CategoryLabel" UniqueName="CategoryLabel">
                <telerik:GridBoundColumn DataField="AttributeLabel" FilterControlAltText="Filter AttributeLabel column" HeaderText="Attribute Label" SortExpression="AttributeLabel" UniqueName="AttributeLabel">
                <telerik:GridBoundColumn DataField="AttributeTypeLabel" FilterControlAltText="Filter AttributeTypeLabel column" HeaderText="Attribute Type Label" SortExpression="AttributeTypeLabel" UniqueName="AttributeTypeLabel">
                <telerik:GridClientDeleteColumn FilterControlAltText="Filter column column" UniqueName="column">

<EditColumn UniqueName="EditCommandColumn1" FilterControlAltText="Filter EditCommandColumn1 column"></EditColumn>

<FilterMenu RenderMode="Native"></FilterMenu>

<HeaderContextMenu RenderMode="Native"></HeaderContextMenu>





1 Answer, 1 is accepted

Sort by
Konstantin Dikov
Telerik team
answered on 20 Oct 2016, 10:11 AM
Hello Jonathan,

I would suggest that you use the Advanced Data-Binding and set the DataSource manually:
The above will allow you to use a global variable for example and set the DataTable from the CSV file conditionally. The rebind could be initiated by calling the Rebind method of the grid.

As for your current approach, you can try the following:
ReceiptGrid.DataSourceID = null;
ReceiptGrid.DataSource = dt;

Konstantin Dikov
Telerik by Progress
Check out the new UI for ASP.NET Core, the most complete UI suite for ASP.NET Core development on the market, with 60+ tried-and-tested widgets, based on Kendo UI.
Asked by
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question