Wednesday, 1 October 2014

EP: C# Code to capture mouse click event on AXGridView / Code to get the cell index on AXGidView

EP: C# Code to capture mouse click event on AXGridView / Code to get the cell index on AXGidView


-----we need to write the code in page render method-----

protected override void Render(HtmlTextWriter writer)
    {
        // Ensure that the control is nested in a server form.
        if (Page != null)
        {
            Page.VerifyRenderingInServerForm(this);
        }
        #region Code for mouse click event
        
         //Add the extras to each row.
        foreach (GridViewRow row in AxGridView2.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                // Show hand like the mouseover of buttons. (Not very functional, but very cute.)
                //row.Attributes["onmouseover"] = "this.style.cursor='pointer';";
               
                // Each Cell will need its own PostBack link, with the necessary information in it.
                foreach (TableCell cell in row.Cells)
                {
                    if (row.Cells.GetCellIndex(cell) >= 8) // click to apply only for cell index > 8
                    {
                        // Although we already know this should be the case,
                        // make safe code. Makes copying for reuse a lot easier.
                        if (cell is DataControlFieldCell)
                        {
                            // Put the link on the cell.
                            cell.Attributes["onclick"] =
                                Page.ClientScript.GetPostBackClientHyperlink(AxGridView2,
                                String.Format("CellSelect${0},{1}", row.RowIndex, row.Cells.GetCellIndex(cell)));
                            // Register for event validation: This will keep ASP from giving nasty errors from
                            // getting events from controls that shouldn't be sending any.
                            //Page.ClientScript.RegisterForEventValidation(AxGridView2.UniqueID,
                            //    String.Format("CellSelect${0},{1}", row.RowIndex, row.Cells.GetCellIndex(cell)));    
                        }
                    }
                }
            }
        }
        #endregion Code for mouse click event
        base.Render(writer);
    }

-----Now Add the RowCommand method for the AXGridview-----

///
    ///
    ///

    string selectedCellDate;
    ///
    /// To get selected cell index
    ///

    ///
    ///
    protected void AxGridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {       
        #region
        this.DisplayMatrix();
        // Don't interfere with other commands.
        // We may not have any now, but this is another safe-code strategy.
        if (e.CommandName == "CellSelect")
        {
            // Unpack the arguments.
            String[] arguments = ((String)e.CommandArgument).Split(new char[] { ',' });
            // More safe coding: Don't assume there are at least 2 arguments.
            // (And ignore when there are more.)
            if (arguments.Length >= 2)
            {
                // And even more safe coding: Don't assume the arguments are proper int values.
                int rowIndex = -1, cellIndex = -1;
                int.TryParse(arguments[0], out rowIndex);
                int.TryParse(arguments[1], out cellIndex);
                // Use the rowIndex to select the Row, like Select would do.
                // ...with safety: Don't assume GridView2 even has the row.
                if (rowIndex > -1 && rowIndex < AxGridView2.Rows.Count)
                {
                    AxGridView2.SelectedIndex = rowIndex;
                }
                if (cellIndex >= 8)
                {
                    selectedCellDate = this.ConvertToDateTime(txtFromDate.Text).AddDays(cellIndex - 8).ToString();
                    this.GetRelatedOrders(chkboxProductCategory.Checked, selectedCellDate);
                }
            }
           
        }       
        #endregion       
       
    }
    #endregion
 

No comments:

How to Disable “Advanced Filter or Sort” and Enforce Custom Filters on Any D365FO Form

 In Dynamics 365 Finance and Operations, users can apply filters through the “Advanced filter or sort” feature found under the Options tab...