Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi can you help me because I'm a li

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi can you help me because I'm a little lost

I don't quite understand DataKeys collection.For example:
DataKeyField="X";
Does 'X' represents a property of objects from some data source collection with which data grid populates DataKeys collection for each object in a data source,or is it something else?Also why do you use item's id as an index into DataKeys collection?

I have one more question,if I can?How do you select a row in a Data Grid?


Maybe the solution is right before my nose,but till than I hope I can get some help from you

Thanks a lot
 
The following is extracted from Stephen Walther's ASP.NET Unleased (Sams Publishing, 2001):

Both the DataList and DataGrid controls have a special collection named DataKeys. This collection represents the primary key field associated with each item in the control.

Imagine, e.g., that you have a database table named Products that you want to display and edit with a DataGrid control. Imagine, furthermore, that the Products table has an identity column named ProductID.

You can associate the value of the identity column with each item in teh DataGrid by assigning the name of the identity column to the DataKeyField property of hte DataGrid like this:

<asp:DataGrid
DataKeyField=&quot;ProductID&quot;
Runat=&quot;server&quot;
/>

The advantage of associating the identity column with each item in the DataGrid is that you can retrieve the value of this column when you edit a field in the DataGrid. If you update a field in the DataGrid and want to update the corresponding field in the underlying edatabase table, you can use the DataKeys collection to uniquely indentify the correct row in the database table to update...&quot;

*****

To set up a hyperlink:

<asp:HyperLinkColumn
DataNavigateUrlField=&quot;ProductID&quot;
DataNavigateUrlFormatString=&quot;MyPage.aspx?MyID={0}&quot;
Text=&quot;Select&quot;
/>

...hope this helps.
 
I somehow understand,but

DataNavigateUrlField=&quot;ProductID&quot;
DataNavigateUrlFormatString=
=&quot;MyPage.aspx?MyID={0}&quot;

The value of ProductID will be written where {0} is even if we don't use DataKeyField=&quot;ProductID&quot; attribute.I'm sorry,but I don't any benefits from having DataKeys

Thanx
 
visi10: I think the general argument here is that the DataKeys collection (which is by definition a collection of the values, in table order, of the Primary key values from a table that is bound to a grid or datalist) finds itself useful in coding events in which the value of the Primary key is required for updating, or further querying.

For example, consider a web page with a datalist on one side, a repeater on the other, and the Primary Key field is used by the repeater in its SQL to populate. Here is an example of part of the code that would be used...

Sub dlstCategories_ItemCommand(s...
Dim intID As Integer
dlstCategories.DelectedIndex = e.Item.ItemIndex
BindDataList
intID=dlstCategories.DataKeys(e.Item.ItemIndex)
BindRepeater(intID)
End Sub

Having a specialized &quot;array&quot; of Primary Key values set aside in the DataKeys Collection seems like, at least, to be an &quot;efficient&quot; way to process ID requiring events.

Take the above example; if you did not use the DataKeyField, how would you accomplish it? And in comparison, are there any advantages of one over the other?
 
Sorry,but I didn't start learning that yet.
But can you tell me how would you use the DataKeys collection to uniquely indentify the correct row in the database table to update.Does datagrid
compare each individual value from datakeys collection
with the value of same property in each row?What if
DataKeys has two fields with the same value?

Thanx
 
vis10: I think you're right on target here. The DataKeys collection is a collection of Primary Field values, therefore, unique, and can be used directly from the DataKeys collection to identify a record in SQL for editing, updating or general querying.

Here's an example of an aspx page where items are edited, and the updated, in a DataGrid using the DataKeyField. An EditCommandColumn is added to the datagrid and the Update and Cancel command events introduced.

The Grid conists of ProductID (Key field), ProductName, Price and the EditCommandColumn which begins with a hyperlink &quot;Edit&quot; heading, changing to Update & Cancel when Edit is selected.

<%@ Import Namespace=&quot;System.Data.SqlCient&quot;%>
<Script Runat=&quot;Server&quot;>

Dim conNorthwind As SqlConnection
Dim cmdSql As SqlCommand
Dim strSql As String

Sub Page_Load
...connection string
If No IsPostBack Then
BindDataGrid
End If
End Sub

Sub BindDataGrid
cmdSql = New SqlCommand(&quot;Select * From Product&quot;, conNorthwind)
conNorthwind.Open()
dgrdProducts.DataSource=cmdSql.ExecuteReader()
dgrdProducts.DataBind()
conNorthwind.Close()
End Sub

Sub dgrdProducts_EditComman d(s As Object, e As DataGridCommandEventArgs)
dgrdProducts.EditItemIndex = e.Item.ItemIndex
BindDataGrid
End Sub

Sub dgrdProducts_UpdateCommand(s As Object, e As DataGridCommandEventArgs)
Dim intProductID As Integer
Dim txtProductName As Textbox
Dim txtUnitPrice As TextBox
Dim StrPRoductName As STring
Dim decUnitPrice As Decimal

intProductID = dgrdProducts.DataKeys(e.Item.ItemIndex)
txtProductName = e.Item.Cells(1).Controls(0)
txtUnitPrice = e.Item.Cells(2).Controls(0)
strProductName = txtProductName.Text
decUnitPrice = txtUnitPrice.Text
strSql = &quot;Update Products Set ProcuctName=@ProductName, &quot;_
& &quot;UnitePrice=@UnitPrice Where ProductID=@ProductID&quot;
cmdSql=New SqlCommand(strSql, conNorthwind)
cmdSql.Parameters.Add(&quot;@ProductName&quot;, strProductName)
cmdSql.Parameters.Add(&quot;@UnitPrice&quot;, decUnitPrice)
cmdSql.Parameters.Add(&quot;@ProductID, inProductID)
conNorthwind.Open()
cmdSql.ExecuteNonQuery()
conNorthwind.Close()
dgrdProducts.EditItemIndex = -1
BindDataGrid
End Sub

Sub dgrdProducts_CancelCommand(s As Objecdt, e As DataGridCommandEventArgs)
dgrdProducts.EditItemIndex = -1
BindDataGrid
End Sub

</script>

<html>
<head><title>DataGridEditProducts.aspx</title></head>
<body>
<form Runat=&quot;server&quot;>
<asp:DataGrid
ID=&quot;dgrdProducts&quot;
OnEditCommand=&quot;dgrdProducts_EditCommand&quot;
OnUpdateCommand=&quot;dgrdProducts_UpdateCommand&quot;
OnCancelCommand=&quot;dgrdProducts_CancelCommand&quot;
DataKeyField = &quot;ProductID&quot;
...
Runat=&quot;Server&quot;>
<Columns>
<asp:BoundColumn
HeaderText=&quot;Product ID&quot;
DataField=&quot;ProductID&quot;
/>
<asp:BoundColumn
HeaderText=&quot;Product Name&quot;
DataField=&quot;ProductName&quot;
/>
<asp:BoundColumn
HeaderText=&quot;Price&quot;
DataField=&quot;UnitPrice&quot;
DataFormatString=&quot;{0:c}&quot; />
<asp:EditCommandColumn
EditText = &quot;Edit!&quot;
UpdateText = &quot;Update!&quot;
CancelText = &quot;Cancel!&quot;
</Columns>
</asp:DataGrid>
</form>
</body>
</html>

...so a good set of questions. I'l like everyone else here, learning -- so I think the DataKeysCollection, it seems, is a useful tool when a Primary Key field is involved in databinding -- no telling how much you can accomplish with it. And to answer your question, yes, the &quot;value&quot; of the Primary Key field is what is being checked against the rows bound to the grid or list.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top