Updating Grid items in a List - c#

I have a list of Grid items called gridCache.
I have a TabItem called ti.
This line of code:
gridCache.Last().Name = ti.Name;
...is updating the Name property of every single Grid item in the list. It should just be updating the last item's Name property.
Why is it doing this?
Maybe I'm rubberducking here, but I've followed it through a break point while debugging and they all just update simultaneously when this line is called.
EDIT: I'd like to basically make 'copies' of flyGrid as it's modified to store them for later use. The idea is to use this to cache data from some SQL calls. Here's what I'm trying:
//some stuff that defines flyGrid
Grid cacheGrid = new Grid();
cacheGrid = flyGrid;
cacheGrid.Name = ti.Name;
gridCache.Add(cacheGrid);
After this recurs 3 or 4 times, the Name property of every item in the list is always the last name supplied.
How can I make a copy of flyGrid that is its own instance each time this code recurs?
SOLUTION EDIT:
I ended up solving the root problem in a completely different way. The idea was to get cached ReportParameterInfoCollection items to keep from talking to the database constantly.
I ended up creating a simple class for an object with two properties: one random string of letters, and a list of ReportParameterInfoCollection items.
This allowed me to populate this list as tabs are opened, and assign these tabs unique IDs that match the parameter information stored in this list of objects.
I didn't really solve the question, so I decided not to post this as an answer.

That would happen if every Grid is the same instance.
Make sure you're actually creating multiple instances when populating the list.

Related

C# Combobox - "deleted entities"?

It's a struggle with any application that provides select fields, that are populated by a certain datasource: Everything works fine in the first place, but once the application ages, some older entries might be deleted, leading to the problem that prior select fields can no longer access the entity in question.
Opening a view, where a select points to an already deleted datarow will (best case) show empty string.
We designed our system in a way, that deletions are not real delete-operations, but only the setting of a deleted flag. (So, all the information is still there)
However, when using Databindings along with C# (or even if not) the most blatant use case is still not covered by general mechanics (I assume):
Select-Field should show all NOT-Deleted-Entities while creating a new object containing references to the entity in question.
Select-Field (populated the very same way) should show the "deleted" entity, if it was selected "days/months/years" ago.
Is there a "handy" solution to this?
Currently we are using a "Proxy-Method" for every datasource, which will reload the data of the deleted entity, if it's not in the "available data" collection - but it's hard to believe there is no better way to deal with this, as this problem applies for almost every language out there?
In a normalized database you would have a constraint with ON DELETE NO ACTION/RESTRICT event that would prevent removal of a referenced element from the list. It would force you to decide what is to be done with the referencing rows.
With your manually-controlled deletions this could have been covered by a trigger. As none of these were implemented, you are left with only one thing to do: updating the dropdown with the selected option before rendering the UI. My approach (in Java, I'm not good at C#):
List<String> options = getNonDeletedWhatever();
if (!options.contains(currentEntity.getWhatever())) {
options.add(currentEntity.getWhatever()); // This optionally inserts an outdated value
}
or simply:
Set<String> options = getNonDeletedWhatever();
options.add(currentEntity.getWhatever()); // This optionally inserts an outdated value
I solve it by creating a list of available (non-deleted) items and if the selected item is a deleted one, then I add that item to the list.
This list becomes the data source for my dropdown.

Assigning a different ViewSource in WPF

I'm experimenting with C#/.net/WPF all for the first time. I've created a project and set up a datasource (just a table with some sample data) and created two tableadapters named Prods and Prods1 - the latter has a filter applied in the query to return slightly different results. I've dropped both tables on my form and both dutifully display their respective data.
I thought I would then swap the data source for each. So the default generated Window_Loaded:
MSDSTest.prodtestDataSet prodtestDataSet = ((MSDSTest.prodtestDataSet)(this.FindResource("prodtestDataSet")));
// Load data into the table Prods. You can modify this code as needed.
MSDSTest.prodtestDataSetTableAdapters.ProdsTableAdapter prodtestDataSetProdsTableAdapter = new MSDSTest.prodtestDataSetTableAdapters.ProdsTableAdapter();
prodtestDataSetProdsTableAdapter.Fill(prodtestDataSet.Prods);
System.Windows.Data.CollectionViewSource prodsViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("prodsViewSource")));
prodsViewSource.View.MoveCurrentToFirst();
// Load data into the table Prods1. You can modify this code as needed.
MSDSTest.prodtestDataSetTableAdapters.Prods1TableAdapter prodtestDataSetProds1TableAdapter = new MSDSTest.prodtestDataSetTableAdapters.Prods1TableAdapter();
prodtestDataSetProds1TableAdapter.Fill(prodtestDataSet.Prods1);
System.Windows.Data.CollectionViewSource prods1ViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("prods1ViewSource")));
prods1ViewSource.View.MoveCurrentToFirst();
I now want to make the first data grid (prodsViewSource) instead display the data for the second table, and ignore the second table entirely. So, I changed that as follows:
MSDSTest.prodtestDataSet prodtestDataSet = ((MSDSTest.prodtestDataSet)(this.FindResource("prodtestDataSet")));
// Load data into the table Prods. You can modify this code as needed.
MSDSTest.prodtestDataSetTableAdapters.Prods1TableAdapter prodtestDataSetProdsTableAdapter = new MSDSTest.prodtestDataSetTableAdapters.Prods1TableAdapter();
prodtestDataSetProdsTableAdapter.Fill(prodtestDataSet.Prods1);
System.Windows.Data.CollectionViewSource prodsViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("prodsViewSource")));
prodsViewSource.View.MoveCurrentToFirst();
With the second block having been commented out.
I must be missing something fundamental - what I think I'm doing is redefining the prodtestDataSetProdsTableAddapter variable to use an instance of the prods1 table adapter, and then using that to populate the prodsViewSource grid on the form, but I end up with a blank. Where's my error?
...
Well, I posted this after beating my head against it for an hour and, minutes later, realized the FAR easier thing to do is to just change the datacontext property of the grid in question.
I would still like to understand why doing it the vastly more complicated-bordering-on-Rube-Goldbergian way didn't work, though, so if anyone can explain that, it would still be welcome.

