How to assign datasource dynamically to Radgrid

1 Answer 207 Views
Grid
Nirmal
Top achievements
Rank 1
Nirmal asked on 01 Jun 2022, 05:59 AM | edited on 01 Jun 2022, 06:07 AM

Hi,

 

My Scenario is I have to export a RadGrid,

I have created a grid name grdExport in aspx file and binding datasource in aspx.vb file.

My problem is i have to assign datasource dynamically,

For example, I have 5 different datasources ie A,B,C,D,E

First i assign "A" datasource to grdExport i will prepare a workbook for that,

then i assign "B" datasource to grdExport i will prepare a workbook for that.

I will repeat the same above process for all the datasources,

my issue is data exporting but the datasource "A" is repeating for rest of the datasources,

I dont know what i have missed and where i missed,

Here is my code,

NOTE: GetProcessDataForExport() is inside the for loop, i have to assign different datasources based on parameter(ModuleId) i got.

 

Public Function GetProcessDataForExport(ByVal ModuleId As Integer) As xlsx.Workbook
            Try
                Dim Shift As New ShiftSettings(TabId)
                Dim ds As DataSet
                ds = SqlDataProvider.phdag_GetShiftTags(ModuleId, KPIModuleId)

                grdExportData.MasterTableView.GetColumn("Description").Visible = CType(SgsSettings("show_Description"), Boolean)
                grdExportData.MasterTableView.GetColumn("Tag").Visible = CType(SgsSettings("show_Tag"), Boolean)
                grdExportData.MasterTableView.GetColumn("UOM").Visible = CType(SgsSettings("show_UOM"), Boolean)
                grdExportData.MasterTableView.GetColumn("Limits").Visible = CType(SgsSettings("show_Range"), Boolean)
                grdExportData.MasterTableView.GetColumn("Target").Visible = CType(SgsSettings("show_Plan"), Boolean)
                grdExportData.MasterTableView.GetColumn("Current").Visible = CType(SgsSettings("show_CurrV"), Boolean)
                grdExportData.MasterTableView.GetColumn("ShiftAvg").Visible = CType(SgsSettings("show_ShiftAvgV"), Boolean)
                grdExportData.MasterTableView.GetColumn("Shift24HrAvg").Visible = CType(SgsSettings("show_Shift24HrV"), Boolean)
                grdExportData.MasterTableView.GetColumn("ShiftMinVal").Visible = CType(SgsSettings("show_ShiftMinV"), Boolean)
                grdExportData.MasterTableView.GetColumn("ShiftMaxVal").Visible = CType(SgsSettings("show_ShiftMaxV"), Boolean)
                grdExportData.MasterTableView.GetColumn("TimeStamp").Visible = CType(SgsSettings("show_TimeStamp"), Boolean)
                grdExportData.MasterTableView.GetColumn("CorrectiveAction").Visible = CType(SgsSettings("show_CorrAct"), Boolean)

                'Add Row Values
                Dim dt As DataTable = GetTagsByTagType(ds.Tables(1), TagType, DeviationType)

                grdExportData.ExportSettings.IgnorePaging = True
                grdExportData.ExportSettings.FileName = ModuleId
                grdExportData.ExportSettings.Excel.Format = GridExcelExportFormat.Xlsx

                grdExportData.DataSource = New String() {}
                grdExportData.DataSource = dt
                grdExportData.Rebind()

                Return TryCast(grdExportData.MasterTableView.GenerateXlsxOutput(Of xlsx.Workbook)(), xlsx.Workbook)

            Catch ex As Exception
            End Try
        End Function

Thanks.

 

 

1 Answer, 1 is accepted

Sort by
0
Mukesh
Top achievements
Rank 1
Iron
answered on 02 Jun 2022, 12:50 PM

Hello Nirmal,

If you want to export to excel for each data sources (A,B,C,D,E) then create drop down list with items (A,B,C,D,E).

Based on drop down selected items, assign data source to Radgrid and rebind grid on drop down change event.

Thanks,

Mukesh Prajapati

Nirmal
Top achievements
Rank 1
commented on 02 Jun 2022, 01:05 PM | edited

Thanks for Reply,

But i have to export All the data sources i cant create dropdown for that and also data is not assigning to grid.

I have to assign datasource to the grid and get an workbook from that this will be inside the loop,

I assign datasource also correctly but only thing its not rebinding.

In Export workbook i got "A" datasource content repeatedly in all worksheets for instead of B,C,D,E

And also datasource is dynamic we cant tell that there will be only 5.

And one more thing, i should export all these at once inside workbook with many worksheets(Each datasource as worksheet).

 

Thanks,

Nirmal

 

Mukesh
Top achievements
Rank 1
Iron
commented on 02 Jun 2022, 01:11 PM

Can you please provide below details?

Which type of output you want (Do you want to export all data source in single excel or want to export separate excel based on each data source)?

 

Mukesh
Top achievements
Rank 1
Iron
commented on 02 Jun 2022, 01:14 PM

Follow below steps to export separate excel sheet for each data source.

First assign A data source to rad grid then export and save excel.

After that assign B data source and rebind the grid (rdgrid.Rebind()) and the export and save excel.

follow same procedure to next data sources.

Nirmal
Top achievements
Rank 1
commented on 02 Jun 2022, 01:15 PM

I want .xlsx file,

Yes i want all the datasources in single excel with multiple sheets.

A datasource in Sheet 1

B datasource in Sheet 2

C datasource in Sheet 3

Like this i need.

 

Thanks,

Nirmal

Nirmal
Top achievements
Rank 1
commented on 02 Jun 2022, 01:22 PM

Yes i even tried the same way you have said.

Yes i have used rebind too.

Also tried using NeedDataSource event

Datasource is assigned to grid but not binded even after using rebind() method

And excel results the first assigned data source i.e "A" datasource.

 

Thanks,

Nirmal

Tags
Grid
Asked by
Nirmal
Top achievements
Rank 1
Answers by
Mukesh
Top achievements
Rank 1
Iron
Share this question
or