Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / October 2004

Tip: Looking for answers? Try searching our database.

Get currently selected row in datagrid?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chris Ashley - 15 Oct 2004 14:46 GMT
I have a datagrid bound to a dataview. How do I get the currently selected
row programmatically so I can retrieve my primary key?
Chris Ashley - 15 Oct 2004 15:24 GMT
>I have a datagrid bound to a dataview. How do I get the currently selected
>row programmatically so I can retrieve my primary key?

I managed to do this by having my primary key as a 0 width column in my
datagrid tablestyles collection then using the following code:

Dim PKCell As String
PKCell = DataGrid1.Item(DataGrid1.CurrentRowIndex, 4)

Index 4 being my hidden cell... I guess this is bad practice but couldn't
find any other way of doing it.
Rakesh Rajan - 15 Oct 2004 16:49 GMT
Hi Chris,

This isn't really necessary. What you usually do is this: find the row which
is the currently selected in a control. You could use the Binding Manager to
find out the current
object/row that the bound datagrid is now referring to. For example, to get
the current row, use:
((CurrencyManager)BindingContext[datatablename]).Current.
Cast this into a datarow (or your typed one), and then use it.
Once you have the row, you could get the primary key or whatever field you
require.

Your method will work, but it not the .NET way of doing things :)

This is a good article which explains the data binding procedure in windows
forms:
http://msdn.microsoft.com/library/en-us/dndotnet/html/databindingadonet.asp

HTH,
Rakesh Rajan

> >I have a datagrid bound to a dataview. How do I get the currently selected
> >row programmatically so I can retrieve my primary key?
[quoted text clipped - 7 lines]
> Index 4 being my hidden cell... I guess this is bad practice but couldn't
> find any other way of doing it.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.