grab primary key of the currently selected row of a collection that is bound to datagridView

Posts   
 
    
yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 02-Apr-2006 16:43:47   

version 1.0.2005.1 final (self-servicing) VS2005 winforms


hiya,

I grabbed some code from an llblGenPro. The user selects a row by: 1) selecting a row in the datagridView 2) clicking on a button.


_selectedCustomer = _allCustomers[customersDataGrid.CurrentRowIndex];

I want to be able to grab the primaryKey of the entityCollection, simply by selecting a row in the datagrid.

has anyone done anything similar?

many thanks,

yogi

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 02-Apr-2006 18:31:43   

entity.Fields.PrimaryKeyFields is your friend simple_smile

Frans Bouma | Lead developer LLBLGen Pro
yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 02-Apr-2006 19:51:20   

cheers Otis.

I have a couple of problems:

1) i don't know which datdagridView event handler to use..eg, if I use the "RowEnter" handler, the this event runs for EVERY row as it is bound to the datagridView..I only want it to run when a user actually selects a row.

2) you say

entity.Fields.PrimaryKeyFields

Does that mean that I don't use the framework "e" event args at all?

if anyone has a code snip, it'd be much appreciated..

I am binding to the datagridView as follows:


 tblDeliveryCollection1.GetMulti(null);
            dataGridView1.DataSource = tblDeliveryCollection1;

Many thanks,

yogi

JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 02-Apr-2006 20:23:36   

In the button Click event, you can reference the entity bound to the CurrentRow as follows:


tblDeliveryEntity delivery = (tblDeliveryEntity) this.dataGridView1.CurrentRow.DataBoundItem;

// you now can reference all of the properties of the current entity
System.Diagnostices.Trace.WriteLine(delivery.DeliveryId.ToString());

// delivery.Fields.PrimaryKeyFields contains a List of IEntityField references which form the primary key 

yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 02-Apr-2006 23:19:06   

thanks Jim.

I was trying to be able to do it without using a button_click, but I don't think it's possible. It's no major hassle though, a button click will do fine wink

yogi

JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 03-Apr-2006 01:40:51   

"RowEnter: Occurs when a row receives input focus and becomes the current row."

This doesn't occur during databinding, but during navigation of the grid.

You may also want to look into the DoubleClick, CellClick or ContentClick events.

hchattaway
User
Posts: 38
Joined: 06-Apr-2006
# Posted on: 06-Apr-2006 04:57:04   

JimHugh wrote:

"RowEnter: Occurs when a row receives input focus and becomes the current row."

This doesn't occur during databinding, but during navigation of the grid.

You may also want to look into the DoubleClick, CellClick or ContentClick events.

What I believe you are looking for is the OnSelectedIndexChanged event. In here you can retrieve the currently selected rows key value.

IN the gridview definition, specify a column for selecting a row like this: <asp:ButtonField Text="Select..." HeaderText="Select" CommandName="Select"/>

clicking on this link in the grid calls the "OnSelectedIndexChanged" event which you hook up with this code in the gridview definition:

     OnSelectedIndexChanged="App_LookUpTables_GridView_OnSelectedIndexChanged"              

DataKeyNames="LuTableID" ---> specifies what field is the pk field.

in the above specified method, you can retrieve the key value like this: [gridname].SelectedDataKey.Value.ToString();

Hope this helps! Harold

yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 19-Apr-2006 11:14:35   

hiya folks,

Harold, I'm using winforms unfortunately.

Grabbing the CellClick event was the answer.

Cheers Jim.

(the user had subsequently decided that they preferred using a button :-0)

yogi