473,397 Members | 2,116 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,397 software developers and data experts.

get cell value within gridview

I have a gridview that has a button in the last cell of each row. I want to get the text from cell 1 for that row the button was clicked on.

So if my gridview looks like this:

N-12 BMW [select]
N-35 Mercedes [select]

if my user clicks row 2, I want to see Mercedes, How can I do that?

I have a buttonClick event for my select button, I'm trying to pull the values using the DataKeyName, but I keep getting 'object reference not set to an instance of an object.

here is what i'm trying

select_Click()
{
string make = grid1.SelectedDataKey.Values["CarMake"].toString();

}

any suggestions on how I can get the cell 1 text for the selected row within my Select_Click()?
Oct 18 '07 #1
14 91306
S_K
On Oct 18, 10:18 am, "Mike" <M...@community.nospam.comwrote:
I have a gridview that has a button in the last cell of each row. I want to get the text from cell 1 for that row the button was clicked on.

So if my gridview looks like this:

N-12 BMW [select]
N-35 Mercedes [select]

if my user clicks row 2, I want to see Mercedes, How can I do that?

I have a buttonClick event for my select button, I'm trying to pull the values using the DataKeyName, but I keep getting 'object reference not set to an instance of an object.

here is what i'm trying

select_Click()
{
string make = grid1.SelectedDataKey.Values["CarMake"].toString();

}

any suggestions on how I can get the cell 1 text for the selected row within my Select_Click()?
Try using the GridView.SelectedIndex value to point to the selected
row.
Then use the GridView1.Rows[SelectedIndex].Cells[1].Text to get the
value inside the cell.

I hope that helps!

Steve

Oct 18 '07 #2
"Mike" <Mi**@community.nospam.comwrote in message
news:e3**************@TK2MSFTNGP06.phx.gbl...
any suggestions on how I can get the cell 1 text for the selected row
within my Select_Click()?
grid1.SelectedRow.Cells[1].Text;
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Oct 18 '07 #3
I tried that and it gave me this error:

Index was out of range. Must be non-negative and less than the size
of the collection. Parameter name: index

"S_K" <st***********@yahoo.comwrote in message
news:11**********************@v23g2000prn.googlegr oups.com...
On Oct 18, 10:18 am, "Mike" <M...@community.nospam.comwrote:
>I have a gridview that has a button in the last cell of each row. I want
to get the text from cell 1 for that row the button was clicked on.

So if my gridview looks like this:

N-12 BMW [select]
N-35 Mercedes [select]

if my user clicks row 2, I want to see Mercedes, How can I do that?

I have a buttonClick event for my select button, I'm trying to pull the
values using the DataKeyName, but I keep getting 'object reference not
set to an instance of an object.

here is what i'm trying

select_Click()
{
string make = grid1.SelectedDataKey.Values["CarMake"].toString();

}

any suggestions on how I can get the cell 1 text for the selected row
within my Select_Click()?

Try using the GridView.SelectedIndex value to point to the selected
row.
Then use the GridView1.Rows[SelectedIndex].Cells[1].Text to get the
value inside the cell.

I hope that helps!

Steve

Oct 18 '07 #4
I've tried that to and get;

Object reference not set to an instance of an object

here is what i've tried actaully and got the above message for all
protected void SendSales_Click(object sender, EventArgs e)
{
try
{
string carMake= " ";
//carMake= grid1.SelectedDataKey.Values[1].ToString();
//carMake =
grid1.SelectedDataKey.Values["CarMake"].ToString();
//carMake = grid1.SelectedDataKey["CarMake"].ToString();
//carMake =
grid1.Rows[grid1.SelectedIndex].Cells[1].Text.ToString();
//carMake = grid1.SelectedRow.Cells[1].Text.ToString();

Response.Write(carMake);

}
catch (Exception ex)
{
result = ex.Message;
}
}
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
"Mike" <Mi**@community.nospam.comwrote in message
news:e3**************@TK2MSFTNGP06.phx.gbl...
>any suggestions on how I can get the cell 1 text for the selected row
within my Select_Click()?

grid1.SelectedRow.Cells[1].Text;
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Oct 18 '07 #5
S_K
On Oct 18, 10:56 am, "Mike" <M...@community.nospam.comwrote:
I've tried that to and get;

