[SOLVED] how to get value of selected row in aspxgridview devexpress

Issue

I have a devexpress aspxgridview and I need to get the selected row’s value. Does anyone know how to get the selected row’s primary key value without postback. OnSelectionChanged event doesnt get triggered. How can I get the OnSelectionChanged event to be triggered without postback.

<dx:ASPxGridView ID="popupProductsGrid" runat="server" AutoGenerateColumns="False" Width="815px" KeyFieldName="LOGICALREF" ClientInstanceName="popupProductsGrid" 
OnSelectionChanged="popupProductsGrid_SelectionChanged" OnCustomCallback="popupProductsGrid_CustomCallback">
<Columns>
    <dx:GridViewDataTextColumn Caption="KOD" FieldName="URUNKOD" ShowInCustomizationForm="True" VisibleIndex="1" Width="100px">
    </dx:GridViewDataTextColumn>
    <dx:GridViewDataTextColumn Caption="AÇIKLAMA" FieldName="URUN" ShowInCustomizationForm="True" VisibleIndex="2" Width="250px">
    </dx:GridViewDataTextColumn>
    <dx:GridViewDataTextColumn Caption="STOK" FieldName="MIKTAR" ShowInCustomizationForm="True" VisibleIndex="3" Width="50px">
    </dx:GridViewDataTextColumn>
    <dx:GridViewDataTextColumn Caption="LOGICALREF" FieldName="LOGICALREF" ShowInCustomizationForm="True" VisibleIndex="0" Visible="False" Width="100px">
    </dx:GridViewDataTextColumn>
    <dx:GridViewDataTextColumn Caption="BİRİM" FieldName="ANABIRIM" ShowInCustomizationForm="True" VisibleIndex="4" Width="40px">
    </dx:GridViewDataTextColumn>

    </Columns>
    <SettingsBehavior AllowFocusedRow="True" AllowSelectByRowClick="True" AllowSelectSingleRowOnly="True" />
    <Settings ShowFilterRow="True" />
    <SettingsText EmptyDataRow="Listelenecek Kayıt Bulunamadı" />
    </dx:ASPxGridView>


protected void popupProductsGrid_SelectionChanged(object sender, EventArgs e)
    {
        DataRow dr = popupProductsGrid.GetDataRow(popupProductsGrid.FocusedRowIndex);
        Session["stok_kodu"] = dr[0].ToString();
    }

One more thing, I dont want it to postback. So I tried alternative ways like HtmlRowPrepared and CustomCallback.

  protected void popupProductsGrid_HtmlRowPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
    {
        if (e.KeyValue != null)
        {
            string parameter = e.KeyValue.ToString();
            e.Row.Attributes.Add("onclick", "popupProductsGrid.PerformCallback('" + parameter + "')");
        }
    }

    protected void popupProductsGrid_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
    {
        if (e.Parameters != "")
        {
            Session["stok_kodu"] = e.Parameters;
        }
    }

Solution

DevExpress uses Callbacks in almost all of his controls which are basically kinda like postbacks but without reloading the entire page just the control itself (in your case it will be the ASPxGridView with the ID popupProductsGrid).

So assuming that you want to use only callbacks so the page isn’t entirely refreshed for your ASPxGridView you will need

  1. Set ClientInstanceName property of the grid (like popupProductsGrid)
  2. Handle the CustomCallback event (which you are already doing)
  3. Use the PerformCallback function on the client-side whenever a user clicks a row (so you can send the current row index and from that on the server side you can get the primary key and the other row values that you need).

The easiest way to achieve this is using the FocusedRowChanged client-side event to trigger the "on click" that you want and from that call PerformCallback sending the source object property GetFocusedRowIndex to the server side so you can use the GetRowValues method of the grid on the server side code (CustomCallback event)

There is a good example at the end of the documentation for ASPxGridView.CustomCallback Event which does exactly what you want.

Also remember that for callbacks to work as you want, you need to set to false the AutoPostBack property and set to true the EnableCallBacks property of the grid (default behavior is to use callbacks instead of postbacks but check that both properties are properly set).

Answered By – Jorge Iván Burgos Aguilar

Answer Checked By – Mildred Charles (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *