Unigrid does not return the proper SelectedItems

Sergiu Filip asked on March 21, 2016 21:08

I am trying to enhance an existing page that employs a Unigrid control, trying to add a selection (check-boxes) to implement an action on multiple (selected) rows. The selections properly tick and get preserved between pages, but all the selection-count properties return 0 when submitting ( Grid.NewlySelectedItems, Grid.DeselectedItems, etc.)

Recent Answers


Trevor Fayas answered on March 21, 2016 21:11

This sounds like a Page Life Cycle issue.

The control needs to be fully loaded and rendered before you can grab the selected rows. Can you submit your code so we can take a look?

0 votesVote for this answer Mark as a Correct answer

Sergiu Filip answered on March 21, 2016 21:49

Yeah... I thought of that, however I debugged it and inspected the attributes at various points in the life cycle. They seem to be zero all the time. Here is the skeletal logic:

    //during init, I sneak in a couple of buttons (added a place-holder to the UIPager)   
    **protected override void OnInit(EventArgs e)**
    {
        try
        {
            base.OnInit(e);

            List<Button> listActions = new List<Button>();

            Button firstButton = new Button();
            firstButton.ID = "btnPublishAll";
            firstButton.ControlStyle.CssClass = "btn btn-primary";
            firstButton.Text = "Publish Selected";
            firstButton.CommandName = "PUBLISH";
            firstButton.Click += new EventHandler(action_Click);
            listActions.Add(firstButton);

             //add action buttons to the header of the Grid
            Grid.PagerWithActions.AddActions(listActions);

            // Unigrid initialization
            Grid.OnExternalDataBound += DoDataBound;
            Grid.OnDataReload += GetDataSet;
        }

        //the handler for the buttons 
        **protected void action_Click(object sender, EventArgs e)**
        {
        try
        {
            Button buttClicked = sender as Button;
            if (buttClicked == null)
                return;
            var rows = Grid.SelectedItems;
            List<string> additions = Grid.NewlySelectedItems;
            List<string> removals = Grid.DeselectedItems;
            if (buttClicked.CommandName.Equals("PUBLISH"))
                //do something with the selection here;
            if (buttClicked.CommandName.Equals("UNPUBLISH"))
                ;

        }
        catch (Exception ex) {ShowError(ex);}
        }

    **protected override void OnLoad(EventArgs e)**
    {
        try {
            base.OnLoad(e);
            if (StopProcessing)
                return;
            Grid.ReloadData();
        }
        catch (Exception ex)      {
            StopProcessing = true;
            ShowAlert("Error.", ex.Message);
        }

    }

    //and here is where I handle my ExternalDataBound event
    **private object DoDataBound(object sender, string sourceName, object parameter)**
    {
        var row = parameter as DataRowView;
        if (row == null)
            return null;

        switch (sourceName.ToLower())
        {
            case "eventname":
                //Preview Event
                dataCell.CssClass = "coleventname";
                link = new HyperLink()
                {
                    Text = eventName,
                    NavigateUrl = "javascript:void(0)"
                };

             case "date":
                dataCell.CssClass = "coldate";
                return seatingStartDateTime.Value.ToString("ddd MMM d, yyyy");

              etc. etc.
              . . . 
           }
         }
0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on March 21, 2016 23:14

Did you set the SelectedColumn anywhere? I could be wrong but it seems like you need it defined that so it knows what array values to return.

https://docs.kentico.com/display/K9/Reference+-+UniGrid+definition

Sets the name of the column from which the UniGrid loads values into the array of selected rows. You can access the array of selected rows through the SelectedItems property of the UniGrid. By default the UniGrid uses the first column in the data source.

0 votesVote for this answer Mark as a Correct answer

Sergiu Filip answered on March 22, 2016 02:14

Hi Trevor, and thank you for trying to help with this. No,I don't set the selector-column explictely I go with the default, which is the ID of the row in the database (that's the first column); and apparently it works well, as - mentioned in my original post - preserves those ticks between page-by-page navigation.

Errggh, it may be something else.

Cheers ! Sergiu

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on March 22, 2016 14:34

Sorry, i was also having problems with the UniGrid and loading a custom datasource into it. I feel you're pain! If i have time to experiment (i want to get a good prototype going that i can copy and paste that takes any dataset), i'll pass it along.

0 votesVote for this answer Mark as a Correct answer

Peter Cranston answered on March 28, 2017 10:08 (last edited on March 28, 2017 10:09)

I know this is a year old, but I was struggling with a similar issue trying to get the selected items from an extender class. I eventually found that accessing the grid at the pre-render stage meant that SelectedItems was populated. Below is the code I used:

public class CustomUniGridExtender : ControlExtender<UniGrid>
{    
    public override void OnInit()
    {
        ...
        Control.PreRender += Control_PreRender;
        ...
    }

    protected void Control_PreRender(object sender, EventArgs e)
    {
        // Control.SelectedItems is now populated        
    }
}
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.