GridView delete not working - c#

I'm using a GridView in C#.NET 3.5 and have just converted the underlying DataSource from Adapter model to an object which gets its data from LINQ to SQL - i.e. a Business object that returns a List<> for the GetData() function etc.
All was well in Denmark and the Update, and conditional Select statements work as expected but I can't get the Delete function to work. Just trying to pass in the ID or the entire object but it's being passed in a "new" object with none of the properties set. I'm just wondering if it's the old OldValuesParameterFormatString="original_{0}" monster in the ObjectDataSource causing confusion again.
Anybody have any ideas?

I found the solution. I had to set the GridView's DataKeyNames property to the unique key that my data was returning (in this case a classically named ID field). I'm guessing that this property "unset" itself when the Grid refreshed.

Related

ViewModel data with dynamic rows

I have a view that list a collections of items. I allow the user to add and delete rows dynamically from the view. This all works fine. I am adding and deleting rows in the controller then sending the model back to the view,
return View("EditPage", Dmodel);
The problem is one of my fields uses a dropdown list,
<td>#Html.DropDownListFor( m=>m.Order[i].Software, Software())
After I remove a row from my collection and return to the view, the dropdown does not select the correct value for the row that took the place of the deleted row.
I noticed that these two model access methods are returning different values,
m=>m.Order[i].Software --This is wrong
Model.Order[i].Software -- This is correct
it looks like the lambda expression is looking at the old data, but Model is looking at what I passed to the view.
Any ideas on how to fix this?
Instead of returning view in POST method, use redirect, so instead of
return View("EditPage", Dmodel);
redirect
return RedirectToAction("EditPage",...);
This pattern is called POST-REDIRECT-GET. After successful operation you should redirect to GET.
Currently existing ModelState values conflict with new ones. You deleted values in model, but ModelState still holds them, so you have issues with indexing. You can fix it differently, but using this pattern is the best solution.

"Operation is not valid due to the current state of the object." when I attempt to unseal a field on a list

I have a few fields on a list that was created using a schema. The schema and list seem to have gotten out of sync, so when I try to change attributes for a field on the schema, those changes are not reflected on the list, unless they are additions (ie, new fields).
I have a few Sealed fields that I need to unseal. But when I attempt to set Sealed=false, either using SharePoint Manager, or using a utility I wrote, I get the error:
"Operation is not valid due to the current state of the object." I can't seem to find any more usable information about the error.
On my utility, this happens on the line:
field.Sealed = false;
I haven't even gotten to the point of trying to update the field before an exception is thrown. field.SchemaXml looks fine too. I don't see anything wrong with it.
This is happening on multiple fields in this list.
Anyone know what to do?
What is the error and what is the field that is failing?
According to MSDN, you cannot change out of the box or external data fields:
InvalidOperationException: An attempt is made to assign a value to a field type that is built into SharePoint. The true/false value of such fields must remain at its factory setting.
NotSupportedException: An attempt is made to assign false when the SharePoint field is an external data column.
Update (to highlight information that might be hidden in the comments):
Call SPBuiltInFieldId.Contains to avoid (or detect) InvalidOperationException. The Sealed property cannot be set if SPBuiltInFieldId.Contains returns true.
Following up on Rich's answer, if you need to modify a column that is in the SPBuiltInFieldId collection, you can set the GUID of the column to new GUID() during runtime then make any changes to the column you need. Be careful, there is a reason that Microsoft attempts to lock you out of editing these columns. I needed to fix the list relationship for the Resources (Facilities) column in my group calendar and this allowed me to do so.

saving second result set from a SelectMethod

We have a Gridview, which gets filled via a DataSource. The datasource's SelectMethod is a function in the BL class, which calls a Stored Procedure that returns 2 Result-sets. The first result set is supposed to fill the gridview, so it's returned via the "return" keyword, and that works fine. The second result set is for later use (we're not separating them into 2 SP's because the query applies the same logic to get both result sets, and it's a long logic, so we don't want to run it twice). Our question is how to save the second result-set for the later use.
We initially thought of creating a global member in the BL class, and saving the result-set to it. But the problem is that when we need to use the second result-set, we're not in the same class where the gridview is, and that other class creates an instance of its own of the BL class, and of course that object doesn't have the data (the data is in the object created by the aspx page in which the gridview resides).
So then we thought of passing an "out" parameter the datasource's SelectMethod, and the method would save the second result set to that out parameter. But that's not working. I think we don't know how to tell the method that a parameter is "out". The way we pass the other parameters is by defining a method that is attached to the OnSelecting event, and then we pass them so:
e.InputParameters["schoolCode"] = currentSchoolCode;
e.InputParameters["levelCode"] = currentLayer;
Etc. However, we don't know how to tell the method that a parameter is "out". When we googled a little, all we could find was how to tell the method if a parameter is an output parameter - that is, passed to the SP as an output parameter. But that is not the case here.
So we will be very grateful if you help us find the solution for:
"Telling" the method that a certain parameter is "out", in order to get the value into it.
thanks.
Hmm... not quite understand your concern, but, how about this? pass your dataset to presentation layer and then:
dataset.Tables(0)
dataset.Tables(1)
or
DataReader.NextResult()

DataForm, DropBox and data annotations (description and such)

I've got a DataForm in which I replace some TextBox fields with DropBoxes to imitate a lookup using the AutoGeneratingField event.
However, when I do that, I loose any data annotations that could otherwise be used (such as a description which I specified in my data model).
Is there a way to get these values from the data model without resorting to auto-generated fields? The DataFormAutoGeneratingFieldEventArgs parameter doesn't seem to contain them.
Set the DataField.PropertyPath property to the name of the model property with metadata.

How to bind a telerik grid column to a child data object that is a list?

I have a grid that binds a number of child data objects to columns with no issue, using the syntax defined at http://www.telerik.com/help/aspnet-ajax/grdbindingtosubobjects.html.
What I can't figure out, however, is how to aggregate a child object that is a list or collection. For example, if I have a Customer object and I want to get the customer's first street address, I would use DataField="Customer.Addresses[0].Street" on a standard GridBoundColumn.
How can I get the count of the addresses? I have tried all sorts of GridCalculatedColumn DataFields and Expressions, to no avail. I am looking for something along the lines of this:
That doesn't work, of course. In fact, if I try to do a Count on any dotted data field, I get an exception of
"System.Data.SyntaxErrorException: Syntax error in aggregate argument: Expecting a single column argument with possible 'Child' qualifier."
For example, just trying to use an expression of Count({0}) with DataFields set to Customer.FirstName (of which there is only one), causes that exception to be thrown at runtime. Doing the same thing with a non-dotted data field, such as SendDate, does not cause the same exception.
Seems like you already received an answer to this question from the Telerik forums but for people that might stumble upon this question looking for an answer.
Essentially the RadGrid does not support having Collections in its DataFields, and the supported bindable property types can be found here. So in terms of this aggregation you could either do a calculation across this collection before binding it to the RadGrid and have an aggregate column defined, or you could look into using footers much like in this demo.

Categories