Wednesday 20 March 2013

Using DataKey property in DataGrid

The DataKey property is used to identify the primary key column in a DataGrid. The property will then be used to uniquely identify each row in the DataGrid. The value specified here must be a column in the database.

Here's some tips and tricks on how to use this property.

Using DataKey to identify visited links

View this post to know more about the feature.

Using DataKey to automatically update data

Setup a DataGrid and add all columns:



Scenario 1 : Update MaritalStatus in the DataGrid (without DataKey)

  • Specify an UPDATE statement in the DataGrid and assign all the parameters

Scenario 2: Update Marital status in the DataGrid (with DataKey)

  • Specify a DataKey value

  • Specify an UPDATE statement in the DataGrid and

  • Declare all the parameters
(Notice that I do not have to assign any value to the parameter which is present in DataKey)



In both scenarios, the data will be updated when the Save button is clicked.

In Scenario 2, since the row is identified using a DataKey (employeeid), the parameter in UPDATE statement will be automatically assigned to the DataKey provided that they both have the same name.

Tip 1 : Ensure that the case matches. DataKey value ‘Employeeid’ will not match Parameter ‘EmployeeID’

Tip 2 : Stadium does not allow spacing in Parameter name. Therefore, Scenario 2 will not work if there is spacing in DataKey/database column names.

Tip 3 : Only 1 value is allowed for DataKey

Tip 4 : If you need to use same DataKey across different DataGrids, you can use it as a CustomSetting

More information on DataKey at :

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeys.aspx

Wednesday 13 March 2013

SelectedRow vs DataRow

This post describes the difference between SelectedRow and DataRow properties in a DataGrid.

Here are some examples used to demonstrate the difference between the 2 properties :

I have a DataGrid as displayed below:

Scenario 1: SelectedRow
  • This property is used to reference a selected DataGrid row.
  • SelectedRow value is passed through if a user clicks on the row, which means that the SelectedRow property will not work unless it is assigned within a click event (such as Button click or Link click)
Example:
  1. Select a row in DataGrid.
  2. Click ‘Display Selected Row’ button.
  3. You will notice that the label has been populated with the SelectedRow.EmployeeID value.
For this example, the SelectedRow value is assigned within the Button Click event :

Scenario 2: DataRow
  • This property is used to reference a Text column in the current row of a DataGrid control.
  • The DataRow property is available from within the DataGrid’s dropdown column.
  • Since the value can be populated without a user action, it does not need to be assigned AND is unavailable within a click event.
Example :
  1. Add a Dropdown column in the DataGrid called ‘DataRow Value’
  2. Set the DataSource value as displayed. (Notice that the parameter is assigned to DataRow.EmployeeID)
You will notice that when the page loads, the ‘DataRow Value’ column will be populated with the Employee ID value for the current row.