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

RadGrid DetailTable Not Updating on Datasource Change

13 Answers 400 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Zachary
Top achievements
Rank 1
Zachary asked on 14 Aug 2012, 12:37 PM
Hello, I am having trouble with a detail table in my hierarchical radgrid not updating to show the appropriate columns. I do have an AjaxManager on the page, but I am changing the datasource through a button click event not monitored by the AjaxManager. The colums are autogenerated, and on the columncreated method, HTMLEncode is set to true for GridBoundColumns. I looked into rebinding, needdatasource, and altering the viewstate with no luck.

The datasource I am using can be switched from a detailed to a summary view, so that columns that used to be in the radgrid should be removed on databind. Currently, the columns remain and are empty. I would guess that they are assigned null values.

ASPX
<telerik:RadGrid ID="rdgGridView" runat="server" AllowSorting="true" DataSourceID="datasource1" AutoGenerateColumns="false" AllowPaging="true" EnableViewState="true">
<MasterTableView DataSourceID="datasource1" DataKeyNames="ID" AutoGenerateColumns="false">
<DetailTables>
<telerik:GridTableView Name="Detail" runat="server" AutoGenerateColumns="true" AllowSorting="true">
<columns>
<telerik:GridTemplateColumn>
<HeaderTemplate>Link</HeaderTemplate>
<ItemTemplate><asp:LinkButton ID="LB" runat="server" Text="text" /></ItemTemplate>
</telerik:GridTemplateColumn>
</columns>
</DetailTables>
</MasterTableView>
</telerik:RadGrid>

VB
Private Sub Submit_Click(ByVal sender As System.object, ByVal e As System.EventArgs) Handles Submit.Click
 
rdgGridView.MasterTableView.DetailTables(0).DataSource = Nothing
 
rdgGridView.DataBind()
 
End Sub
 
  
 
Private Sub rdgGridView_DetailTableDataBind(ByVal sender As System.object, ByVal e As GridDetailTableDataBindEventArgs) Handles rdgGridView. GridDetailTableDataBindEventArgs
 
'Modify datasource
 
rdgGridView.MasterTableView.DetailTables(0).DataSource = DataSource
 
End Sub

So any thoughts would be appreciated.

13 Answers, 1 is accepted

Sort by
0
Zachary
Top achievements
Rank 1
answered on 14 Aug 2012, 08:06 PM
This was a time sensitive issue, so I hacked it and manually show/hide the autogenerated columns on the databound event.
0
Andrey
Telerik team
answered on 17 Aug 2012, 08:45 AM
Hi,

You could check this help topic for more information on how to change the structure of RadGrid on postback.

Kind regards,
Andrey
the Telerik team
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 their blog feed now.
0
Slava
Top achievements
Rank 1
answered on 02 Mar 2013, 12:21 AM
I have a somewhat similar problem (see below) but unless I'm missing something your example is not solving it.

