Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > get cell value within gridview

Reply
Thread Tools

get cell value within gridview

 
 
Mike
Guest
Posts: n/a
 
      10-18-2007
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()?


 
Reply With Quote
 
 
 
 
S_K
Guest
Posts: n/a
 
      10-18-2007
On Oct 18, 10:18 am, "Mike" <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
 
 
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      10-18-2007
"Mike" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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

 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      10-18-2007
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> On Oct 18, 10:18 am, "Mike" <(E-Mail Removed)> wrote:
>> 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
>



 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      10-18-2007
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]" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> "Mike" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> 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



 
Reply With Quote
 
S_K
Guest
Posts: n/a
 
      10-18-2007
On Oct 18, 10:56 am, "Mike" <(E-Mail Removed)> wrote:
> 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]" <(E-Mail Removed)> wrote in messagenews:%(E-Mail Removed) ...
>
>
>
> > "Mike" <(E-Mail Removed)> wrote in message
> >news:(E-Mail Removed)...

>
> >> 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

 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      10-18-2007
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> On Oct 18, 10:56 am, "Mike" <(E-Mail Removed)> wrote:
>> 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]" <(E-Mail Removed)> wrote in
>> messagenews:%(E-Mail Removed) ...
>>
>>
>>
>> > "Mike" <(E-Mail Removed)> wrote in message
>> >news:(E-Mail Removed)...

>>
>> >> 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
>



 
Reply With Quote
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      10-18-2007
"S_K" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...

> On Oct 18, 10:56 am, "Mike" <(E-Mail Removed)> wrote:
>> 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]" <(E-Mail Removed)> wrote in
>> messagenews:%(E-Mail Removed) ...
>>
>>
>>
>> > "Mike" <(E-Mail Removed)> wrote in message
>> >news:(E-Mail Removed)...

>>
>> >> 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

 
Reply With Quote
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      10-18-2007
"S_K" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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

 
Reply With Quote
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      10-18-2007
"Mike" <(E-Mail Removed)> wrote in message
news:uhd$(E-Mail Removed)...

> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Get GridView Cell Value Chris ASP .Net 4 06-14-2009 07:59 AM
get value of a cell in a gridview graphicsxp ASP .Net 12 02-20-2008 06:52 PM
How to get Gridview cell value on select? Dave Hewes ASP .Net Web Controls 1 02-28-2007 06:19 PM
Gridview: Get the value in the cell G. Whiz ASP .Net 1 03-01-2006 10:21 PM
How do I have a table cell fetch another page to display within the cell? Phillip Roncoroni HTML 14 04-05-2004 05:58 PM



Advertisments