C# – According to Msdn GridView.DataKeyNames must be set if we want to update database

ado.netasp.netcgridview

Use the DataKeyNames property to specify the field or fields that represent the primary key of the data source. You must set the DataKeyNames property in order for the automatic update and delete features of the GridView control to work.

a) According to above quote Update should only be possible if DataKeyNames is set, and yet I was able to update database even though I don’t have DataKeyNames set.

b) Or does the term automatic update mean something else?

Thank you

Best Answer

A GridView or a Formview can support automatic updates, only if it is used with some kind of a properly configured *Datasource (ObjectDataSource, SqlDataSource etc.). For delete/update operation, a unique key is required, hence the DataKeyNames collection. If the DataSource is for example a Strongly typed dataset, it is working automatically out of the box.