Avatar of FEAssociates
FEAssociates asked on

How do i return the row number in a datagrid by searching on a specific datakeyfield

I need to return the actual datagrid row number so i can highlight any timetable clashes in red.
I basically search for any clashes in Sql Server and then reutrn the ID number, i then need to search for the ID in the the datakeyfield and get the actual datagrid row number that this exists in.
ASP.NET.NET Programming

Avatar of undefined
Last Comment
FEAssociates

8/22/2022 - Mon
molku

Use the DataKeys  property of your datagrid to return a DataKeyCollection. Loop through testing Item[x] for your value and you will have your index. One thing I am not sure of is if paging affects this collection.
superthumper

assuming you're doing this on a postback....



PK = 'the ID you're searching for
For Each rw As GridViewRow In GridView1.Rows
                If Convert.ToInt32(GridView1.DataKeys(rw.DataItemIndex).Value) = PK Then
                    'do whatever you need to do to highlight the row
                    'rw.Style
                    'rw.BackColor
                     Exit For
                End If
            Next

Open in new window

ASKER
FEAssociates

I am using Vb 2003, so i dont have gridview, i only have datgrid which doesn't explicitly have a rows collection
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
prairiedog

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
FEAssociates

Thank you, just need to refine my sql statement now :-)