Error with row action in DevExpress Gridview

731 Views Asked by At

I have a DevExpress gridview and I bind datasource from code behind (c#) to it. My action works properly, but when I use rowfilter it doesn't.

I think my command argument, that sends information to my datasource is wrong.

My id in row 1 is 1 and my id in row 13 should be 13 but after row the rowfilter and click on button (row command) on row 13 , my id which sends to code behind (command argument) is 1 !

How can I get the ID from an row (GridView Devexpress)?

My gridview:

<dx:ASPxGridView ID="gv" runat="server" AutoGenerateColumns="False" OnRowCommand="gv_RowCommand"  OnDataBinding="gv_DataBinding"          
        ClientInstanceName="gv" KeyFieldName="Id"
        Width="100%">
        <Columns>
            <dx:GridViewDataTextColumn Caption="" CellStyle-HorizontalAlign="Center" VisibleIndex="0" Width="12%">

                <DataItemTemplate>
                    <asp:LinkButton ID="btnconfirm" Text="Print" Width="70px" CssClass="btn btn-primary btn-small" ForeColor="#cc0000" CommandArgument='<%# Eval("Id") %>'
                        CommandName="print" runat="server"
                        OnClientClick="return confirm('Print?');"> 
                    </asp:LinkButton>

                </DataItemTemplate>
                <HeaderStyle HorizontalAlign="center" />
            </dx:GridViewDataTextColumn>

        </Columns>

        <SettingsPager PageSize="80">
        </SettingsPager>
        <Settings ShowFilterRow="true" />

    </dx:ASPxGridView>

My binding code:

     protected void Page_Load(object sender, EventArgs e)
      {
        if (!IsPostBack)
        {
            refreshGrid();
            gv.DataBind();
        }
    }


  protected void gv_DataBinding(object sender, EventArgs e)
    {
        refreshGrid();
    }


  protected void gv_RowCommand(object sender,     DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
    {
        if (e.CommandArgs.CommandName == "print")
        {
          String _ID = e.CommandArgs.CommandArgument.ToString();

        }
    }

       private void refreshGrid()
        {
        DataClasses1DataContext m = new DataClasses1DataContext();

          var  q = from p in m.TblUserElectronics.AsQueryable()
                from w in m.ViewPersonCarRelations
                where p.CID == w.CarId
                select new
                {
                    p.Id,
                    p.Enabled,
                    CodeTaxi = w.Code_Taxi
                };

        gv.DataSource = q;

    }
1

There are 1 best solutions below

0
On

Javascript Functions :

 function gv_CustomButton_Click(s, e) {
            switch (e.buttonID) {
                case "cmdPrint":
                    if (confirm("Print?")) {
                        s.GetRowValues(e.visibleIndex, 'ID', PrintCommand);
                    }
            }
        }

        function PrintCommand(values) {
            var ID = values; //this will give u actual ID.
            //put your actions here
        }

Modified Devexpress GridView:

  <dx:ASPxGridView ID="gv" runat="server" AutoGenerateColumns="False" OnRowCommand="gv_RowCommand"  OnDataBinding="gv_DataBinding"          
    ClientInstanceName="gv" KeyFieldName="Id" Width="100%">
    <ClientSideEvents CustomButtonClick="gv_CustomButton_Click" />
    <Columns>

    <dx:GridViewCommandColumn VisibleIndex="0" ButtonType="Image" Width="12%" Caption="Actions" CellStyle-VerticalAlign="Top" Visible="false" ShowInCustomizationForm="true">
    <CustomButtons>
                <dx:GridViewCommandColumnCustomButton ID="cmdPrint" Image-ToolTip="Print" CssClass="btn btn-primary btn-small">                        
               </dx:GridViewCommandColumnCustomButton>                                     
    </CustomButtons>
    <HeaderStyle HorizontalAlign="center" />
    </dx:GridViewCommandColumn>    
    </Columns>   
    <SettingsPager PageSize="80">
    </SettingsPager>
    <Settings ShowFilterRow="true" />
    </dx:ASPxGridView>