Problems with Gridview and ObjectContainerDataSource

Topics: Web Client Software Factory
Jun 22, 2007 at 3:25 PM
Hi All,

I am trying to use the ObjectContainerDataSource and Gridview and the Edit Event to permit my user to edit some columns of the gridview.
Well, when the user click in update button, inside de gridview to finalize the edition is called the method below:

protected override int ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues)
{
*Guard.CollectionNotNullNorEmpty(keys, String.Format(CultureInfo.CurrentCulture, Properties.Resources.NoKeysSpecified), "keys");
Guard.ArgumentNotNull(values, "values");*

ObjectContainerDataSourceUpdatingEventArgs updatingEventArgs =
new ObjectContainerDataSourceUpdatingEventArgs(DictionaryHelper.GetReadOnlyDictionary(keys), values, oldValues);
OnUpdating(updatingEventArgs);
if (updatingEventArgs.Cancel)
return 0;

object newInstance = CreateInstance();
TypeDescriptionHelper.BuildInstance(keys, newInstance);
TypeDescriptionHelper.BuildInstance(values, newInstance);
int rowsAffected;
object oldInstance = FindInstance(keys);
if (oldInstance != null)
{
int index = Data.IndexOf(oldInstance);
Dataindex = newInstance;
rowsAffected = 1;
}
else
{
rowsAffected = 0;
}
OnDataSourceViewChanged(EventArgs.Empty);

ObjectContainerDataSourceStatusEventArgs updatedEventArgs = new ObjectContainerDataSourceStatusEventArgs(newInstance, rowsAffected);
OnUpdated(updatedEventArgs);

return rowsAffected;
}

That method calls another one below:

public static void CollectionNotNullNorEmpty(ICollection collection, string message, string argumentName)
{
if (collection == null)
throw new ArgumentNullException(argumentName);

  • if (collection.Count == 0)
throw new ArgumentException(message, argumentName); *
}

In this last method I receive an exception cause there are no items in the collection. I really don´t know how to resolve that. Tried almost everything I know, but with no success.

Please, its a simple thing I need to do, that is to update an only one column in gridview and after that, I finish the process clicking a button to update those datas into database.

Thanks for any support.
Jun 23, 2007 at 2:38 AM
Sounds like you need to set the DataKeyNames Property of the GridView Control to the properties that represent the Primary Key of your object.

Regards,

Dave

____________________________

David Hayden
Microsoft MVP C#
Jun 25, 2007 at 3:31 PM
Edited Jun 25, 2007 at 3:31 PM
Hi David,

You were Perfect !!! You found the answer... Thank you very much !!!

regards,
Marcelo.


DavidHayden wrote:
Sounds like you need to set the DataKeyNames Property of the GridView Control to the properties that represent the Primary Key of your object.

Regards,

Dave

____________________________

David Hayden
Microsoft MVP C#