Search code examples
asp.netdevexpressaspxgridview

AspxGridView checkbox checked column value


I am using one aspxGridview where I used checkbox. Now I need when I check any of the row particular column value I should get in server side to complete my business logic. Below is the gridview used:

<dx:ASPxGridView KeyFieldName="PracticeID" ID="ASPxGrd" runat="server" ClientInstanceName="grid"
                            ClientIDMode="AutoID" AutoGenerateColumns="false" Width="100%" OnSelectionChanged="ASPxGrd_SelectionChanged">
                            <Columns>
                                <dx:GridViewDataColumn VisibleIndex="0" Name="CheckBoxColumn">
                                    <DataItemTemplate>
                                        <dx:ASPxCheckBox ID="ASPxCheckBox1" runat="server" OnCheckedChanged="ASPxCheckBox1_CheckedChanged" AutoPostBack="true">
                                        </dx:ASPxCheckBox>
                                    </DataItemTemplate>
                                </dx:GridViewDataColumn>
                                
                                <dx:GridViewDataColumn FieldName="PracticeName" Caption="Description" VisibleIndex="1">
                                <FooterTemplate>
                                    Total:
                                </FooterTemplate>
                                </dx:GridViewDataColumn>
                                 </dx:ASPxGridView>

I have tried to use oncheckedevent in checkbox with auto postback true and used code to get selected row like below:

 protected void ASPxCheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            ASPxGridView grid = sender as ASPxGridView;
            string currentMasterKey = Convert.ToString(grid.GetMasterRowKeyValue());
        }
but getting null value of grid object. Need help.


Solution

  • In your example you have used DataItemTemplate, so in that case the sender will be the control which is added in that data template i.e ASPxCheckBox and you are casting it to grid bcoz of that it is getting null.

    try out below snippet.

    protected void ASPxCheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        ASPxCheckBox checkBox = sender as ASPxCheckBox;
        var grid = (checkBox.NamingContainer as DevExpress.Web.ASPxGridView.GridViewDataItemTemplateContainer).Grid;
        string currentMasterKey = Convert.ToString(grid.GetMasterRowKeyValue());
    }