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

Radgrid not exporting data after I change in ItemDataBound

5 Answers 65 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Vitaly
Top achievements
Rank 1
Vitaly asked on 17 Jul 2014, 02:25 PM
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.

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 18 Jul 2014, 06:13 AM
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.
0
Vitaly
Top achievements
Rank 1
answered on 18 Jul 2014, 02:09 PM
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
0
Shinu
Top achievements
Rank 1
answered on 21 Jul 2014, 03:59 AM
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
0
Vitaly
Top achievements
Rank 1
answered on 21 Jul 2014, 07:47 PM
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.
0
Shinu
Top achievements
Rank 1
answered on 22 Jul 2014, 03:48 AM
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
Tags
General Discussions
Asked by
Vitaly
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
Vitaly
Top achievements
Rank 1
Share this question
or