I have a grid with autogeneratedcolumns = true and I have a details table inside of that grid with autogeneratedcolumns = true, have a dropdown box where user selects a particular month, every time the month changes the field names and values of the parent and detail grid change. So if for example selected month is Feb, 2013, set of generated fields would be "sales_12_2012", "sales_1_2013", sales_2_2013" and when I expend detail table it would have similar fields with an addition of some detail information like employee name for example ("empname", "sales_details_1_2013", "sales_details_2_2013", sales_details_3_2013"), and when the selected month would change to Mar, 2013 the set of generated fields would become "sales_1_2013", "sales_2_2013", sales_3_2013"...
As user gets to the page originally everything works fine, user expends the detail table and proper columns are shown (by default current month is selected so it's March, 2013 and therefore it shows: "empname", "sales_details_1_2013", "sales_details_2_2013", sales_details_3_2013") but if user then changes the month to Feb, 2013 for example, so query changes and has the following fields now: "empname", "sales_details_12_2012", "sales_details_1_2013", sales_details_2_2013", but when user expends the detail table again, the set of columns is still the same ("empname", "sales_details_1_2013", "sales_details_1_2013", sales_details_3_2013") as the original one in the details grid. Main "parent" grid works fine.
Please help, it's urgent. Thanks.

ASPX code:
<telerik:RadComboBox ID="rcbMonth" AutoPostBack="True"
            CausesValidation="False"
            Rows="1" runat="server">
        </telerik:RadComboBox>
<telerik:RadGrid ShowGroupPanel="false" AutoGenerateColumns="True" ID="RadGrid1"
            AllowFilteringByColumn="False" AllowSorting="false"
            ShowFooter="True" runat="server" GridLines="None" AllowPaging="false" EnableLinqExpressions="false"
            FooterStyle-BackColor="LightGray"
            >
            <ExportSettings HideStructureColumns="False" ExportOnlyData="true" IgnorePaging="true" OpenInNewWindow="true">
                <Excel Format="Html" />
            </ExportSettings>
            <MasterTableView Name="ParentGrid" DataKeyNames="Name" ShowGroupFooter="true" AllowMultiColumnSorting="true" CommandItemDisplay="Top"
                    CommandItemSettings-ShowAddNewRecordButton="False"
                    CommandItemSettings-ShowRefreshButton="False" ShowFooter="True">
                <CommandItemSettings ShowExportToWordButton="False" ShowExportToExcelButton="true"
                ShowExportToCsvButton="False" />
                <DetailTables>
                    <telerik:GridTableView DataKeyNames="NameDetail" runat="server" AutoGenerateColumns="True"
                        Name="Details" ShowFooter="False"
                        AllowPaging="False" HorizontalAlign="center"
                        GridLines="None">
                        <Columns>
                        </Columns>
                    </telerik:GridTableView>
                </DetailTables>
                <Columns>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>

VB Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If Not Page.IsPostBack Then
            Dim reportdefaultdate As Date = DateAdd("m", -1, DateTime.Now.Date)
            Dim reportdefaultmonth As Date = CDate(CStr(Month(reportdefaultdate)) & "/1/" & CStr(Year(reportdefaultdate)))
            Dim loopdate As Date = reportdefaultmonth.Date
            Do While loopdate >= CDate("7/1/2012")
                Dim item As New RadComboBoxItem()
                item.Text = MonthName(Month(loopdate)) & ", " & Year(loopdate)
                item.Value = loopdate
                rcbMonth.Items.Add(item)
                loopdate = DateAdd("m", -1, loopdate)
            Loop
        End If
    End Sub

    Protected Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
         Dim var_monthval As Date = rcbMonth.SelectedValue
         sql= " select "
         Dim loopdate As Date = DateAdd("m", -2, var_monthval)
         Dim var_columnname As String = ""
         Dim i As Integer = 0
         Do While loopdate <= CDate(var_monthval)
              i += 1
              var_columnname = "sales_" & Month(loopdate) & "_" & Year(loopdate)
              If i > 1 Then sql += ","
              sql += " sales as " & var_columnname
              loopdate = DateAdd("m", 1, loopdate)
         Loop
         sql += " from table where month between '" & DateAdd("m", -2, var_monthval) & "' and '" & var_monthval & "' "
        RadGrid1.DataSource = GetDataTable(Sql)
    End Sub

Private Sub RadGrid1_DetailTableDataBind(ByVal source As Object, ByVal e As GridDetailTableDataBindEventArgs) Handles RadGrid1.DetailTableDataBind
         Dim var_monthval As Date = rcbMonth.SelectedValue
         Dim dataItem As GridDataItem = CType(e.DetailTableView.ParentItem, GridDataItem)
         Dim var_datakeyval As String = dataItem.GetDataKeyValue("Name").ToString()
         sql= " select name "
         Dim loopdate As Date = DateAdd("m", -2, var_monthval)
         Dim var_columnname As String = ""
         Do While loopdate <= CDate(var_monthval)
              var_columnname = "sales_" & Month(loopdate) & "_" & Year(loopdate)
              sql += " , sales as " & var_columnname
              loopdate = DateAdd("m", 1, loopdate)
         Loop
         sql += " from table where month between '" & DateAdd("m", -2, var_monthval) & "' and '" & var_monthval & "' and name = '" &  var_datakeyval & "' "
        e.DetailTableView.DataSource = GetDataTable(Sql)
End Sub

Protected Sub rcbMonth_IndexChanged(ByVal sender As Object, ByVal e As RadComboBoxSelectedIndexChangedEventArgs) Handles rcbMonth.SelectedIndexChanged
        RadGrid1.MasterTableView.Rebind()
    End Sub

Public Function GetDataTable(ByVal query As String) As DataTable
        Dim conn As SqlConnection = New SqlConnection(ConnString)
        Dim adapter As SqlDataAdapter = New SqlDataAdapter
        adapter.SelectCommand = New SqlCommand(query, conn)
        Dim table1 As New DataTable
        conn.Open()
        Try
            adapter.Fill(table1)
        Finally
            conn.Close()
        End Try
        Return table1
    End Function
0
Andrey
Telerik team
answered on 06 Mar 2013, 03:15 PM
Hi,

How are you changing the selected month? I do not see SelectedIndexChanged event of the RadComboBox hooked. Could you please elaborate a bit more about the scenario?

Greetings,
Andrey
the Telerik team
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 their blog feed now.
0
Slava
Top achievements
Rank 1
answered on 06 Mar 2013, 03:28 PM
It's in VB so I'm using "Handles rcbMonth.SelectedIndexChanged" to trigger the SelectedIndexChanged of the RadComboBox... 

Protected Sub rcbMonth_IndexChanged(ByVal sender As Object, ByVal e As RadComboBoxSelectedIndexChangedEventArgs) Handles rcbMonth.SelectedIndexChanged
        RadGrid1.MasterTableView.Rebind()
End Sub
0
Andrey
Telerik team
answered on 11 Mar 2013, 10:47 AM
Hello,

Try to modify the rcbMonth_IndexChanged event as follows:

Protected Sub rcbMonth_IndexChanged(ByVal sender As Object, ByVal e As RadComboBoxSelectedIndexChangedEventArgs) Handles rcbMonth.SelectedIndexChanged
    RadGrid1.DataSource = Nothing
    RadGrid1.MasterTableView.Rebind()
End Sub

This will force RadGrid to fire the NeedDataSource event and may resolve your issue.

Regards,
Andrey
the Telerik team
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 their blog feed now.
0
Slava
Top achievements
Rank 1
answered on 11 Mar 2013, 01:53 PM
Unfortunately that didn't help.
0
Slava
Top achievements
Rank 1
answered on 11 Mar 2013, 02:15 PM
Also, I don't believe that the problem is with the datasource not being "refreshed" since I'm getting the updated recordset, it's just the structure of the columns within the detail table doesn't change...
0
Andrey
Telerik team
answered on 14 Mar 2013, 09:15 AM
Hello,

You need to change your code in accordance with the help topic I suggested in my first reply. Next you need to use NeedDataSource event if you are going to use hierarchy structure, grouping, filtering, sorting or paging.

Regards,
Andrey
the Telerik team
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 their blog feed now.
0
Slava
Top achievements
Rank 1
answered on 14 Mar 2013, 01:55 PM
I am using NeedDataSource and your help topic doesn't really show me how to solve my problem, unless I'm missing something, it shows how to build a grid with static columns, my scenario uses auto generated columns, so can you expend on how that help topic can help with my scenario. I've included my code by using code block here, so it might be more readable.
<telerik:RadComboBox ID="rcbMonth" AutoPostBack="True"
            CausesValidation="False"
            Rows="1" runat="server">
        </telerik:RadComboBox>
<telerik:RadGrid ShowGroupPanel="false" AutoGenerateColumns="True" ID="RadGrid1"
            AllowFilteringByColumn="False" AllowSorting="false"
            ShowFooter="True" runat="server" GridLines="None" AllowPaging="false" EnableLinqExpressions="false"
            FooterStyle-BackColor="LightGray"
            >
            <ExportSettings HideStructureColumns="False" ExportOnlyData="true" IgnorePaging="true" OpenInNewWindow="true">
                <Excel Format="Html" />
            </ExportSettings>
            <MasterTableView Name="ParentGrid" DataKeyNames="Name" ShowGroupFooter="true" AllowMultiColumnSorting="true" CommandItemDisplay="Top"
                    CommandItemSettings-ShowAddNewRecordButton="False"
                    CommandItemSettings-ShowRefreshButton="False" ShowFooter="True">
                <CommandItemSettings ShowExportToWordButton="False" ShowExportToExcelButton="true"
                ShowExportToCsvButton="False" />
                <DetailTables>
                    <telerik:GridTableView DataKeyNames="NameDetail" runat="server" AutoGenerateColumns="True"
                        Name="Details" ShowFooter="False"
                        AllowPaging="False" HorizontalAlign="center"
                        GridLines="None">
                        <Columns>
                        </Columns>
                    </telerik:GridTableView>
                </DetailTables>
                <Columns>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>

code behind:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If Not Page.IsPostBack Then
            Dim reportdefaultdate As Date = DateAdd("m", -1, DateTime.Now.Date)
            Dim reportdefaultmonth As Date = CDate(CStr(Month(reportdefaultdate)) & "/1/" & CStr(Year(reportdefaultdate)))
            Dim loopdate As Date = reportdefaultmonth.Date
            Do While loopdate >= CDate("7/1/2012")
                Dim item As New RadComboBoxItem()
                item.Text = MonthName(Month(loopdate)) & ", " & Year(loopdate)
                item.Value = loopdate
                rcbMonth.Items.Add(item)
                loopdate = DateAdd("m", -1, loopdate)
            Loop
        End If
    End Sub
 
    Protected Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
         Dim var_monthval As Date = rcbMonth.SelectedValue
         sql= " select "
         Dim loopdate As Date = DateAdd("m", -2, var_monthval)
         Dim var_columnname As String = ""
         Dim i As Integer = 0
         Do While loopdate <= CDate(var_monthval)
              i += 1
              var_columnname = "sales_" & Month(loopdate) & "_" & Year(loopdate)
              If i > 1 Then sql += ","
              sql += " sales as " & var_columnname
              loopdate = DateAdd("m", 1, loopdate)
         Loop
         sql += " from table where month between '" & DateAdd("m", -2, var_monthval) & "' and '" & var_monthval & "' "
        RadGrid1.DataSource = GetDataTable(Sql)
    End Sub
 
Private Sub RadGrid1_DetailTableDataBind(ByVal source As Object, ByVal e As GridDetailTableDataBindEventArgs) Handles RadGrid1.DetailTableDataBind
         Dim var_monthval As Date = rcbMonth.SelectedValue
         Dim dataItem As GridDataItem = CType(e.DetailTableView.ParentItem, GridDataItem)
         Dim var_datakeyval As String = dataItem.GetDataKeyValue("Name").ToString()
         sql= " select name "
         Dim loopdate As Date = DateAdd("m", -2, var_monthval)
         Dim var_columnname As String = ""
         Do While loopdate <= CDate(var_monthval)
              var_columnname = "sales_" & Month(loopdate) & "_" & Year(loopdate)
              sql += " , sales as " & var_columnname
              loopdate = DateAdd("m", 1, loopdate)
         Loop
         sql += " from table where month between '" & DateAdd("m", -2, var_monthval) & "' and '" & var_monthval & "' and name = '" &  var_datakeyval & "' "
        e.DetailTableView.DataSource = GetDataTable(Sql)
End Sub
 
Protected Sub rcbMonth_IndexChanged(ByVal sender As Object, ByVal e As RadComboBoxSelectedIndexChangedEventArgs) Handles rcbMonth.SelectedIndexChanged
        RadGrid1.MasterTableView.Rebind()
    End Sub
 
Public Function GetDataTable(ByVal query As String) As DataTable
        Dim conn As SqlConnection = New SqlConnection(ConnString)
        Dim adapter As SqlDataAdapter = New SqlDataAdapter
        adapter.SelectCommand = New SqlCommand(query, conn)
        Dim table1 As New DataTable
        conn.Open()
        Try
            adapter.Fill(table1)
        Finally
            conn.Close()
        End Try
        Return table1
    End Function
0
Andrey
Telerik team
answered on 19 Mar 2013, 09:19 AM
Hi,

NeedDataSource event could be equally used in both scenarios when you have AutoGeneratedColumns to true and to false. The difference with the simple databinding comes when you use advanced functionality. Thus the control will get the needed data as soon as it decides, but in simple databinding you will need to manually provide the data. However, you may not know the correct time to do that and that is why you need to use the NeedDataSource event.

All the best,
Andrey
the Telerik team
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 their blog feed now.
0
Slava
Top achievements
Rank 1
answered on 19 Mar 2013, 02:11 PM
Andrey, did you read my posts and checked the code blocks I've posted here, again I am using NeedDataSource event already and always  was from the beginning of my project, so I'm not sure why you keep going back and suggest something that I'm already doing, I understand the functionality of NeedDataSource and that's why I'm using it, this is the second time within this post I'm letting you know that I'm already using NeedDataSource...

Sorry if I sound little frustrated but it seems to me that you don't really pay attention what I'm posting here and what exactly my issue is with this project.

.

0
Andrey
Telerik team
answered on 22 Mar 2013, 11:37 AM
Hello,

My point in the previous reply was to indicate that the NeedDataSource event could be used in both scenarios.

Since, you are still replicating the issue I will ask you to build the entire RadGrid in code-behind file in the Page_Init event. More information on this approach could be found in this help topic. The code demonstrates programmatically defined columns, but the initial part of the code could be used with autogenerated columns also.

Give this approach a try and check whether the issue still replicates.

Kind regards,
Andrey
the Telerik team
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 their blog feed now.
Tags
Grid
Asked by
Zachary
Top achievements
Rank 1
Answers by
Zachary
Top achievements
Rank 1
Andrey
Telerik team
Slava
Top achievements
Rank 1
Share this question
or