Object reference not set to an instance of an object

here is what i've tried actaully and got the above message for all
protected void SendSales_Click(object sender, EventArgs e)
{
try
{
string carMake= " ";
//carMake= grid1.SelectedDataKey.Values[1].ToString();
//carMake =
grid1.SelectedDataKey.Values["CarMake"].ToString();
//carMake = grid1.SelectedDataKey["CarMake"].ToString();
//carMake =
grid1.Rows[grid1.SelectedIndex].Cells[1].Text.ToString();
//carMake = grid1.SelectedRow.Cells[1].Text.ToString();

Response.Write(carMake);

}
catch (Exception ex)
{
result = ex.Message;
}
}

"Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote in messagenews:%2***************@TK2MSFTNGP06.phx.gbl ...
"Mike" <M...@community.nospam.comwrote in message
news:e3**************@TK2MSFTNGP06.phx.gbl...
any suggestions on how I can get the cell 1 text for the selected row
within my Select_Click()?
grid1.SelectedRow.Cells[1].Text;
--
Mark Rae
ASP.NET MVP
http://www.markrae.net- Hide quoted text -

- Show quoted text -
If you get an error "Object instance not set..." it's because the
GridView hasn't been created yet. Try implementing your logic in the
GridView1_DataBound(..) event. That way you are certain that the
GridView exists.

Steve

Oct 18 '07 #6
On this button, another developer setup an CommandArgument and is getting a
value that way within this same SendSales_Click(), is there a way to add
another value to that CommandArgument so it's passing 2 instead of 1 value?
If I put this in the grid_databound method, how would that work in passing
the value to the button when the user clicks it?
The Grid is already bound, the values are already there in the grid, I just
want the value from cell[1], when I click the button.

"S_K" <st***********@yahoo.comwrote in message
news:11*********************@i38g2000prf.googlegro ups.com...
On Oct 18, 10:56 am, "Mike" <M...@community.nospam.comwrote:
>I've tried that to and get;

Object reference not set to an instance of an object

here is what i've tried actaully and got the above message for all
protected void SendSales_Click(object sender, EventArgs e)
{
try
{
string carMake= " ";
//carMake=
grid1.SelectedDataKey.Values[1].ToString();
//carMake =
grid1.SelectedDataKey.Values["CarMake"].ToString();
//carMake =
grid1.SelectedDataKey["CarMake"].ToString();
//carMake =
grid1.Rows[grid1.SelectedIndex].Cells[1].Text.ToString();
//carMake =
grid1.SelectedRow.Cells[1].Text.ToString();

Response.Write(carMake);

}
catch (Exception ex)
{
result = ex.Message;
}
}

"Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote in
messagenews:%2***************@TK2MSFTNGP06.phx.gb l...
"Mike" <M...@community.nospam.comwrote in message
news:e3**************@TK2MSFTNGP06.phx.gbl...
>any suggestions on how I can get the cell 1 text for the selected row
within my Select_Click()?
grid1.SelectedRow.Cells[1].Text;
--
Mark Rae
ASP.NET MVP
http://www.markrae.net- Hide quoted text -

- Show quoted text -

If you get an error "Object instance not set..." it's because the
GridView hasn't been created yet. Try implementing your logic in the
GridView1_DataBound(..) event. That way you are certain that the
GridView exists.

Steve

Oct 18 '07 #7
"S_K" <st***********@yahoo.comwrote in message
news:11*********************@i38g2000prf.googlegro ups.com...
On Oct 18, 10:56 am, "Mike" <M...@community.nospam.comwrote:
>I've tried that to and get;

Object reference not set to an instance of an object

here is what i've tried actaully and got the above message for all
protected void SendSales_Click(object sender, EventArgs e)
{
try
{
string carMake= " ";
//carMake=
grid1.SelectedDataKey.Values[1].ToString();
//carMake =
grid1.SelectedDataKey.Values["CarMake"].ToString();
//carMake =
grid1.SelectedDataKey["CarMake"].ToString();
//carMake =
grid1.Rows[grid1.SelectedIndex].Cells[1].Text.ToString();
//carMake =
grid1.SelectedRow.Cells[1].Text.ToString();

Response.Write(carMake);

}
catch (Exception ex)
{
result = ex.Message;
}
}

"Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote in
messagenews:%2***************@TK2MSFTNGP06.phx.gb l...
"Mike" <M...@community.nospam.comwrote in message
news:e3**************@TK2MSFTNGP06.phx.gbl...
>any suggestions on how I can get the cell 1 text for the selected row
within my Select_Click()?
grid1.SelectedRow.Cells[1].Text;
--
Mark Rae
ASP.NET MVP
http://www.markrae.net- Hide quoted text -

- Show quoted text -

If you get an error "Object instance not set..." it's because the
GridView hasn't been created yet. Try implementing your logic in the
GridView1_DataBound(..) event. That way you are certain that the
GridView exists.

Steve


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Oct 18 '07 #8
"S_K" <st***********@yahoo.comwrote in message
news:11*********************@i38g2000prf.googlegro ups.com...
If you get an error "Object instance not set..." it's because the
GridView hasn't been created yet. Try implementing your logic in the
GridView1_DataBound(..) event. That way you are certain that the
GridView exists.
??? The OP is trying to get the value of one of the GridView's cells by
clicking a button on the GridView - he wouldn't even see the button if the
GridView hadn't been created yet...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Oct 18 '07 #9
"Mike" <Mi**@community.nospam.comwrote in message
news:uh**************@TK2MSFTNGP06.phx.gbl...
I've tried that to and get;

Object reference not set to an instance of an object
Hmm - OK...

Set a breakpoint on the first line of the click even and run the code again

1) Does the code jump into the correct method?

2) Assuming it does, in the Immediate window write grid1 and press Enter -
do you see the properties of your GridView?

3) Assuming you do, in the Immediate window write grid1.SelectedRow and
press Enter - what do you see...?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Oct 18 '07 #10
1) Does the code jump into the correct method?
Yes

2) Assuming it does, in the Immediate window write grid1 and press Enter -
do you see the properties of your GridView;
yes, but these properties are set to the following, even though I
selected a row via the button
SelectedDataKey: null
SelectedIndex: -1
SelectedRow: null
SelectedRowStyle: {System.Web.UI.WebControls.TableItemStyle}
SelectedValue: null
3) Assuming you do, in the Immediate window write grid1.SelectedRow and
press Enter - what do you see...? null is displayed in the windows

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:ez**************@TK2MSFTNGP02.phx.gbl...
"Mike" <Mi**@community.nospam.comwrote in message
news:uh**************@TK2MSFTNGP06.phx.gbl...
>I've tried that to and get;

Object reference not set to an instance of an object

Hmm - OK...

Set a breakpoint on the first line of the click even and run the code
again

1) Does the code jump into the correct method?

2) Assuming it does, in the Immediate window write grid1 and press Enter -
do you see the properties of your GridView?

3) Assuming you do, in the Immediate window write grid1.SelectedRow and
press Enter - what do you see...?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Oct 18 '07 #11
"Mike" <Mi**@community.nospam.comwrote in message
news:e7**************@TK2MSFTNGP06.phx.gbl...
>1) Does the code jump into the correct method?
Yes
OK.
>2) Assuming it does, in the Immediate window write grid1 and press
Enter - do you see the properties of your GridView?
SelectedRow: null
Aha - well there's your problem right there...

Whatever you are doing behind the click of the Buttons in the GridView's
rows, it's not actually marking the row the Button is on as Selected...

Fix that, and everything else should work...

Alternately, wire up a SelectedIndexChanged event to the GridView

protected void grid1_SelectedIndexChanged(Object sender, EventArgs e)
{
GridViewRow objRow = grid1.SelectedRow;
}

Does that at least tell you which row is selected...?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Oct 18 '07 #12
Just put a test

if ( SelectedIndex < 0 )
return;
-----Original Message-----
From: Mike [mailto:Mi**@community.nospam.com]
Posted At: Friday, 19 October 2007 3:49 AM
Posted To: microsoft.public.dotnet.framework.aspnet
Conversation: get cell value within gridview
Subject: Re: get cell value within gridview

1) Does the code jump into the correct method?
Yes

