0

Code:

private void DataGrid_UnloadingRow(object sender, DataGridRowEventArgs e)
{
    var dataGridRow = e.Row;
    [...]
}

When I extract the Row.GetIndex(), I always get -1. Is there a way to find the row index that used to be occupied by the gone row?

TIA.

Travis Banger
  • 697
  • 1
  • 7
  • 19
  • [XY Problem](http://meta.stackexchange.com/a/66378). What do you want that for? – Federico Berasategui Dec 05 '13 at 17:34
  • You call it XY? I call it: "When somebody has a hammer *everything* seems line a nail" :-) It is a similar/related concept, anyway. At this point I am brainstorming, and maybe not even asking the right question. I am not even sure of which rows should survive and which disappear. Each row is an XML file and after perusal, my code discovers: "This XML file does not contain what I need - Maybe I should just remove its row?" The user can actually *fix* some rows, by double-click and further processing. – Travis Banger Dec 05 '13 at 19:33
  • everything you're talking about is `data`, I completely fail to see how all that is related to UI. You should take care of `data`-related concepts in a `data`-related layer (such as the Model), and of `application logic`-related concepts in an application layer such as the `ViewModel`. None of this has anything to do with UI. – Federico Berasategui Dec 05 '13 at 19:36
  • @HighCore: This is the situation: A row is removed and I need to do something about it, right? The GUI row is tied (by Binding) to the underlying model "row" (properties). So when my code is called, what do I care whether it was in response to the visual GUI or the underlying model? The real problem is that my code is called **after** the fact (a typical problem with interrupt-based processing). Well, it is a moot point. I am rethinking the whole thing. – Travis Banger Dec 05 '13 at 20:20
  • since you already accepted an answer to this question. I suggest you create a new question, including these details you're telling me now, some code samples, and a specific detailed description of what the issue is and what are your expected results. – Federico Berasategui Dec 05 '13 at 20:22
  • You will be the first to know, HighCore :-) – Travis Banger Dec 05 '13 at 20:27

2 Answers2

1

DataGrid Unload is usually use to indicate when a row is visually unloaded for reuse. Since the row is removed from the visual system the row index it can be invalid.

If you need to access the row data use e.Row.Item. You can use that to get the data index. See the following solution presented in this answer. https://stackoverflow.com/a/11295302/2696641

Community
  • 1
  • 1
  • "the row index it can be invalid." No, that is not exactly the case. Re-read my OP. The index is **guaranteed** to be invalid: it is always -1. – Travis Banger Dec 05 '13 at 20:25
0

Each control is based off of the control class which has a Tag property of type object. That property is exclusively for any consumer to place any object on it as needed and extract that object for later use. I recommend that you place the index on the property (or maybe a reference) to the data you are actually interested in at anytime before the unload occurs on the row. Then when the unload occurs, you simply extract the data from it.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • 1
    Using the `Tag` property to store data shows a **complete lack** of a proper UI architecture. It is simply a horrible hack that is acceptable only in frameworks that do not support proper patterns, such as winforms. In WPF you define a proper Data Model and a proper ViewModel and use DataBinding to bind the UI to the Data, instead of resorting to such horrendous hacks as the `Tag` property. – Federico Berasategui Dec 05 '13 at 17:56
  • @HighCore I have used the Tag property on Winforms, WPF, Silverlight and WP8 projects. I use it with MVVM and MVM. I think it is foolish not to use the resources provided by the designers. The Tag allows for quick access to any references without using system resources to try to divine what it was created from. If one chooses to handicap software by not using all things available to one; I find that a greater problem. – ΩmegaMan Dec 05 '13 at 18:01
  • there is clearly an XY problem here, as stated in my first comment. Your solution suggests to add even more horrible hacks to an already hacky UI manipulation in procedural code, which is completely unneeded when proper DataBinding is in place. My point still stands, let the OP edit the question and clarify his intent and I'll provide a proper answer. – Federico Berasategui Dec 05 '13 at 18:06
  • 1
    @HighCore Using the Tag property is not considered acceptable in winforms either by any developer worth their salt. It _may_ be acceptable in a vb6 application, depending on the exact situation. – Yandros Dec 05 '13 at 18:09
  • @HighCore: Your tip is good to know, and I will keep it in mind. The problem is that I wasn't sure *when* my handler was being called: before of after the fact (the name "Unloading" can be confusing, I would have used `Unloaded`). As luck would have it, the first field of my row happens to contain the row number (all I want to do with the event is renumber that particular field in the surviving rows), so I can use that. Thanks! – Travis Banger Dec 05 '13 at 18:56
  • @TravisBanger put that value in your Data Items, not the UI. the UI is not responsible for enumerating your Data Rows. – Federico Berasategui Dec 05 '13 at 18:58
  • @TravisBanger please edit your question and add more detail about what you're trying to do, so I can give you a proper answer. – Federico Berasategui Dec 05 '13 at 18:58
  • I decided to do something unusual. This question does not have a real answer (I need to go back to the drawing board and rethink my UI) but the last (in another thread) response from HighCore was so **extremely** helpful that he deserves 10x the points of one question. So this question goes to him (he saved my bottom and restored my mental sanity). – Travis Banger Dec 05 '13 at 19:42