Access Row Values from Item Command

12 posts, 0 answers
  1. allen oleksak
    allen oleksak avatar
    2 posts
    Member since:
    Jul 2009

    Posted 02 Aug 2010 Link to this post

    I have a Link Button in my Rad grid, and am able to capture the Item_Command event when clicking.  However, I cant figure out how to access the other values within the row from in the item command.  I dont need just the data key values, and the other examples for the update command and delete command, i need access to the data in all columns.

    Basically i have a url with some special variables that the page will redirect to when clicked, and i need to basically loop through all the columns,and replace the url variables dynamically with the values associated with the correct column.

    For example, say my url has a parameter {orderNumber}.  I want to replace that value in the string with the value from the orderNumber column thats in my row. 

    I am aware of the hyperlink button, but cant go that route because under certain circumstances we're not allowed to show the querystring.

    So, all that said, in an item command does anyone know how to loop through and grab the values from the other columns?
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 03 Aug 2010 Link to this post

    Hello Allen,

    You can directly access the cell value of that row by using 'ColumnUniqueName'. Check out the following code snippet.

    C#:
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
     {
      if (e.CommandName == "Command")// CommandName of LinkButton
       {
        GridDataItem item = (GridDataItem)e.Item;
        string value = item["ColumnUniqueName"].Text; // access the value in column using 'UniqueName' of that column       
       }
     }

    Please take a look at the document link below.
    Accessing cells and rows

    Thanks,
    Princy.
  3. allen oleksak
    allen oleksak avatar
    2 posts
    Member since:
    Jul 2009

    Posted 03 Aug 2010 Link to this post

    Thanks for the quick response.

    I'm aware of using that method, I was hoping for a more generic way to loop through each column and get the column/uniquename for that column and replace tokens with the column value for the row.  To elaborate a little further imagine i have this url stored in my database.

    page.aspx?OrderNumber={ORDERNUMBER}&CustomerName={CUSTOMERNAME}

    Each thing surrounded by { } is a token that needs to be replaced by the value in that column for the row clicked.  I need the event handler to not make any assumptions about the url, or the column names, it simply needs to do a generic find/replace.

    So that said the user clicks my link button for some row in the grid.  I now have access to the item they clicked in the Item_Command.  From there, i need a way to loop through generically, grab each column name and do like a url = url.Replace("{" + ColumnName + "}",ColumnValue) kind of a thing.

    Is there a way to do this completely dynamically like that?

    Thanks,
    Allen
  4. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 04 Aug 2010 Link to this post

    Hello Allen,

    You could iterate through the columns of grid using 'foreach' loop as shown below. Get the corresponding column's UniqueName and cell value and add to your url.

    C#:
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
       {
           if (e.CommandName == "Command")// CommandName of LinkButton
           {
               string url="page.aspx?";
               GridDataItem item = (GridDataItem)e.Item;
               foreach (GridColumn col in RadGrid1.MasterTableView.Columns)
               {
                   string columnname="";
                   string value="";
                   if (col.ColumnType=="GridBoundColumn")
                   {
                        columnname = col.UniqueName;//UniqueName of GridBoundColumn
                        value = item[col.UniqueName].Text;//cell value of GridBoundColumn
                        url +=  columnname + "=" + value + "&";
                   }
               }
               string url2 = url.Substring(0, url.Length - 1);
           }
       }

    Thanks,
    Princy.
  5. gc_0620
    gc_0620 avatar
    246 posts
    Member since:
    Feb 2008

    Posted 23 Nov 2010 Link to this post

    Hi Princy,

    While in GridEditTableItem, can one access GridDataItem either in Item_Command event or Item_Databound event?  Lets say below is the code:
    if (e.Item is GridEditableItem && e.Item.IsInEditMode
    && e.Item.OwnerTableView.Name == "Master") 
     
    {
      
    // How to access "Master" Table GridDataitem columns. Below does not work for me. 
      
    GridDataItem ditem = (GridDataItem)e.Item; 
      
    }



    As always Thanks

    GC_0620
  6. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 24 Nov 2010 Link to this post

    Hello,

    I guess you are using RadGrid EditMode as 'EditForms'. If so try the following code snippet to get GridDataItem.

    C#:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
           if (e.Item is GridEditFormItem && e.Item.IsInEditMode
    && e.Item.OwnerTableView.Name == "Master"
            {
               GridEditFormItem editItem = (GridEditFormItem)e.Item;
               GridDataItem item = (GridDataItem)editItem.ParentItem; // accessing DataItem
            }
        }

    Thanks,
    Princy.
  7. gc_0620
    gc_0620 avatar
    246 posts
    Member since:
    Feb 2008

    Posted 24 Nov 2010 Link to this post

    Thanks Princy. As always your solution works.

    Happy Thanksgiving!!!

    Regards

    gc_0620
  8. Sheenu
    Sheenu avatar
    1 posts
    Member since:
    Aug 2012

    Posted 15 Aug 2012 Link to this post

    Thanks Princy your solution worked. i was looking for this for 2 days and you gave me peace of mind.
  9. Margret
    Margret  avatar
    47 posts
    Member since:
    Mar 2013

    Posted 15 May 2013 Link to this post

    Hi,all

    <telerik:GridTemplateColumn DataField="Place "

                 HeaderText="Place "

                 UniqueName=" Place " SortExpression=" Place  asc">

                 <ItemTemplate>

                        <a href="#" onclick="showDetails(<%# Eval("Place ")%>,<%# Eval("Placeitem ")%>);return false;" target="_self">            

                     <asp:Label ID="lblPlace" runat="server"  Text='<%# Eval("Place ")%>' Visible="True"></asp:Label></a>

                </ItemTemplate>

            </telerik:GridTemplateColumn>

    Place:Red-Hills

    Here Place –Red,Placeitem-Hills will be passed in the showdetails function.
    How can i get the Red and Hills from Red-Hills in this scenario?

  10. Kostadin
    Admin
    Kostadin avatar
    1733 posts

    Posted 20 May 2013 Link to this post

    Hi Margret,

    Could you please elaborate a little bit more on your scenario? As far as I understand the Red and Hills are generated from two different data fields, so they are already separated. Are you trying to get them on server side or on client side?

    All the best,
    Kostadin
    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.
  11. Dehanys
    Dehanys avatar
    3 posts
    Member since:
    Mar 2016

    Posted 07 Aug 2018 Link to this post

    Hello Princy.

    I have a similar issue to the one that you responded, and I do not know why your first example it is not working for me. I have found this same example over and over again, but in my case simply doesn't do it. I have an exception of 'Telerik.Web.UI.GridCommandItem' to type 'Telerik.Web.UI.GridDataItem'. Help please!!!

    Protected Sub grdPhases_ItemCommand(sender As Object, e As GridCommandEventArgs) Handles grdPhases.ItemCommand
    If e.CommandName = "InitInsert" Then
    Dim item As GridDataItem = CType(e.Item, GridDataItem)  <==this is where it fails.

    Dim value As String = item("RecruitProfileID").Text
    End If
    End Sub

  12. Eyup
    Admin
    Eyup avatar
    3988 posts

    Posted 10 Aug 2018 Link to this post

    Hello Dehanys,

    The command you are probably looking for is called PerformInsert:
    https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/control-lifecycle/command-reference-

    Please replace it with InitInsert and the issue should be resolved. Alternatively, you can use the separate OnInsertCommand event handler provided by the grid.

    You can also check the resources provided here:
    https://www.telerik.com/forums/edit-value-from-gridboundcolumn#Kk0ZY89DGU-v8BXstZgrCw

    As well as this one:
    https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/data-editing/database-level-editing

    I hope this will prove helpful.

    Regards,
    Eyup
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top