Getting a value from a sorted datagrid in asp.net?

J

Jason

I've been trying to figure out a good way to do this but haven't had much
luck, any input would be greatly appreciated.

Basically, after a datagrid is sorted, how can I get the primary key value
of the item selected by the user? That is, not the datagrid index
'location' of the item the user selected, but the value inside a column of
this item the user clicked on.

I would use the CurrencyManager in a windows application but I'm not sure
what to use in a web form.

Thanks in advance,

Jason
 
C

Curt_C [MVP]

make sure to set a DataKeyField in the binding, then use
myValue = dataGrid1.DataKeys[this.dataGrid1.SelectedIndex].ToString();
 
J

Jason

Thanks for the reply Curt.

I've set a DataKeyField in the binding but when I click on a datagrid item
after a sort it still returns the wrong value. Does it matter that I'm
sorting using a dataview of the datagrid?


Curt_C said:
make sure to set a DataKeyField in the binding, then use
myValue = dataGrid1.DataKeys[this.dataGrid1.SelectedIndex].ToString();

--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com


Jason said:
I've been trying to figure out a good way to do this but haven't had much
luck, any input would be greatly appreciated.

Basically, after a datagrid is sorted, how can I get the primary key value
of the item selected by the user? That is, not the datagrid index
'location' of the item the user selected, but the value inside a column of
this item the user clicked on.

I would use the CurrencyManager in a windows application but I'm not sure
what to use in a web form.

Thanks in advance,

Jason
 
C

Curt_C [MVP]

I'm assuming you are doing something like this....

yourObj.View.Sort = this.dataGrid1.SortOrder;
this.dataGrid1.DataKeyField = "PrimaryKeyFieldName";
this.dataGrid1.DataSource = yourObj.View;
this.dataGrid1.DataBind();

?
--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com


Jason said:
Thanks for the reply Curt.

I've set a DataKeyField in the binding but when I click on a datagrid item
after a sort it still returns the wrong value. Does it matter that I'm
sorting using a dataview of the datagrid?


Curt_C said:
make sure to set a DataKeyField in the binding, then use
myValue = dataGrid1.DataKeys[this.dataGrid1.SelectedIndex].ToString();

--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com


Jason said:
I've been trying to figure out a good way to do this but haven't had much
luck, any input would be greatly appreciated.

Basically, after a datagrid is sorted, how can I get the primary key value
of the item selected by the user? That is, not the datagrid index
'location' of the item the user selected, but the value inside a
column
 
J

Jason

Close, here's my Sort Proecdure:

Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
DataGrid1.SortCommand
Dim dtParam As DataTable = DsChanges1.Tables(0)
dvwParam = dtParam.DefaultView
dvwParam.Sort = e.SortExpression
DataGrid1.DataSource = dvwParam
DataGrid1.DataBind()
End Sub

dvwParam is a dataview that binds to the above datatable
(dtparam.defaultview) as above. So, whenever I actually click on one of the
datagrid1's headhers to sort by that column it sorts just fine, but if I
click on a record, it gives the 'original' value (presorted record) not the
now-sorted-value (record).

The sub that's called when I click on a record is:

Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.EditCommand

'This should be the value of the cell(1) a.k.a. Primary key value
"Change_ID" in the datagrid
propChange_ID = CType(e.Item.Cells(1).Text, String)
'Pass this ID to the Details form
Server.Transfer("frmChange_Details.aspx")
End Sub

I hope this makes sense. It looks like although the dataview is getting
sorted (which makes it appear the datagrid is sorted as well) the underlying
data table (or whatever is underlying to a dataview/grid) isn't being sorted
which is what my 'DataGrid1_EditCommand' is pointing to.


Curt_C said:
I'm assuming you are doing something like this....

yourObj.View.Sort = this.dataGrid1.SortOrder;
this.dataGrid1.DataKeyField = "PrimaryKeyFieldName";
this.dataGrid1.DataSource = yourObj.View;
this.dataGrid1.DataBind();

?
--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com


Jason said:
Thanks for the reply Curt.

I've set a DataKeyField in the binding but when I click on a datagrid item
after a sort it still returns the wrong value. Does it matter that I'm
sorting using a dataview of the datagrid?


Curt_C said:
make sure to set a DataKeyField in the binding, then use
myValue = dataGrid1.DataKeys[this.dataGrid1.SelectedIndex].ToString();

--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com


I've been trying to figure out a good way to do this but haven't had much
luck, any input would be greatly appreciated.

Basically, after a datagrid is sorted, how can I get the primary key value
of the item selected by the user? That is, not the datagrid index
'location' of the item the user selected, but the value inside a
column
of
this item the user clicked on.

I would use the CurrencyManager in a windows application but I'm not sure
what to use in a web form.

Thanks in advance,

Jason
 
C

Curt_C [MVP]

not certain but try setting the sort expression prior to setting the source.

--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com


Jason said:
Close, here's my Sort Proecdure:

Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
DataGrid1.SortCommand
Dim dtParam As DataTable = DsChanges1.Tables(0)
dvwParam = dtParam.DefaultView
dvwParam.Sort = e.SortExpression
DataGrid1.DataSource = dvwParam
DataGrid1.DataBind()
End Sub

dvwParam is a dataview that binds to the above datatable
(dtparam.defaultview) as above. So, whenever I actually click on one of the
datagrid1's headhers to sort by that column it sorts just fine, but if I
click on a record, it gives the 'original' value (presorted record) not the
now-sorted-value (record).

The sub that's called when I click on a record is:

Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.EditCommand

'This should be the value of the cell(1) a.k.a. Primary key value
"Change_ID" in the datagrid
propChange_ID = CType(e.Item.Cells(1).Text, String)
'Pass this ID to the Details form
Server.Transfer("frmChange_Details.aspx")
End Sub

I hope this makes sense. It looks like although the dataview is getting
sorted (which makes it appear the datagrid is sorted as well) the underlying
data table (or whatever is underlying to a dataview/grid) isn't being sorted
which is what my 'DataGrid1_EditCommand' is pointing to.


Curt_C said:
I'm assuming you are doing something like this....

yourObj.View.Sort = this.dataGrid1.SortOrder;
this.dataGrid1.DataKeyField = "PrimaryKeyFieldName";
this.dataGrid1.DataSource = yourObj.View;
this.dataGrid1.DataBind();

?
--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com


Jason said:
Thanks for the reply Curt.

I've set a DataKeyField in the binding but when I click on a datagrid item
after a sort it still returns the wrong value. Does it matter that I'm
sorting using a dataview of the datagrid?


"Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message
make sure to set a DataKeyField in the binding, then use
myValue = dataGrid1.DataKeys[this.dataGrid1.SelectedIndex].ToString();

--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com


I've been trying to figure out a good way to do this but haven't had
much
luck, any input would be greatly appreciated.

Basically, after a datagrid is sorted, how can I get the primary key
value
of the item selected by the user? That is, not the datagrid index
'location' of the item the user selected, but the value inside a column
of
this item the user clicked on.

I would use the CurrencyManager in a windows application but I'm not
sure
what to use in a web form.

Thanks in advance,

Jason
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top