1

Im creating a line object which is an entity it has a Tags navigation property which I also want to set. I want to get this tags collection from a datagridview bound to Tag objects :

dgvTags.DataSource = _rs.Tags.Where(x => x.TagGroup.Name == "All Reasons").OrderBy(x => x.Name);

Code with issue :

Line l = new Line
{
    Part = o.Part,
    Description = desc,
    Price = o.Price.Value,
    InvoiceNo = o.InvoiceNo,
    Cost = o.Cost.Value,
    Comments = txtComment.Text,
    Tags = dgvTags.SelectedRows as List<Tag> // <--- needs work here
                    };

The line is showing an error :

Error 5 Cannot convert type 'System.Windows.Forms.DataGridViewSelectedRowCollection' to 'System.Collections.Generic.List' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion C:\SVN\RS\fAddLines.cs 142 15 RS

Is there a correct way of doing this?

UPDATE:

Ive been able to acheive the desired result with the code below, but still looking for the correct way to do it :

foreach (DataGridViewRow r in dgvTags.SelectedRows)
{
    l.Tags.Add(r.DataBoundItem as Tag);
}
David Hall
  • 32,624
  • 10
  • 90
  • 127
sprocket12
  • 5,368
  • 18
  • 64
  • 133
  • you can post the code you have working as an answer rather than as an update to your question. This is the more usual way of posting solutions to you own questions on StackOverflow. – David Hall May 04 '12 at 12:27

2 Answers2

2

Credit to David Hall he tried very hard, but in the end the code below was the simplest solution :

foreach (DataGridViewRow r in dgvTags.SelectedRows)
{
    l.Tags.Add(r.DataBoundItem as Tag);
}
sprocket12
  • 5,368
  • 18
  • 64
  • 133
1

This is one of those cases where there are lots and lots of ways of solving the problem. The code I've given below may not be the best but it works for getting a list of typed objects from the SelectedRows collection.

IList<Tag> myrows = dataGridView1.SelectedRows.Cast<DataGridViewRow>().Select(x => x.DataBoundItem as Tag).ToList();

This uses Linq, and is essentially the same as your method, the Linq just tidies things up a little by removing the foreach.


The problem you are having in your comment is because of no implicit converstion between the System.Collections.Generic.List<T> created by Linq and System.Data.Objects.DataClasses.EntityCollection<T>.

I believe that this should work:

Tags = new System.Data.Objects.DataClasses.EntityCollection<RS.Tag>(
    dataGridView1.SelectedRows.Cast<DataGridViewRow>()
    .Select(x => x.DataBoundItem as Tab).ToList());

I say believe since I've only tested with a conversion to BindingList<T> not with EntityCollection<T>.


And it turns out the EntityCollection has no way of assigning a whole list! From this question here: convert or cast a List<t> to EntityCollection<T> I came up with the following code:

dataGridView1.SelectedRows.Cast<DataGridViewRow>()
    .Select(x => x.DataBoundItem as Tab).ToList().ForEach(y => Tags.Add(y));

I'd probably at this point prefer the vanilla ForEach over this oneliner.

Community
  • 1
  • 1
David Hall
  • 32,624
  • 10
  • 90
  • 127
  • @MuhammadA I've edited with an answer to that problem - you need to create a new EntityCollection list. – David Hall May 04 '12 at 13:13
  • Unfortunately EntityCollection does not have a constructor that takes arguments -- so that new way is not possible. – sprocket12 May 04 '12 at 13:22
  • 1
    @MuhammadA really? lol, ok, yuck :) I've added a bit to the linq that still gives a single line answer, but personally with so many chained expressions I would say that the foreach becomes more readable! Going to leave the answer incase anyone wants to do the same and is using lists that take constructor arguments. – David Hall May 04 '12 at 13:28
  • @MuhammadA just tested that new code against EntityCollection and it works. As I say, not sure I've I'd personally prefer this code over the plain foreach... but still :) – David Hall May 04 '12 at 13:37
  • The vanilla foreach is attractive all of the sudden, thanks for making me realise my hidden talent of making readable code :P -- thanks for all ur help. – sprocket12 May 04 '12 at 13:38
  • @MuhammadA No problem, was actually fun poking around at parts of the framework that I don't use so often. As I said on your post - put what you have as an answer and explain why you ended up preferring it, then mark that as the accepted answer. – David Hall May 04 '12 at 13:42