473,769 Members | 3,350 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Datagrid selectedindex from datakey value

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
7 3584
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********@com cast.net> wrote in message
news:9J******** ************@co mcast.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
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******** ********@TK2MSF TNGP10.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********@com cast.net> wrote in message
news:9J******** ************@co mcast.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
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.RegisterSt artupScript 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********@com cast.net> wrote in message
news:Ga******** ************@co mcast.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******** ********@TK2MSF TNGP10.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********@com cast.net> wrote in message
news:9J******** ************@co mcast.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
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******** ******@tk2msftn gp13.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.RegisterSt artupScript 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********@com cast.net> wrote in message
news:Ga******** ************@co mcast.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******** ********@TK2MSF TNGP10.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********@com cast.net> wrote in message
news:9J******** ************@co mcast.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
Joel,

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

Using the On_ItemDataboun d event will be important though.

What you need to do is create a counter to use within On_ItemDataboun d 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_ItemD ataBound(ByVal sender As System.Object, ByVal e
As System.Web.UI.W ebControls.Data GridItemEventAr gs) Handles
DataGrid1.ItemD ataBound
Try
Dim itemType As ListItemType = e.Item.ItemType
If ((itemType = ListItemType.Pa ger) Or (itemType =
ListItemType.He ader) Or (itemType = ListItemType.Fo oter)) Then
Return
Else
CurrentIndex += 1
If e.Item.Cells(0) .Text = "[Your ID Here]" Then
DataGrid1.Selec tedIndex = 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******** ******@tk2msftn gp13.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.RegisterSt artupScript 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********@com cast.net> wrote in message
news:Ga******** ************@co mcast.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******** ********@TK2MSF TNGP10.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********@com cast.net> wrote in message
news:9J******** ************@co mcast.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
here's a cleaner way to do it (C#)"

void DataGrid1_ItemD ataBound(object sender, DataGridItemEve ntArgs
e)
{
int target = 1; //the orderId you want to select
//assume set DataGrid1.DataK eyField = "OrderId"; somewhere

if(e.Item.ItemT ype == ListItemType.It em || e.Item.ItemType ==
ListItemType.Al ternatingItem)
{
DataGrid grid = (DataGrid) sender;
int orderId = (int) grid.DataKeys[e.Item.ItemInde x];
if(orderId == target)
{
grid.SelectedIn dex = e.Item.ItemInde x;
}
}

}

Nov 19 '05 #7
Neil,

Very nice. I had forgotten that e.Item.ItemInde x 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**********@g mail.com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.com...
here's a cleaner way to do it (C#)"

void DataGrid1_ItemD ataBound(object sender, DataGridItemEve ntArgs
e)
{
int target = 1; //the orderId you want to select
//assume set DataGrid1.DataK eyField = "OrderId"; somewhere

if(e.Item.ItemT ype == ListItemType.It em || e.Item.ItemType ==
ListItemType.Al ternatingItem)
{
DataGrid grid = (DataGrid) sender;
int orderId = (int) grid.DataKeys[e.Item.ItemInde x];
if(orderId == target)
{
grid.SelectedIn dex = e.Item.ItemInde x;
}
}

}

Nov 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1148
by: Jeff Reed | last post by:
I have this in my coding for a DataGrid: <EditItemTemplate> <asp:DropDownList id="eCategoryList" runat="server" CssClass="selectBoxSmall" SelectedIndex='<%# DataBinder.Eval(Container.DataItem), "Category_FK"%>' DataValueField="Category_PK" DataTextField="Category" DataSource="<%#eCATdv%>"> </asp:DropDownList> </EditItemTemplate>
0
898
by: Alberto Teixeira | last post by:
Hi, I have a bound datagrid. I need to be able to select a row by clicking any of the cells and have it fire of the SelectedIndexChanged event. How can I do this? Thanks Alberto
2
8970
by: Serg | last post by:
Hello, I need to set the selection in the RadioButtonList according to the value of SelectedValue in the underlying dataset - please suggest. The code is below. Thank you. <asp:TemplateColumn> <HeaderStyle Width="7px"></HeaderStyle> <ItemStyle HorizontalAlign="Center"></ItemStyle> <ItemTemplate> <asp:RadioButtonList DataTextField="SelectedValue" DataValueField="SelectedValue" RepeatDirection="Horizontal" ID="Selection"
0
1428
by: js | last post by:
I have a DataGrid with a command button on the first column of the DataGrid. The DataGrid is databound to a dataset with DataKeyFiled bound to "ControlNumber" field in the dataset. I am trying to save the row's ControlNumber to a page level variable when the corresponding item command is clicked . However, no matter what I do, the page property is alway blank. I also tried use CheckBox in the ItemTemplate. The CheckBox's...
0
6926
by: mike | last post by:
how can i get datakey value of GridView ??????? protected void BookGridView_RowDataBound(object sender, GridViewRowEventArgs e) { int tableId = int.Parse( BookGridView.DataKeys.ToString() ); .....
0
1822
by: zhuang | last post by:
Hi, Adding combobox to datagrid has been posted many times. I have a datagrid which has multiple combobox columns and normal textbox columns. But how could I change other combo box values at the same row when user change the value of one of the combo box. The textbox value in the same row could be changed by modifying the
0
1334
by: Kim | last post by:
Im having a problem with one of my datagrids. Basically the datagrid has paging included in it and when a user clicks on page other than the first page and the user selects a row the value is stored but the problem is the value is wrong its still getting the value from the first page. I have view state enabled on both page and datagrid. Im not sure whats wrong, any help will be greatly appreciated
0
1389
by: Stu | last post by:
Hiya, Im sure this is one of those easy if you know how type basically i have a dataview and i want to bind it to a gridview grdResults.DataSource=objDataSet.Tables.DefaultView; grdResults.DataBind(); However i want to set the key like in the old datagrid so i can access it on
3
7694
by: =?Utf-8?B?V2FubmFiZQ==?= | last post by:
I have a gridview that has a label field and a checkbox field in it. I have the checkbox set to autopost when changed. How can I get the datakey value that is assigned to that row when a checkbox is either checked or unchecked?
0
9424
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10051
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8879
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.