Not able to copy multiple SPFieldLookupValue from one list to another

I am having a scenario where I am required to copy the SharePoint list items from one list to another it includes various types of fields such as text, lookup and people group.
I am trying to copy the look-up field from one list to another using the following piece of code but what it does is it copies only the last value in the loop into the other list column but I want all the entire set items copied from one list to another.
foreach (SPFieldLookupValue value in values)
{
targetItem["Hiring_x0020_Manager"] = new SPFieldLookupValue(value.ToString());
}
Is there anyway to copy the entire set of items from one list to another separately.
Also is it possible to copy SP Column type of people and look-up from one list to another using a similar code snippet.
…but what it does is it copies only the last value in the loop…
You keep overwriting the value of the destination field. Hence yes, it behaves like that.
For lookup fields that have multiple values enabled, you have to construct a new SPFieldLookupValueCollection instance and assign it to the destination field. I assume your values variable is exactly of this type. There is an extensive example in MSDN.

Creating a list of dataset fields and form fields for easy updating

I have a relatively complex dataset (numerous tables, some with multiple records) generated from reading in an XML file. I need to connect said dataset fields to an ASP form. At the moment I'm assigning them manually, like so in the pageload:
txt_first_init.Text = formData.ds.Tables["phrmHdrKey"].Rows[0]["first_init"].ToString();
txt_last_name.Text = formData.ds.Tables["phrmHdrKey"].Rows[0]["last_name"].ToString();
ddl_pregnancy_flag.SelectedValue = formData.ds.Tables["pPhrm"].Rows[0]["pregnancy_flag"].ToString();
And conversely when it's time to submit.
formData.ds.Tables["phrmHdrKey"].Rows[0]["first_init"] = txt_first_init.Text;
formData.ds.Tables["phrmHdrKey"].Rows[0]["last_name"] = txt_last_name.Text;
formData.ds.Tables["pPhrm"].Rows[0]["pregnancy_flag"] = ddl_pregnancy_flag.SelectedValue.ToString();
I did some looking into binding the textboxes (and dropdownlists, and checkboxes, and and and...) directly, but it seemed to be too many formats to use.
So this works fine, but as the number of fields increases, those load and unload lists are going to get unwieldy.
Trying to come up with a way to make a future update or addition so said list neater. I've gotten a very hand-wavy suggestion to place the two columns of names into a list, but unsure how to set such up, and how to load the items into a function that could evaluate and run the resulting commands.
Thoughts?
see here: Dynamic Form Generation in ASP.NET
looks like MS Dynamic Data is the answer.

Array returning same values for all indexes

I created an object called Participant.
Now I want to have an array of my Participant objects so that I could show them in a datagrid.
Here are the codes I tried (for better understanding of the problem, I removed the loops and datagrid codes):
Participant[] list = new Participant[count];
Participant one = new Participant(name, address);
Participant two = new Participant(name2, address2);
list[0] = one;
list[1] = two;
However, when I get values of one participant like through a messagebox in this manner,
MessageBox.Show(list[0].getName());
all it reflects are the data of participant two. Same is true if I have 3 objects, all it reflects is the data last sent into the array.
I know it is possible to have array of objects so there must be something I'm doing wrong. Or is there a better way to do this?
With the code as presented, the only way I can think of causing that is if the backig field (in Participant) was declared "static". If so, remove the "static".
Otherwise; does the actual code do a "new" for the two objects? Or does it overwrite an object after adding it to the array? (which means you have the same object twice in the array).
I would expect ReferenceEquals(list[0], list[1]) to be false in a sane world - can you test this and let us know?
Final thought; is there a "foreach" in the real code? It could be the infamous captured variable problem...
If you are using a loop to populate your array, be sure you use the loop index as the index of the array when you assign the paticipant.
Debugging is an underrated skill... Set a breakpoint on the line where you create the array and add list as a watch, and expand it so you see the contents. Step through your code and hover over the parameters as you're creating the Participants to see the values being passed. Step over the statemens that add them to the list and verify in your watch that the correct item and values are in the array each time and that the existing values haven't changed.

Categories

Resources