Philippsen's Blog

Everyday findings in my world of .net and related stuff

Multiselection checkboxes in an AxGridview

Posted by Torben M. Philippsen on October 30, 2014

For a customer Ihad to fix a very non user friendly behavier in an AxGridview control.

The scenario was that they had an AxGridview control containing attachments in an expence module. For a single Expence you would one or more attachments – those attachments would be shown in the AxGridview when viewin the given expence post. In the gridview you would be able to select and deselect attachments but in the standard behavior the user flow would be something like this:

  1. Mark a row in the gridview – wait for postback to finish
  2. now the row is in edit mode – check the combobox – wait for postback
  3. click the update command button in the row – wait for poatback to finish

That’s a lot of waiting and posting back just to check a combobox. Ofcourse the customer wanted a more smooth solution.

My workaround was to try and create comboboxes that would be checkable but without posting back all the time. Doing this I had to consider how I would read the checked state from the comboboxes and persist the state to Ax/the underlying dataset. Here’s what I ended up with…

The markup

In order to have a combobox that is checkable at all times – not just in editmode, you will have to create a template field
EP_Multiselect_markup
Please notice that the AxGridview doesn’t allow paging and editing. Also you should notice that the combox doesn’t postback.

The first time the page loads checkboxes should be checked according to values from AX. Notice that the checked propety is handled by the Marked method

private Hashtable checkbox_values = new Hashtable();

    /// <summary>

    /// Method used in when each row is databound (See markup)

    /// </summary>

    /// <param name="dataItem"></param>

    /// <param name="container"></param>

    /// <returns></returns>

    protected bool Marked(object dataItem, object container)

    {

        AxGridViewRow gridViewRow = (AxGridViewRow)container;

        //CheckBox checkbox = (CheckBox)gridViewRow.FindControl("markCheckbox");

        DataSetViewRow row = dataItem as DataSetViewRow;

        if (row != null)

        {

            //string hashcode = checkbox.UniqueID;

            string hashcode = gridViewRow.RowIndex.ToString();

            if (this.checkbox_values.ContainsKey(hashcode))

            {

                //return row.Mark((bool)this.checkbox_values[hashcode]);

                return (bool)checkbox_values[hashcode];

            }           

        }

        return false;

    }

You will notice that the checked state depends on whether the current rows combobox has been added to a hashtable. The hashtable contains a boolean value for each rows combobox indicating whether it should be checked or not. The hashtable is populated on every postback like this:

 

   protected void Page_Load(object sender, EventArgs e)

    {

        getChecked(false);

 

 

//EG/2014.10.28/tomph, 00392460 XpenseAttachments –>

    /// <summary>

    /// Method used to fetch all marked checkboxes from the grid.

    /// This approach is used because we use a classic .net combobox with no postback (not an axboundfield)

    /// </summary>

    /// <param name="_saveToDataset">indicates whether this method is triggered from the save button click event or is just a normal postback</param>

    protected void getChecked(bool _saveToDataset)

    {

        foreach (GridViewRow row in AxGridView1.Rows)

        {

            if (row.RowType == DataControlRowType.DataRow)

            {

                CheckBox chk = (CheckBox)row.FindControl("markCheckbox");

                string hashcode = row.RowIndex.ToString();

                    if (chk.Checked)

                    {

                        if (!checkbox_values.ContainsKey(hashcode))

                        {

                            //only add if it does not exist

                            checkbox_values.Add(hashcode, true);

                        }

                        else

                        {

                            //update value if exists

                            checkbox_values[hashcode] = false;

                        }

                        if (_saveToDataset)

                        {      

                               //save the checked state to the datasat

                               DataSetView dsv = this.ImageDS.GetDataSourceView(this.AxGridView1.DataMember).DataSetView;

                               dsv.SetCurrent(row.RowIndex);

                               DataSetViewRow dsvr = dsv.GetCurrent();

                               dsvr.BeginEdit();

                               dsvr.SetFieldValue("selection**", 1);

                               dsvr.EndEdit();

                               this.AxGridView1.UpdateRow(row.RowIndex, false);

                        }

                    }

                    else

                    {

                        if (!checkbox_values.ContainsKey(hashcode))

                        {

                            //only add if it does not exist

                            checkbox_values.Add(hashcode, false);

                        }

                        else

                        {

                            //update value if exists

                            checkbox_values[hashcode] = false;

                        }

                        if (_saveToDataset)

                        {

                            //save the unchecked state to the datasat

                            DataSetView dsv = this.ImageDS.GetDataSourceView(this.AxGridView1.DataMember).DataSetView;

                            dsv.SetCurrent(row.RowIndex);

                            DataSetViewRow dsvr = dsv.GetCurrent();

                            dsvr.BeginEdit();

                            dsvr.SetFieldValue("selection**", 0);

                            dsvr.EndEdit();

                            this.AxGridView1.UpdateRow(row.RowIndex, false);

                        }

                    }               

            }

        }

    }

The only thing we will have to do know is to persist the checked comboboxes to the data, when clicking a “save/update” button. The save button click event calls the same method as above. Only difference is that the boolean value true is passed which again causes each row to be updated.

    protected void UpdateButton_Click(object sender, EventArgs e)

    {

        getChecked(true);

        this.ImageDS.GetDataSet().DataSetRun.AxaptaObjectAdapter.Call("updateSelections");

    }

Advertisements

Sorry, the comment form is closed at this time.

 
%d bloggers like this: