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

Clearing RadGrid & Setting DataSource to dataTable

1 Answer 794 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jonathan
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?

 

aspx.vb(UploadButton)

 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)
        FileUpload1.SaveAs(csvPath)

        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
                dt.Rows.Add()
                Dim i As Integer = 0
                For Each cell As String In row.Split(","c)
                    dt.Rows(dt.Rows.Count - 1)(i) = cell
                    i += 1
                Next
                'ReceiptGrid.DataSource = Nothing
                ''ReceiptGrid.Rebind(dt.DataSet);

               

            End If

        Next

        Me.ReceiptGrid.DataSource = Nothing
        Me.ReceiptGrid.DataBind()
        '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  
    End Sub

 

aspx(RadGrid)

 

   <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">
        </GroupingSettings>
        <ExportSettings ExportOnlyData="True">
            <Excel Format="Xlsx" />
            <Csv EncloseDataWithQuotes="False" ColumnDelimiter="Tab" RowDelimiter="Comma" />
        </ExportSettings>
        <ClientSettings>
            <Selecting AllowRowSelect="True" />
        </ClientSettings>
        <MasterTableView AutoGenerateColumns="False" DataSourceID="Attributes" CommandItemDisplay="Top" PageSize="15">
           
          
            <RowIndicatorColumn Visible="False">
                <HeaderStyle Width="41px" />
            </RowIndicatorColumn>
            <ExpandCollapseColumn Created="True">
                <HeaderStyle Width="41px" />
            </ExpandCollapseColumn>
           
          
            <CommandItemSettings ShowExportToExcelButton="True" ShowExportToCsvButton="True" />
           
          
            <Columns>
                <telerik:GridBoundColumn DataField="CategoryID" FilterControlAltText="Filter CategoryID column" HeaderText="Category ID" SortExpression="CategoryID" UniqueName="CategoryID">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="CategoryLabel" FilterControlAltText="Filter CategoryLabel column" HeaderText="Category Label" SortExpression="CategoryLabel" UniqueName="CategoryLabel">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="AttributeLabel" FilterControlAltText="Filter AttributeLabel column" HeaderText="Attribute Label" SortExpression="AttributeLabel" UniqueName="AttributeLabel">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="AttributeTypeLabel" FilterControlAltText="Filter AttributeTypeLabel column" HeaderText="Attribute Type Label" SortExpression="AttributeTypeLabel" UniqueName="AttributeTypeLabel">
                </telerik:GridBoundColumn>
                <telerik:GridEditCommandColumn>
                </telerik:GridEditCommandColumn>
                <telerik:GridClientDeleteColumn FilterControlAltText="Filter column column" UniqueName="column">
                </telerik:GridClientDeleteColumn>
            </Columns>

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

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

<HeaderContextMenu RenderMode="Native"></HeaderContextMenu>
    </telerik:RadGrid>

 

 

 

 

1 Answer, 1 is accepted

Sort by
0
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;
ReceiptGrid.DataBind();



Regards,
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.
Tags
Grid
Asked by
Jonathan
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or