Datagrid selectedindex from datakey value

J

Joel Reinford

I am looking for a way to set the selectedindex of a datagrid based on the
datakey value.

For example given this grid with a datakey of OrderID:

OrderID Item
54 A
98 B
102 C
263 A


If I have a value of 102, I'd like to have selectedindex = 2. The usage for
this is for selecting a specific row in the datagrid after adding/deleting
rows.

Joel Reinford
Data Management Solutions LLC
 
S

S. Justin Gengo

Joel,

I have some sample code on my website, www.aboutfortunate.com, that shows
how to make a row in a datagrid clickable. If you click the "Code Library"
link at the top of the page and then use the search box that will appear to
search for "make row clickable" the code will give you some pointers.

If the sample code doesn't answer all your questions feel free to email me.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
J

Joel Reinford

Justin:

Thanks for the reply but I don't see that this has anything to do with my
question. I can select the row just fine from the UI just fine already. I
can also get the datakey value based on the selectedindex value. What I want
to do is reverse that process.

Joel Reinford
Data Management Solutions LLC
 
S

S. Justin Gengo

Joel,

Did you look at the example? There is some javascript in there inside of the
OnItemDatabound that selects the row clientside.

If you use that same script in a Page.RegisterStartupScript you'd have what
you need.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
J

Joel Reinford

Justin:

Yes, I looked at the example but it has nothing to do with my question. I am
looking to set the selectedindex based on a given datakey value. This
question is about VB.NET/C# code, not javascript and not UI.


Joel Reinford
Data Management Solutions LLC
 
S

S. Justin Gengo

Joel,

Hold off on my previous answer, thinking about this there is no reason for
you to do this clientside.

Using the On_ItemDatabound event will be important though.

What you need to do is create a counter to use within On_ItemDatabound and
check for the row's id (If you don't want the id column to show then set it
to not be visible). Then, as your grid is bound, check the id to see if the
current item being bound is the row you want and select it.

So, for example, using a hidden ID you could write your code like this:

'---Dim a variable set to zero each time to track the current index within
the datbound event.
Private CurrentIndex As Int32 = 0

Private Sub DataGrid1_ItemDataBound(ByVal sender As System.Object, ByVal e
As System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
Try
Dim itemType As ListItemType = e.Item.ItemType
If ((itemType = ListItemType.Pager) Or (itemType =
ListItemType.Header) Or (itemType = ListItemType.Footer)) Then
Return
Else
CurrentIndex += 1
If e.Item.Cells(0).Text = "[Your ID Here]" Then
DataGrid1.SelectedIndex = CurrentIndex
End If
End If
Catch ex As Exception
'---Handle Exception
End Try
End Sub


--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
N

neilmcguigan

here's a cleaner way to do it (C#)"

void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs
e)
{
int target = 1; //the orderId you want to select
//assume set DataGrid1.DataKeyField = "OrderId"; somewhere

if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
DataGrid grid = (DataGrid) sender;
int orderId = (int) grid.DataKeys[e.Item.ItemIndex];
if(orderId == target)
{
grid.SelectedIndex = e.Item.ItemIndex;
}
}

}
 
S

S. Justin Gengo

Neil,

Very nice. I had forgotten that e.Item.ItemIndex existed.

The same would work in the VB example.

Thanks,

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top