Search code examples
mysqltelerikradgriddata-retrieval

I am unable to retrieve column value in a Telerik radgrid.


I am trying to select a column value in a selected row but I am unable to select the row. It seems that after clicking on the row to be selected, it does not go into the if statement. If I change the if statement to (dataItem.Selected = true) with only one "=", it goes in but returns the invoice id for all of the rows. Any advice on how to resolve this issue?

protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e)
    {

        var a ="";
        foreach (GridDataItem dataItem in RadGrid1.MasterTableView.Items)
        {
            if (dataItem.Selected == true)
            {

                a = dataItem.GetDataKeyValue("InvoiceId").ToString();
                Response.Write(a);
            }
        }


    }

Solution

  • Do you allow multiple selections or only just single selection in your RadGrid?

    To me, the foreach loop code block doesn't seem correct if you do single row selection.

    protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e)
    {
       // get selected row
       GridDataItem item =(GridDataItem)RadGrid1.SelectedItems[0]; 
    }
    

    Also, another point is you should set EnablePostBackOnRowClick property to true so that RadGrid's SelectedIndexChanged event will be fired properly on the server side.

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

    But if you wish to fire row select command from client side, then you should add a row click event with JS.

    function RowClick(sender, eventArgs) {
        sender.get_masterTableView().fireCommand("Select", eventArgs.get_itemIndexHierarchical());
    }
    

    Then associate that JS function to <ClientEvents OnRowClick="RowClick" /> in your aspx.