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

RadGrid1_ItemCommand Can't get cell value

5 Answers 116 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Anieda
Top achievements
Rank 1
Anieda asked on 28 Apr 2014, 08:22 PM
I have this block of code which worked at the prior version (VS 2010, older RadGrid control).  Now I upgraded to VS 2013 and use RadGrid control in version 2013.3.114.45,  I can't figure out why I can't get the "ApplicationId" value from e.item.

Please see code below:
private void LoadData()
        {
            if (RadGrid1.Items.Count > 0) RadGrid1.DataSource = new Object[0];            //If Academic year is selected
            if (editAcademicYear.SelectedValue !="Select One")
            {
                var academicyear = (from ay in context.AcademicYears
                                    where ay.Name == editAcademicYear.SelectedValue
                                    select ay).First();
                yearID = academicyear.AcademicYearID;
            }   if (rbLName.Checked)
   {
    if (editSName.Text.Length > 0)
    {
     if (yearID > 0)
     {
      application = from s in context.Applications.Include("AcademicYear")
           where s.LastName.StartsWith(editSName.Text.Trim()) &&
           s.AcademicYear.AcademicYearID == yearID
           orderby s.LastName, s.FirstName
           select s;
     }
     else
     {
      application = from s in context.Applications
           where s.LastName.StartsWith(editSName.Text.Trim())
           orderby s.LastName, s.FirstName
           select s;
     }
     ShowApps(application, application.Count());   //Show data to RadGrid1 for selection.    }
    else
    {
     lblmissing.Visible = true;
     lblmissing.Text = "Please enter the last name.";
     return;
    }
   }
         }
  
//question:  how can I get the value for dataItem below?  What is wrong here?
protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
           
            LoadData();
            //from debugging, I saw "application" value, but I did not see value
              from RadGrid1 or its "items".            try
            {
                if (e.Item.ItemIndex > -1)
                {
                    var dataItem = e.Item as GridDataItem;   //dataItem has no value here
                    string appid = dataItem["ApplicationId"].Text;    //therefore, appid has no value
                    Response.Redirect("DisplayApp.aspx?appid=" + appid);
                }
            }
            catch (Exception ex)
            {
                logger.ErrorException("Could not get appid from the grid row", ex);
                lblMessage.Visible = true;
                RadGrid1.Visible = false;
                lblMessage.Text = "<b>Error exists in the grid level.  Please contact the Care Tech Support.</b>";
            }
    } 

I have spent a few hours searching, the site is in production, but just can't get the "appid" of the selected row to display the detail page.  Your quick response is much appreciated.

Thanks in advance!

5 Answers, 1 is accepted

Sort by
0
Anieda
Top achievements
Rank 1
answered on 30 Apr 2014, 02:22 AM

I want to get dataItem value.  What is the correct way?  e.Item has no value here.

Thanks for your help!

protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
    //load data to RadGrid1     
    LoadData();    
             
     try
     {
           if (e.Item.ItemIndex > -1)
           {
             //dataItem has no value, is there any other way to write the code so that I can get the the dataItem value                
            var dataItem =  e.Item as GridDataItem;                        
           string appid = dataItem["ApplicationId"].Text;                        
           Response.Redirect("DisplayApp.aspx?appid=" + appid);
          }
    }
    catch (Exception ex)
    {
          logger.ErrorException("Could not get appid from the grid row", ex);
          lblMessage.Visible = true;
          RadGrid1.Visible = false;
          lblMessage.Text = "<b>Error exists in the grid level.  Please contact the Care Tech Support.</b>";
     }
}
0
Shinu
Top achievements
Rank 2
answered on 30 Apr 2014, 05:53 AM
Hi Anieda,

I guess you want to get the selected rows of the grid. Please take a look at the below code snippet.

ASPX:
<ClientSettings Selecting-AllowRowSelect="true" EnablePostBackOnRowClick="true">
</ClientSettings>

C#:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
  if (e.CommandName == "RowClick")
  {
   GridDataItem dataItem = (GridDataItem)e.Item;
   string appid = dataItem["ApplicationId"].Text;
  }
}

Thanks,
Shinu
0
Anieda
Top achievements
Rank 1
answered on 30 Apr 2014, 06:50 AM

Hello Shinu,

Thank you very much for your response.  On ClentSettings, the RadGrid only has AllowRowSelect="True" option in this version of RadGrid control and has been set.  There is no "EnablePostBackOnRowClick" option.

<ClientSettings>
           <Selecting AllowRowSelect="True" />         
</ClientSettings>

What should I do next? 

Thanks!

0
Accepted
Eyup
Telerik team
answered on 01 May 2014, 10:04 AM
Hello Anieda,

The initial problem may be related to some invisible columns. Please check this thread for a detailed explanation:
http://www.telerik.com/forums/breaking-change-hidden-column-cell-text-is-not-persisted-in-viewstate

Hope this helps.

Regards,
Eyup
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Anieda
Top achievements
Rank 1
answered on 05 May 2014, 03:12 AM
Thanks Eyup, after reading the link you sent, it solved the issue.

Thanks a million! 
Tags
Grid
Asked by
Anieda
Top achievements
Rank 1
Answers by
Anieda
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Eyup
Telerik team
Share this question
or