Solved

Trying to access cell value of a gridview

Posted on 2008-10-01
12
1,596 Views
Last Modified: 2012-05-05
Hello Experts,
Here is the code I'm using to acess the cell value of a gridviw but there is no luck.... Help me out

 protected void Button3_Click(object sender, EventArgs e)
       {
           
                          string speakerName =GridView1.Rows[0].Cells[0].Text;    
           
       }
0
Comment
Question by:will110
  • 6
  • 4
  • 2
12 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22619329
define "no luck"
0
 

Author Comment

by:will110
ID: 22619421
I couldnt get the cell value
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 22619433
Try:

string s = (string)this.dataGridView1.Rows[0].Cells[0].Value;
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 22619487
Still your question is too vague, do you have an error message?
you can use:

string speakerName = GridView1.Rows[0].Cells[0].Value.ToString();

notice that is not the same as:

string speakerName = (string)GridView1.Rows[0].Cells[0].Value;

because last will be fail if value is not of type string
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 22619520
Agree jaime_olivares, my fault :)
0
 

Author Comment

by:will110
ID: 22620647
Hello
Sorry for Incomplete question..
I dont have any errors..
and I cant find .value in the below statement which you gave it might be because I'm building a web application

string speakerName = GridView1.Rows[0].Cells[0].Value.ToString();  //Cell[0].Value is not found

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 22620730
Yeah, for web - have to use Text property.


If it's web you probably need to get this value from the response object, I'm not very sure about that.
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 22620966
Try something like:

GridView1..Rows[0].Cells[0].Controls[0]).Text;
0
 

Author Comment

by:will110
ID: 22626342
This is what I was looking for

foreach (GridViewRow gvr in this.GridView1.Rows)
                   {
                        Label lblName = (Label)gvr.FindControl("lblSpeakerName");
                       Label lblTitle = (Label)gvr.FindControl("lblSpeakerTitle");
                    }

Basically, if you use TemplateField to display data, then you will use FindControl to access the content. If you use BoundField, then you use Cells[i].Text to access the content.

Thank You anyway
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 22628059
In this case GridView1..Rows[0].Cells[0].Controls[0]).Text; should work...
0
 

Author Comment

by:will110
ID: 22628406
I was having many controls on my page how to know the contol number?
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 22628586
Does not matter - you have found the solution.

But something tells me that the clicked cell has just ONE control that you retrieve with this code. Not sure of course.
BTW, if it works it has one advantage over searching a label - performance.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Parameter transfer to cristal report 3 52
the name DateAdd does not exist in this context 2 36
Create XML 5 34
XAML: Layout 8 0
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now