0

What I need to do is to capture the data before being loaded in the dgv and change one of the properties. What I have now is a generic method which I'm using to create the DataSource for my dgv:

dgv.DataSource = new SortableBindingList<T>(result.ResultEntities);

And what I want to do is to get this data, manipulate it the way I need and then return the changed data for loading in the dgv.

I tried to accomplish this by simply doing this:

SortableBindingList<MyType> someVar = (SortableBindingList<MyType>)dgvMydgv.DataSource;

well, it seems it's not the way to do this.

Leron
  • 9,546
  • 35
  • 156
  • 257
  • It amazes me to see this kind of winforms mentality.. Why don't you just keep a reference somewhere to the `SortableBindingList`?? Instead of doing the hack of having to cast the `DataSource` property of whatever UI element? – Federico Berasategui Feb 20 '13 at 14:28
  • Because I thought it's not that strange to get the grid data for whatever purpose and in fact that's what I want to do. – Leron Feb 20 '13 at 14:34
  • 1
    will winforms developers ever understand that [UI is Not Data](http://stackoverflow.com/questions/14381402/wpf-programming-methodology/14382137#14382137)?? – Federico Berasategui Feb 20 '13 at 14:43
  • Ok, I upvoted your answer provided by the link. – Leron Feb 20 '13 at 14:55

1 Answers1

1

If you bind a local variable like your sortableBindingList to an datagridview its not accessable after the datagrid databound successfully. datasource will be NULL after the "DataBound()"-Event fired.

FYI:

If you bind a SqlDataSource to your datagridview you are able to Export the sortablebindinglist after you bound the datasource.

you have to read out every column in every row and save it in a new defined sortablebindinglist and then rebind the datasource to the gridview:

foreach (GridViewRow gvrMyRow in gvFields.Rows)
{
     foreach (TableCell tcMyCell in gvrMyRow.Cells)
     {
         string sMyValue = tcMyCell.Text;
     }
}

Keep in mind:

keeping the refference to the original filled list is the normal way to go and you should go to achive this way. its way easier to find entries, manipulate entries and so on.

Taryn
  • 242,637
  • 56
  • 362
  • 405
noonecares
  • 216
  • 1
  • 6
  • I still don't understand what's the difference between this and keeping the reference to the actual original List. – Federico Berasategui Feb 20 '13 at 14:58
  • keeping the ref to the original list is way better and the "normal way to go" :) but he asked for a way to read out the existing gridview rows. – noonecares Feb 20 '13 at 15:00