Hi,
I have an interesting scenario. I have a wingrid that displays records based on a search by the user with outlook groupby feature enabled. From the grid on double clicking a row i'm showing the details of the record in a windows form. I'm opening as a new form and not as a show dialog.
When the user double clicks the same row anytime.,i should not create another instance of the form, but i have to show the already opened instance. So if the instance is available i have to open the instance else i have to create a new instance.
To do this., i declared array of objects and resized the array with the row count of the grid. This is where the problem starts. I would pass the row index and open the instances like this.,
Array.Resize(ref obj, Grid.Rows.Count)
rowID = e.Row.Index;
obj[rowId] = new frmDetailsForm();
if(obj[rowID].IsDisposed !=true)
and show the existing window opened, else create a new instance.
Now., when there is a groupby in the grid, the row count is only groupby row count and i tend to miss the actual row count in the grid. My questions are,
1. How to track the available instances? (Might not be related to Ultragrid, still wanna clarify myself)
2. How to get the row index after the groupby is performed?
Thanks.,
Another similar option may be to use the row's ListIndex property, instead of its Index. The ListIndex property reflects the position of the row as it exists in the underlying data source, rather than as it exists in the grid row's containing collection. The benefit is that ListIndex is not updated when you sort or group, while Index is.
If you want to use ListIndex in this fashion, I'd recommend using a Dictionary<int,frmDetailsForm> object, instead of an array. Your key (an int) would store the ListIndex of the corresponding grid row, while your value (an instance of frmDetailsForm) would be the instance of your form. This way, you avoid needing to resize an array each time you add items, meaning that you no longer have to concern yourself about the grid's Rows.Count changing when you group/ungroup the grid. This also gives you a way to more easily track available instances, such as by iterating over the dictionary using a foreach loop.
Sorry for the late reply..
The scenario is that i have a search grid that displays the results. I have add screen (a new form with a grid in it) where users can add new items and upon saving i'm refreshing the search grid to display the newly added item.
User can double click the serach grid row to open the details of the item.One of my test cases is to open the item details and if they try to open the already existing item from the grid., i should NOT create a new instance, rather i should show the existing instance. This is where i used the dictionary to store the listindex and form isntance.
Now the question is, as i reload the search results., how can i get the list index of the newly added item?
Phew., pretty long., but i'm sure it is clear.
visiwanth,
If you mean an item that exists in your Dictionary<int,frmDetailsForm>, then you can use the ContainsKey method of the dictionary (link leads to MSDN), passing in the ListIndex property of the row you double-clicked. If this returns false, you need to create a new instance and add it to your dictionary, using the ListIndex as the key. If this returns true, you can pass the ListIndext to the dictionary's indexer (such as "myDictionary[rowListIndex]" to retrieve the instance of your form related to that key.
Yeah, i'm using the containskey to find the instance that is already available. The catch for me is., from the search form i'm loading the add new item form. On successful save of a new item., i'm handling the search button click event from the add new item form to refresh the search results and i'll keep that instance open for the user to update the item details. This becomes one instance for the item details.
Now they come back to the search form and click the newly added row in the grid, which will not be there in the dictionary, so it opens another new instance of the same item details form. This is what i have to avoid.
Any help is appreciated.
Sorry for the delayed reply.. Yes., Mike, i added in the dictionary the instance i'm creating and now i got the way it should work
Thanks,.
I'm afraid I'm not following you.
If you add a new item and add that item into the search results and you want to use the same form that the user already used to create the new item, then you have to add the new grid row and the new form into the dictionary.