Assoicate a primary key with a checkbox inside a datagrid

M

Mark

Hi everyone, could someone please recommend a way to do the following.

I have a datagrid which contains a bound template column consisting of
checkbox and a string pulled from a database table.
I want to be able to associate the primary key of the database table with
the checkbox so that when someone checks a box I can iterate through all the
checkboxs and identify the associated value in the database.

Thanks in advance
Mark
 
G

Guest

Hi Mark,

In order to get parimary key value in a datagrid, you can set the datagrid's
DataKeyField = "Parimary_Key_Field_Name". And loop thru datagrid items

CheckBox ck;
foreach (DataGridItem item in datagrid.Items)
{
string pk_value = datagrid.DataKeys[item.Item.ItemIndex].ToString();
ck = (CheckBox)item.FindControl("ckBoxID");
bool checked = ck.Checked;
// process
}


HTH

Elton Wang
(e-mail address removed)
 
M

Mark

Hi all, I could not find an answer to my original question from anyone so I
just did a little hack

Basically, inside the itemtemplate I created a hidden text field which I
populated with the associated primary key for each record.

When I want to check if a checkbox has been checked and retrieve the primary
key I just did the looked for this hidden field and retrieved the primary
key value that was originally stored in it. Works a treat but I can't help
feeling that there must be a better way

Cheers
Mark
 
M

Mark

Ahh, that's it :) Thanks Elton
Cheers
Mark
Elton W said:
Hi Mark,

In order to get parimary key value in a datagrid, you can set the datagrid's
DataKeyField = "Parimary_Key_Field_Name". And loop thru datagrid items

CheckBox ck;
foreach (DataGridItem item in datagrid.Items)
{
string pk_value = datagrid.DataKeys[item.Item.ItemIndex].ToString();
ck = (CheckBox)item.FindControl("ckBoxID");
bool checked = ck.Checked;
// process
}


HTH

Elton Wang
(e-mail address removed)


Mark said:
Hi everyone, could someone please recommend a way to do the following.

I have a datagrid which contains a bound template column consisting of
checkbox and a string pulled from a database table.
I want to be able to associate the primary key of the database table with
the checkbox so that when someone checks a box I can iterate through all the
checkboxs and identify the associated value in the database.

Thanks in advance
Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top