470,821 Members | 2,054 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,821 developers. It's quick & easy.

Get data from selected record in gridview

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

Oct 21 '08 #1
7 10919
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

"Julia B" <Ju****@discussions.microsoft.comwrote in message
news:3B**********************************@microsof t.com...
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
Oct 21 '08 #2
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
"Winfried Wille" wrote:
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

"Julia B" <Ju****@discussions.microsoft.comwrote in message
news:3B**********************************@microsof t.com...
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
Oct 21 '08 #3
Thanks for your help, found this:

http://msdn.microsoft.com/en-us/libr...lectedrow.aspx

"Winfried Wille" wrote:
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

"Julia B" <Ju****@discussions.microsoft.comwrote in message
news:3B**********************************@microsof t.com...
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
Oct 21 '08 #4
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

"Julia B" <Ju****@discussions.microsoft.comwrote in message
news:DA**********************************@microsof t.com...
Thanks for your help, found this:

http://msdn.microsoft.com/en-us/libr...lectedrow.aspx
Oct 21 '08 #5
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

"Winfried Wille" wrote:
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

"Julia B" <Ju****@discussions.microsoft.comwrote in message
news:DA**********************************@microsof t.com...
Thanks for your help, found this:

http://msdn.microsoft.com/en-us/libr...lectedrow.aspx
Oct 22 '08 #6
Got it:

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

"Julia B" wrote:
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

"Winfried Wille" wrote:
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

"Julia B" <Ju****@discussions.microsoft.comwrote in message
news:DA**********************************@microsof t.com...
Thanks for your help, found this:
>
http://msdn.microsoft.com/en-us/libr...lectedrow.aspx
>
Oct 22 '08 #7
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

"Julia B" <Ju****@discussions.microsoft.comwrote in message
news:B5**********************************@microsof t.com...
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?
>"Julia B" <Ju****@discussions.microsoft.comwrote in message
news:DA**********************************@microso ft.com...
Thanks for your help, found this:

http://msdn.microsoft.com/en-us/libr...lectedrow.aspx
Oct 22 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by MikeB | last post: by
3 posts views Thread by btreddy | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.