Using linq on an OrderedDictionary to get max using a GridView

I have a custom GridView control (inherits from system.web.ui.webcontrols.gridview) and I would like to control the TextBox value based on values โ€‹โ€‹recently inserted by the user (the inserted values โ€‹โ€‹are not bound to the data source).

In Grid.DataBound, I can query the cells of one column to get the current maximum that I set in the TextBox.

int maxTimeSlot = grid.Rows.OfType<GridViewRow>().Max(r => int.Parse(r.Cells[1].Text)); 

After entering a new row, I have access to the "Insert" property of the Grid, which contains a list of all inserted rows so far, for example:

 GridView.Inserts[0].NewValues & GridView.Inserts[0].OldValues 

What I need to do is use this Insert list to get the maximum value again, but I'm trying to query NewValues.Values โ€‹โ€‹(OrderedDictionaryKeyValueCollection) to get the value.

 NewValues.Values.OfType<KeyValuePair<object, object>>().Select(r => r.Key == "timeslot").Max(t => (int)t.Value) 

Above was my "idea" of how it will work, but, of course, this does not happen.

+4
source share
1 answer

The call for selection projects each KeyValuePair in NewValues.Values onto a logical one, so you no longer have access to the source data.

It looks like you are trying to find all the values โ€‹โ€‹of the โ€œtime intervalโ€, I think you need something like

 NewValues.Values.OfType<KeyValuePair<object, object>>().Where(r => r.Key == "timeslot").Max(t => (int)t.Value) 
+3
source

All Articles