This is a migrated thread and some comments may be shown as answers.

Programmatically selecting a row in GridView

3 Answers 1912 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 20 May 2015, 04:55 PM

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;
}

3 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 22 May 2015, 12:00 PM
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
0
Michael
Top achievements
Rank 1
answered on 27 May 2015, 06:09 PM

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();
      }
    }

 

 

0
Stefan
Telerik team
answered on 28 May 2015, 12:42 PM
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
Tags
GridView
Asked by
Michael
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Michael
Top achievements
Rank 1
Share this question
or