Radgrid not exporting data after I change in ItemDataBound

6 posts, 0 answers
  1. Vitaly
    Vitaly  avatar
    68 posts
    Member since:
    Aug 2011

    Posted 17 Jul 2014 Link to this post

    Hi,
    I change column in radgid dynamically in ItemDataBound but when i export the data in to excel i am getting the default value from table:

    protected void EmplReqGrid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
    if (e.Item is GridDataItem)
    {
    GridDataItem item = (GridDataItem)e.Item;
    string ddlTerm = ((DataRowView)e.Item.DataItem)["TERMID"].ToString().Trim();
    if (ddlTerm == "F")
    item.Cells[4].Text = "Fall";
    else
    item.Cells[4].Text = "Spring";
    }
    }

    Instead of showing "Fall" or "Spring" after I exporting grid to excel  it shows "F" or "S".

    Please help me.
    Thanks so much.
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 18 Jul 2014 in reply to Vitaly Link to this post

    Hi Vitaly,

    Such an issue is not expected. Please have a look into the sample code snippet which works fine at my end.

    ASPX:
    <telerik:RadGrid ID="rgrdCountry" runat="server" DataSourceID="sqldsCountry" OnItemDataBound="rgrdCountry_ItemDataBound">
        <MasterTableView CommandItemDisplay="Top" CommandItemSettings-ShowExportToExcelButton="true">
        </MasterTableView>
    </telerik:RadGrid>

    C#:
    protected void rgrdCountry_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            string ddlTerm = ((DataRowView)e.Item.DataItem)["CountryName"].ToString().Trim();
            if (ddlTerm == "NotConfimed")
            {
                item.Cells[3].Text = "Demo";
                 
            }
            else
            {
                item.Cells[3].Text = "Test";
            }
        }
    }

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Vitaly
    Vitaly  avatar
    68 posts
    Member since:
    Aug 2011

    Posted 18 Jul 2014 Link to this post

    Good morning Shinu,
    thanks for you respond.It's still not working.On actual grid it shows Fall and Spring and when I exportint to Excel it shows F and S on the spreadsheet.

    Thanks so much for your help
  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 20 Jul 2014 in reply to Vitaly Link to this post

    Hi Vitaly,

    Please try the below sample code snippet which works fine at my end. If this doesn't help, provide your complete code snippet.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" AllowPaging="true" OnNeedDataSource="RadGrid1_NeedDataSource" OnItemDataBound="RadGrid1_ItemDataBound">
        <ExportSettings ExportOnlyData="true">
        </ExportSettings>
        <MasterTableView CommandItemDisplay="Top">
            <CommandItemSettings ShowExportToExcelButton="true" />
            <Columns>
                <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID" />
                <telerik:GridBoundColumn DataField="Name" HeaderText="Name" UniqueName="Name" />
                <telerik:GridBoundColumn DataField="Number" HeaderText="Number" UniqueName="Number" />
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    C#:
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        dynamic data = new[] {
    new { ID = 1, Name = "Name1", Number=123},
    new { ID = 2, Name = "Name2", Number=234},
    new { ID = 3, Name = "Name3", Number=234},
    new { ID = 4, Name = "Name4", Number=456},
    new { ID = 5, Name = "Name5", Number=567},
    new { ID = 6, Name = "Name6", Number=567},
    new { ID = 7, Name = "Name7", Number=789},
    new { ID = 8, Name = "Name8", Number=896},
    new { ID = 9, Name = "Name9", Number=741}
    };
        RadGrid1.DataSource = data;
    }
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            if (item["Name"].Text == "Name6")
            {
                item["Name"].Text = "Demo";
            }
            else
            {
                item["Name"].Text = "Test";
            }
        }
    }

    Thanks,
    Shinu
  6. Vitaly
    Vitaly  avatar
    68 posts
    Member since:
    Aug 2011

    Posted 21 Jul 2014 Link to this post

    Hi Shinu,
    I do not undestand what should be done in needdata source event.I have this code:

    protected void EmplReqGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
    RadGrid ReqInfoGrid = (RadGrid)EmpReqReport.FindItemByValue("EmployeeData").FindControl("EmplReqGrid");
    ReqInfoGrid.DataSource = Session["dtGrid"];
    }

    Please help.
    Thanks so much.
  7. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 21 Jul 2014 in reply to Vitaly Link to this post

    Hi Vitaly,

    In a NeedDataSource event handler, you should prepare the data source (list of objects) for RadGrid and assign it to the grid's DataSource property. In the NeedDataSource event you can directly access the RadGrid from the sender or using its ID property. Please take a look at this article to know more about Advanced Data-binding (Using NeedDataSource Event). Please elaborate on your requirement if this doesn't help.

    Thanks,
    Shinu
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017