473,327 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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 11037
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: | last post by:
Hello, Does anyone have an idea on how I can filter the data in the gridview control that was returned by an sql query? I have a gridview that works fine when I populate it with data. Now I...
2
by: dbuchanan | last post by:
Hello, I want to open a child form based on the record selected in a dataGridView of the parent form. I want the child form to open with a populated child table based on the selected parent...
1
by: MikeB | last post by:
ooh boy, I hope I'm in the right place to ask this. I'm trying to do a class project that binds controls to data sources. I have a Drop-down List that I bound to the Author column of an SQL...
2
by: Steve Kershaw | last post by:
Hi, I'm working on a project in which I have a Gridview that has data. That Gridview data must be then exported to an Excel spreadsheet. I have successfully displayed the Excel spreadsheet and...
7
by: KiwiGenie | last post by:
I have a form which lists selected records, there could be anything from 1 record to all records selected. I want to output a report for each record as HTML. I want each file to take its name from a...
1
by: Bob | last post by:
Hello, Using VB 2005.net. I have a bound datagridview and a delete button control (or when the user pressed the 'Delete' key) I can successfully delete the selected record from the table and...
3
by: btreddy | last post by:
Hiii experts , I've received one critical requirement from my customer this morning. I've a gridview,in one of my webpage, which displays all the prticipants who are participating in a...
2
by: eneyardi | last post by:
How to filter selected record in a report? i have a form name Data Tracking Details where i can view my records one by one using record selector. Now, i want my current record in a form to be...
2
by: David Patz | last post by:
Is it possible for a user to select a record from a datasheet in a subform, double click it, and write that specific record to another table? I have attempted to perform this task using...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.