473,774 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.SelectedD ataKey.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 91370
S_K
On Oct 18, 10:18 am, "Mike" <M...@community .nospam.comwrot e:
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.SelectedD ataKey.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.Select edIndex 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.comwrot e in message
news:e3******** ******@TK2MSFTN GP06.phx.gbl...
any suggestions on how I can get the cell 1 text for the selected row
within my Select_Click()?
grid1.SelectedR ow.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.goo glegroups.com.. .
On Oct 18, 10:18 am, "Mike" <M...@community .nospam.comwrot e:
>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.SelectedD ataKey.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.Select edIndex 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.SelectedD ataKey.Values[1].ToString();
//carMake =
grid1.SelectedD ataKey.Values["CarMake"].ToString();
//carMake = grid1.SelectedD ataKey["CarMake"].ToString();
//carMake =
grid1.Rows[grid1.SelectedI ndex].Cells[1].Text.ToString( );
//carMake = grid1.SelectedR ow.Cells[1].Text.ToString( );

Response.Write( carMake);

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

grid1.SelectedR ow.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.comwrot e:
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.SelectedD ataKey.Values[1].ToString();
//carMake =
grid1.SelectedD ataKey.Values["CarMake"].ToString();
//carMake = grid1.SelectedD ataKey["CarMake"].ToString();
//carMake =
grid1.Rows[grid1.SelectedI ndex].Cells[1].Text.ToString( );
//carMake = grid1.SelectedR ow.Cells[1].Text.ToString( );

Response.Write( carMake);

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

"Mark Rae [MVP]" <m...@markNOSPA Mrae.netwrote in messagenews:%2* **************@ TK2MSFTNGP06.ph x.gbl...
"Mike" <M...@community .nospam.comwrot e in message
news:e3******** ******@TK2MSFTN GP06.phx.gbl...
any suggestions on how I can get the cell 1 text for the selected row
within my Select_Click()?
grid1.SelectedR ow.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_DataB ound(..) 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******** *************@i 38g2000prf.goog legroups.com...
On Oct 18, 10:56 am, "Mike" <M...@community .nospam.comwrot e:
>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.Selected DataKey.Values[1].ToString();
//carMake =
grid1.Selected DataKey.Values["CarMake"].ToString();
//carMake =
grid1.Selected DataKey["CarMake"].ToString();
//carMake =
grid1.Rows[grid1.SelectedI ndex].Cells[1].Text.ToString( );
//carMake =
grid1.Selected Row.Cells[1].Text.ToString( );

Response.Write( carMake);

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

"Mark Rae [MVP]" <m...@markNOSPA Mrae.netwrote in
messagenews:%2 *************** @TK2MSFTNGP06.p hx.gbl...
"Mike" <M...@community .nospam.comwrot e in message
news:e3******* *******@TK2MSFT NGP06.phx.gbl.. .
>any suggestions on how I can get the cell 1 text for the selected row
within my Select_Click()?
grid1.SelectedR ow.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_DataB ound(..) event. That way you are certain that the
GridView exists.

Steve

Oct 18 '07 #7
"S_K" <st***********@ yahoo.comwrote in message
news:11******** *************@i 38g2000prf.goog legroups.com...
On Oct 18, 10:56 am, "Mike" <M...@community .nospam.comwrot e:
>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.Selected DataKey.Values[1].ToString();
//carMake =
grid1.Selected DataKey.Values["CarMake"].ToString();
//carMake =
grid1.Selected DataKey["CarMake"].ToString();
//carMake =
grid1.Rows[grid1.SelectedI ndex].Cells[1].Text.ToString( );
//carMake =
grid1.Selected Row.Cells[1].Text.ToString( );

Response.Write( carMake);

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

"Mark Rae [MVP]" <m...@markNOSPA Mrae.netwrote in
messagenews:%2 *************** @TK2MSFTNGP06.p hx.gbl...
"Mike" <M...@community .nospam.comwrot e in message
news:e3******* *******@TK2MSFT NGP06.phx.gbl.. .
>any suggestions on how I can get the cell 1 text for the selected row
within my Select_Click()?
grid1.SelectedR ow.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_DataB ound(..) 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******** *************@i 38g2000prf.goog legroups.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_DataB ound(..) 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.comwrot e in message
news:uh******** ******@TK2MSFTN GP06.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.SelectedR ow and
press Enter - what do you see...?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Oct 18 '07 #10

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

Similar topics

1
9388
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 in the dataset is empty. The cells that are empty, in the gridview control, I would like to have the value of 0 (zero) inserted. Is there a default cell value property or something similar for the asp.net gridview control. So if no data is...
0
1596
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 click the update, I don't get the value. Is there another way of doing this. I want to Read the GridView cell from my code-behind and pass that to a update method that I have written in the .cs file. -- Thanks,
3
3309
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 because it contains "&nbsp". I really don't want to compare the value to that so how can I retrieve an empty cell so that it is String.Empty. Bill
27
109403
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 function. If I am in debug mode and have a breakpoint at this line, I can see when hovering my mouse over "Range("C1").Value" the actual value in that cell (in my case 30), but when I want to step to the next statement, the VB programs ends (due to some...
1
2189
by: Vyas111111 | last post by:
Hello All, How can i get the value of a gridview cell outside any GridView event
2
7613
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 event of gridview but it gives Null reference exception error while clicking the button in button column of grid view : int index = gdvOdetails.SelectedIndex; object oid = (object)gdvOdetails.SelectedDataKey.Values; txtMsg.Text =...
4
5862
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 know if it is possible to make such focus? Thanks! Bill
2
14728
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 please. Thank you very much in advance
3
5087
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 radiobuttonlist control.i have two options in radiobuttonlist.
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10040
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9914
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8939
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6717
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4012
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2852
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.