Question

Ive no problems using Javascript to read the rows of a telerik radgrid component im using however I can seem to find anyway to access the row data server side when a postback occurs. Ive spent ages looking for solution but no luck. Any pointers would be greatly appreciated.

Tony

Was it helpful?

Solution

You might want to look at the DataKeyValues property of the OwnerTableView object, which will let you access a collection of values that represent the fields in a given row. I use it during the EditCommand event handler, since a user of my site is directed to an edit page if they click on the link to edit a row in the grid, and I need to pass along certain info about the given row in the query string.

If this turns out to be what you need, you'll also need to define which fields should be made available through this property. To do that, look at the MasterTableView.DataKeyNames property in the property sheet for the grid. You basically specify a comma-delimited list of field names.

OTHER TIPS

I use the Telerik grid from some time and found these articles in their docs about how to fetch data from selected rows server or client side:

Server-side Client-side

Hope you will find them helpful.

Dick

The server-side is the easy part:

GridItemCollection gridRows = TestGrid.Items;
foreach (GridDataItem data in gridRows)
{
    ItemClass obj = (ItemClass)data.DataItem;
}

It's the client side part that I don't know! :[

private Int32 GetID()
{
    foreach (Telerik.Web.UI.GridDataItem dataItem in radGrid.MasterTableView.Items)
    {
        if (dataItem.Selected == true)
        {
            Int32 ID = (Int32)dataItem.GetDataKeyValue("ID");
            return ID;
        }
    }
    throw new ArgumentNullException("Id Not found");
}
private Int32 GetID()
{
    foreach (Telerik.Web.UI.GridDataItem dataItem in radGrid.MasterTableView.Items)
    {
        if (dataItem.Selected == true)
        {
           // Int32 ID = (Int32)dataItem.GetDataKeyValue("ID");
Int32 ID =Convert.ToInt32(dataItem.GetDataKeyValue("ID"));
            return ID;
        }
    }

}
//this will work

This is the one that works for me and uses the RadGrid.SelectedItems collection.

protected void LinkButton1_Click(object sender, EventArgs e)
    {
        List<Guid> OrderIdList = new List<Guid>();

        foreach (GridDataItem OrderItem in this.RadGrid1.SelectedItems)
        {
            OrderIdList.Add(new Guid(OrderItem.GetDataKeyValue("OrderId").ToString()));
        }
    }

If you correctly created your controls in markup or page init for dynamic controls, then the RadGrid will properly restore state.

You can access the initial values that were loaded from the data source like this example below, provided you told the table view in question to keep the columns around in the data keys.

protected T GetInitialGridData<T>(GridDataItem item, string uniqueColumnName) {
   item.ThrowIfNull("item");
   uniqueColumnName.ThrowIfNullOrEmpty("uniqueColumnName");

   return (T)item.OwnerTableView.DataKeyValues(gridItem.ItemIndex)(columnName);
}

If you are using a dynamic custom template column, and need to get to any values that may now be in their states, you can use:

protected string GetCustomTextBoxValue(GridDataItem item, string controlID) {
   item.ThrowIfNull("item");
   controlID.ThrowIfNullOrTrimmedEmpty("controlID");

   return ((TextBox)item.FindControl(controlID)).Text;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top