2) Assuming it does, in the Immediate window write grid1 and press
Enter -
do you see the properties of your GridView;
yes, but these properties are set to the following, even
though
I
selected a row via the button
SelectedDataKey: null
SelectedIndex: -1
SelectedRow: null
SelectedRowStyle:
{System.Web.UI.WebControls.TableItemStyle}
SelectedValue: null
3) Assuming you do, in the Immediate window write grid1.SelectedRow
and
press Enter - what do you see...? null is displayed in the windows

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:ez**************@TK2MSFTNGP02.phx.gbl...
"Mike" <Mi**@community.nospam.comwrote in message
news:uh**************@TK2MSFTNGP06.phx.gbl...
I've tried that to and get;

Object reference not set to an instance of an object
Hmm - OK...

Set a breakpoint on the first line of the click even and run the
code
again

1) Does the code jump into the correct method?

2) Assuming it does, in the Immediate window write grid1 and press
Enter -
do you see the properties of your GridView?

3) Assuming you do, in the Immediate window write grid1.SelectedRow
and
press Enter - what do you see...?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Oct 18 '07 #13
"Ian Semmel" <an****@rocketcomp.com.auwrote in message
news:A8A047B34A504EA5A7DE7F646DBB42AE@DIMITY...
Just put a test

if ( SelectedIndex < 0 )
return;
How will that help...?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Oct 18 '07 #14
I've just noticed (after about an hour on this), the developer that created
this page is using a template field that has a imgButton in it. So he has
the image Button calling the Sales_Click() method. he is currently passing
the ID of the field using the CommandArgument.

So I guess I have a new issue then with this. Is there a way to either pass
2 parameters in the command argument or is there a way to get the value from
a particular cell within the grid when the user clicks the imgButton on a
row?


"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:em**************@TK2MSFTNGP06.phx.gbl...
"Mike" <Mi**@community.nospam.comwrote in message
news:e7**************@TK2MSFTNGP06.phx.gbl...
>>1) Does the code jump into the correct method?
Yes

OK.
>>2) Assuming it does, in the Immediate window write grid1 and press
Enter - do you see the properties of your GridView?
SelectedRow: null

Aha - well there's your problem right there...

Whatever you are doing behind the click of the Buttons in the GridView's
rows, it's not actually marking the row the Button is on as Selected...

Fix that, and everything else should work...

Alternately, wire up a SelectedIndexChanged event to the GridView

protected void grid1_SelectedIndexChanged(Object sender, EventArgs e)
{
GridViewRow objRow = grid1.SelectedRow;
}

Does that at least tell you which row is selected...?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Oct 18 '07 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jon S via DotNetMonster.com | last post by:
HI all, I'm returning a dataset to a gridview control. When the gridview asp.net control is populated from the returning dataset some of the cells remain empty. This is expected as some data...
0
by: SDRoy | last post by:
Hi Can someone help me to figure out how to get the editable textbox value from GridView in my code-behind ? I am using the code something like- GridView2.Rows.Cells.Text.Trim(). But when I...
3
by: Bill Gower | last post by:
I have an cell which contains a field member which is filled with String.Empty. When I retrieve the value in the cell with Cells.Text.ToString() and try to compare it to String.Empty, I can't...
27
by: geniet | last post by:
Hello all of you, I have some problem in setting cells values (or fromat or...) in my Excel spreadsheet using a VB statement like within my Module: Range("C1").Value = 20 I have this in a...
1
by: Vyas111111 | last post by:
Hello All, How can i get the value of a gridview cell outside any GridView event
2
by: rameshgohil | last post by:
I am using grid view and a button column in it using <itemTemplate> but I am not able to rerive cell value of a selected row from grid view. I have tried the following to methods in Row_command...
4
by: foolmelon | last post by:
Before AJAX, we were able to focus a cell in a gridview during a fullpage postback. After putting the gridview inside an UpdatePanel, we cannot focus a cell in this gridview anymore. Does anybody...
2
by: minhtran | last post by:
HI all Please, anyone has any idea how to get value from gridview in ASP.NET using VB.Net. I use C# as a code Gridview1.SelectedRow.Cell(i).Text , it works but not on VB.NET. If any one can help me...
3
by: bharathi228 | last post by:
hi, iam doing one asp.net application in gridview i have 3 columns. the first two column values are coming from database. the third column is template field.here i have one...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.