Get data from selected record in gridview

J

Julia B

Hi all, this is a second post, so apologies, but I never had an answer to my
first post (several weeks ago) and I really need some help.

I'm using a .Net 2.0 Gridview which is populated using an ObjectDataSource
which calls on a method in a class. This all works fine.

The Gridview has a select button automatically generated. When the user
presses this I want to be able to take the data from the selected record and
use it. However whatever I try, I can't seem to be able to get hold of the
data.

Can anyone help me? How do you read the data in the selected record of a
Gridview?

Thanks in advance.
Julia
 
W

Winfried Wille

Hallo Julia,

i did this for a customer a long time ago.
One way is to implement the Gridviews rowdataboundevent, take the row from
the passed in event argument, check if it's rowtype is
DataControlRowState.Selected
and save the values you need in the viewstate.
Another way would be to enrich the attributes collection of the button
during databinding with the needed values.
The usual way is to use the grids SelectedDataKey property and supply a key
in the definition from the grid.

Regards

Winfried Wille
 
J

Julia B

Hi and thanks for the response.

I'm new to .Net 2.0 and am struggling to understand how Gridviews work as
opposed to 1.0 Datagrids.

I'm not sure I understand how to implement the selecteddatakey solution. I
have found several "samples" but none of them seem to work. Do you know of
any examples that I can look at?

Thanks
 
W

Winfried Wille

Hallo Julia,

fine you found a solution. Selectedrow is another working way, but as you
have seen in the sample, you have to dig into the cell contents, to get your
value .
If you have further problems, i would have looked for some sample code
lines/sample.

Good Luck
Winfried Wille
 
J

Julia B

Thanks for your responses on this. I've got something working using
selectedrow as follows:

Dim row As GridViewRow = Me.dgAccounts.SelectedRow
currentAcType.accountTypeOriginal = row.Cells(1).Text
Me.tbAcc.Text = row.Cells(1).Text
If row.Cells(4).Text <> " " Then
Me.tbEmail.Text = row.Cells(4).Text
End If

This works fine, however I've got a problem with a couple of the cells which
are autogenerated checkboxes. Regardless of whether the display value is true
or false, the text value of the cell is always an empty string. Do you know
how I get the value of the checkbox?

Thanks
Julia
 
J

Julia B

Got it:

CType(row.Cells(9).Controls(0), System.Web.UI.WebControls.CheckBox).Checked
 
W

Winfried Wille

Hallo Julia,

you can use something like this:
protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
bool b = ((CheckBox)
(((GridView)sender).SelectedRow.Cells[2].Controls[0])).Checked;
}
Where 2 is used as the index for your autogenerated checkbox column.
You can download a sample here http://wtww2.de/downloads.gridview.zip.
Using this, you might have compatibilties problems in the future, because
you depend on the automatically generated html for the autogenerated
checkbox column, which is not obligatory specified.
I hope this helps you to progress on your project. If you go further into
asp.net programming, you should consider read a book and stay away from such
things as autogenerated columns.

Regards
Winfried Wille
 

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,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top