This is a migrated thread and some comments may be shown as answers.
Access Row Values from Item Command
11 Answers 829 Views
This is a migrated thread and some comments may be shown as answers.
allen oleksak
Top achievements
Rank 1
allen oleksak asked on 03 Aug 2010, 02:11 AM
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?

11 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 03 Aug 2010, 08:18 AM
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.
0
allen oleksak
Top achievements
Rank 1
answered on 03 Aug 2010, 06:13 PM
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
0
Princy
Top achievements
Rank 1
answered on 04 Aug 2010, 08:02 AM
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.
0
gc_0620
Top achievements
Rank 1
answered on 24 Nov 2010, 12:12 AM
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
0
Princy
Top achievements
Rank 1
answered on 24 Nov 2010, 06:39 AM
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.
0
gc_0620
Top achievements
Rank 1
answered on 24 Nov 2010, 10:14 PM
Thanks Princy. As always your solution works.

Happy Thanksgiving!!!

Regards

gc_0620
0
Sheenu
Top achievements
Rank 1
answered on 15 Aug 2012, 05:27 PM
Thanks Princy your solution worked. i was looking for this for 2 days and you gave me peace of mind.
0
Margret
Top achievements
Rank 1
answered on 15 May 2013, 12:02 PM
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?

0
Kostadin
Telerik team
answered on 20 May 2013, 06:53 AM
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.
0
Dehanys
Top achievements
Rank 2
answered on 07 Aug 2018, 04:11 PM

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

0
Eyup
Telerik team
answered on 10 Aug 2018, 05:34 AM
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.
Tags
Grid
Asked by
allen oleksak
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
allen oleksak
Top achievements
Rank 1
gc_0620
Top achievements
Rank 1
Sheenu
Top achievements
Rank 1
Margret
Top achievements
Rank 1
Kostadin
Telerik team
Dehanys
Top achievements
Rank 2
Eyup
Telerik team
Share this question
or