Programmatically selecting a row in GridView

4 posts, 0 answers
  1. Michael
    Michael avatar
    11 posts
    Member since:
    Oct 2013

    Posted 20 May 2015 Link to this post

    My Challenge:

    I am trying to capture the selected row in a RadGridView which I would like to store in the database when the user exits the screen. When the user re-enters the screen I would like to programmatically select the row previously stored.

    I set up a proof of concept using 2 Test buttons and a GridView:

    Test 1 Button:
     - Get the selected row
     - Cast the selected row object to a string (which would be stored in the database)
     - Cast the string back to an object (simulate return from the database)
     - Add object to a list of objects
     - Clear selected items in the grid

    Test 2 Button:
     - Execute the Select method on the GridView with the list of selected objects as an argument
     - Check the GridView SelectedItems count

    Problem:
    The select method executes but nothing happens. What am I missing? 

    Sample Code:

    private object rowObject = new object();
    private List<object> rowList = new List<object>(); 

    private void test1_Click(object sender, System.Windows.RoutedEventArgs e)
    {
      if (ProjectListingGridView.SelectedItems.Count > 0)
      {
          var item = ProjectListingGridView.SelectedItem.ToString();

          if (item != null)
          {
            var rowObject = (object)item;
            rowList.Clear();
            rowList.Add(rowObject);
            }

          this.ProjectListingGridView.SelectedItems.Clear();
      }
    }

    private void test2_Click(object sender, System.Windows.RoutedEventArgs e)
    {
      var selectList = rowList;

      try
      {
           ProjectListingGridView.Select(selectList);
      }
      catch (Exception)
      {
          throw;
      }
        var cnt = ProjectListingGridView.SelectedItems.Count;
    }

  2. Stefan
    Admin
    Stefan avatar
    1073 posts

    Posted 22 May 2015 Link to this post

    Hello Michael,

    There are two important points here.

    Firstly, in order to select back the previously saved items, RadGridView expects a collection of the same data type that is within the collection bound to the ItemsSource property. In your approach, you cast the items to "object" type and pass them through Select() method, but the control is not meant to auto-cast the "object" type to the data type you are using within the ItemsSource collection.

    And secondly, If you want to select items through Select() method, you will also need to set the SelectionMode of RadGridView to "Multiple". Since the method accepts an IEnumerable collection, internally the control ensures that the selection mode is not "Single". Note, that this is the default value of the property.

    If you are not comfortable with setting the SelectionMode to "Multiple", you can set a single selected item through the "SelectedItem" property of RadGridView.

    I am also attaching a sample project illustrating both approaches for your convenience.

    Best Regards,
    Stefan
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. Michael
    Michael avatar
    11 posts
    Member since:
    Oct 2013

    Posted 27 May 2015 in reply to Stefan Link to this post

    Stefan,

    Thank you for the response. I still seem to be having difficulty.  When I store the selected row in Button1 and restore the selection in Button2, it works great (Thanks to your sample code). I need to serialize the selected object for database storage and deserialize for row re-selection. Serialization/Deserialization seems to work fine but the resulting row selection does not work.   I'm guessing the Type casting is the cause of my problem.  My revised sample code cast the selected row as a ProjectListItemModel. To make matters more complex I would like the object type casting be generic. I would like the final process to be able to store row information for any DataGrid (Projects, Loans, Contracts, etc.). Below is my revised code and assistance is Much Appreciated!!!

    private ProjectListItemModel LastSelectedRow = new ProjectListItemModel();
    private string stringObject;
     
    private void test1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
     
          if (ProjectListingGridView.SelectedItems.Count > 0)
          {
            LastSelectedRow = ProjectListingGridView.SelectedItem as ProjectListItemModel;
     
            XmlSerializer serializer = new XmlSerializer(LastSelectedRow.GetType());
     
            using (StringWriter sw = new StringWriter())
            {
              serializer.Serialize(sw, LastSelectedRow);
              stringObject = sw.ToString();
            }
     
            this.ProjectListingGridView.SelectedItems.Clear();
          }
        }
     
        private void test2_Click(object sender, System.Windows.RoutedEventArgs e)
        {
          if (ProjectListingGridView.HasItems)
          {
            var row = ProjectListingGridView.Items[0];
     
            var deserializer = new XmlSerializer(row.GetType());
     
            TextReader tr = new StringReader(stringObject);
             
            var selectRow = deserializer.Deserialize(tr);
     
            ProjectListingGridView.SelectedItem = selectRow as ProjectListItemModel;
     
            //ProjectListingGridView.SelectedItem = LastSelectedRow;
     
            var count = this.ProjectListingGridView.SelectedItems.Count();
          }
        }

     

     

  4. Stefan
    Admin
    Stefan avatar
    1073 posts

    Posted 28 May 2015 Link to this post

    Hello Michael,

    You have correctly implemented the type casting. The reason for the selection not to work with the deserialized object is that the XmlSerializer.Deserialize() method returns a new instance of the object, which RadGridView does not contain. Please take a look at the How do I deserialize into an existing object forum thread, as couple of approaches for such scenarios are suggested in it.

    As for your second request, if you use the approach with BinarySerialization from the referred forum topic, you should be able to serialize/deserialize all objects you need.

    Hope this helps.

    Best Regards,
    Stefan
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top