By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,313 Members | 2,725 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,313 IT Pros & Developers. It's quick & easy.

Datagrid selectedindex from datakey value

P: n/a
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

Nov 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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
"Joel Reinford" <jr********@comcast.net> wrote in message
news:9J********************@comcast.com...
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

Nov 19 '05 #2

P: n/a
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. Justin Gengo" <justin@[no_spam_please]aboutfortunate.com> wrote in
message news:%2****************@TK2MSFTNGP10.phx.gbl...
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
"Joel Reinford" <jr********@comcast.net> wrote in message
news:9J********************@comcast.com...
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


Nov 19 '05 #3

P: n/a
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
"Joel Reinford" <jr********@comcast.net> wrote in message
news:Ga********************@comcast.com...
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. Justin Gengo" <justin@[no_spam_please]aboutfortunate.com> wrote in
message news:%2****************@TK2MSFTNGP10.phx.gbl...
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
"Joel Reinford" <jr********@comcast.net> wrote in message
news:9J********************@comcast.com...
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



Nov 19 '05 #4

P: n/a
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. Justin Gengo" <justin@[no_spam_please]aboutfortunate.com> wrote in
message news:e3**************@tk2msftngp13.phx.gbl...
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
"Joel Reinford" <jr********@comcast.net> wrote in message
news:Ga********************@comcast.com...
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. Justin Gengo" <justin@[no_spam_please]aboutfortunate.com> wrote in
message news:%2****************@TK2MSFTNGP10.phx.gbl...
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
"Joel Reinford" <jr********@comcast.net> wrote in message
news:9J********************@comcast.com...
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




Nov 19 '05 #5

P: n/a
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
"S. Justin Gengo" <justin@[no_spam_please]aboutfortunate.com> wrote in
message news:e3**************@tk2msftngp13.phx.gbl...
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
"Joel Reinford" <jr********@comcast.net> wrote in message
news:Ga********************@comcast.com...
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. Justin Gengo" <justin@[no_spam_please]aboutfortunate.com> wrote in
message news:%2****************@TK2MSFTNGP10.phx.gbl...
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
"Joel Reinford" <jr********@comcast.net> wrote in message
news:9J********************@comcast.com...
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




Nov 19 '05 #6

P: n/a
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;
}
}

}

Nov 19 '05 #7

P: n/a
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
<ne**********@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
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;
}
}

}

Nov 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.