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

can not get the DataItem of SelectedItems.

8 Answers 586 Views
Grid
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
James asked on 13 May 2013, 06:30 AM
          protected void RadButtonSave_Click(object sender, EventArgs e)
        {
            foreach (GridItem item in RadGrid1.Items)
            {
                item.DataItem // this is null
            }

            foreach (GridItem item in this.RadGrid1.SelectedItems) //get SelectedItems successfully
            {
                 but item.DataItem is null
            }
        }

can not get the DataItem of SelectedItems.

8 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 13 May 2013, 07:13 AM
Hi,

Please try the following code snippet.

C#:
protected void RadButtonSave_Click(object sender, EventArgs e)
{
    foreach (GridDataItem item in RadGrid1.Items)
        {
            //your code
        }
 
    foreach (GridDataItem item in this.RadGrid1.SelectedItems)
        {
            //your code
        }
}

Thanks,
Princy.
0
James
Top achievements
Rank 1
answered on 13 May 2013, 09:08 AM
Hi Princy,

It doesn't work also.
0
Princy
Top achievements
Rank 2
answered on 14 May 2013, 05:02 AM
Hi,

Unfortunately I couldn't replicate any issue at my end. Here is the complete code I tried.

ASPX:
<telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource2" AutoGenerateColumns="false"
    runat="server">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn DataField="OrderID" UniqueName="OrderID">
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
    <ClientSettings Selecting-AllowRowSelect="true">
    </ClientSettings>
</telerik:RadGrid>
<telerik:RadButton ID="Button1" runat="server" OnClick="Button1_Click" />
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString3 %>"
    SelectCommand="select top 10  * from [Orders]"></asp:SqlDataSource>

C#:
protected void Button1_Click(object sender, EventArgs e)
{
    foreach (GridDataItem item in RadGrid1.Items)
    {
        string value1 = item["OrderID"].Text;
    }
 
    foreach (GridDataItem item in this.RadGrid1.SelectedItems)
    {
        string value2 = item["OrderID"].Text;
    }
}

Please elaborate the scenario if it doesn't help.

Thanks,
Princy.
1
Vijay
Top achievements
Rank 1
answered on 14 May 2013, 07:02 PM
Hi James, 

I ran into a similar problem but only after upgrading to the latest Q1 2013 release. For me, I was unable to access the dataItem for hidden columns (visible="false").

After reading the following post about breaking changes, I changed my hidden columns to use display="false" instead of visible="false" and am now able to access the dataItem values like I used to.

http://www.telerik.com/community/forums/aspnet-ajax/grid/breaking-change-hidden-column-cell-text-is-not-persisted-in-viewstate.aspx

Hope this helps,

Vijay
 
Jose
Top achievements
Rank 1
commented on 22 Feb 2023, 07:54 PM

Excellent contribution, I had been giving it for a while because I did not bring the data after updating the telerik.
Thanks a lot.
0
Andrey
Telerik team
answered on 17 May 2013, 12:45 PM
Hello,

The DataItem property is only initialized during the databinding events like the ItemDataBound event. In any other event the DataItem property will return null.

Try to modify your code as follows and you should succeed:

protected void RadButtonSave_Click(object sender, EventArgs e)
{
    RadGrid1.DataSource = null;
    RadGrid1.Rebind();
}
 
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem dataItem = e.Item as GridDataItem;
        //dataItem.DataItem now will hold the datasource data
    }
}

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
PPRINC
Top achievements
Rank 1
answered on 18 Jun 2013, 06:12 PM
Hello Dear,

I am facing an issue.

After upgrading from telerik v2009  to  telerk v2012 , I can't get my selected rows values in a radgrid with checkboxes from code behind

here is the code in the markup
<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True"
                          PageSize="5"     Width="100%"    OnPageIndexChanged="RadGrid1_PageIndexChanged"
                            GridLines="None" Skin="Vista" AllowMultiRowSelection="True"  >
                            <MasterTableView >
                            <Columns>
                                <telerik:GridClientSelectColumn UniqueName="ClientSelectColumn" ></telerik:GridClientSelectColumn>
                            </Columns>
                                <RowIndicatorColumn>
                                    <HeaderStyle Width="20px" />
                                </RowIndicatorColumn>
                                <ExpandCollapseColumn>
                                    <HeaderStyle Width="20px" />
                                </ExpandCollapseColumn>
                                <PagerStyle Mode="NextPrev" />
                            </MasterTableView>
                            <HeaderStyle Font-Bold="True" Font-Italic="False" Font-Overline="False"
                                Font-Strikeout="False" Font-Underline="False" Wrap="True" />
                            <ClientSettings EnableRowHoverStyle="true"  >
                                <Selecting AllowRowSelect="True" UseClientSelectColumnOnly = "true"   />
                            </ClientSettings>
                        </telerik:RadGrid>
and now the code behind

 protected void  Ok_Click(object sender, System.EventArgs e)
        {
foreach (GridDataItem item in RadGrid1.SelectedItems)
               {
                   CheckBox chk = (CheckBox)item["ClientSelectColumn"].Controls[0];
                   id = item["ID"].Text; // get the id with its column name
                   name = item["NAME"].Text; // get the name with its column name
                   chec = chk.Checked;
 
 
               }
}

It doesn't even enter in the loop.
What can I do to resolve it ?

Thank in advance for your assistance
0
Andrey
Telerik team
answered on 20 Jun 2013, 01:11 PM
Hi,

Could you share your full page source code along with the code-behind file content? Thus all the people who want to help you will have better understanding of your case.

Regards,
Andrey
Telerik
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 the blog feed now.
0
PPRINC
Top achievements
Rank 1
answered on 24 Jun 2013, 09:54 AM
Aleardy found!

As i'm using master pages, I just needed to ad add a radAjaxManager on my master page and a radAjaxManagerProxy on the content page to get it fixed. and that's done.

Thanks anyway,

Regards.
Tags
Grid
Asked by
James
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
James
Top achievements
Rank 1
Vijay
Top achievements
Rank 1
Andrey
Telerik team
PPRINC
Top achievements
Rank 1
Share this question
or