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
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.
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
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.
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
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.
Happy Thanksgiving!!!
Regards
gc_0620
<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?
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
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
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