• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1614
  • Last Modified:

Trying to access cell value of a gridview

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
will110
Asked:
will110
  • 6
  • 4
  • 2
1 Solution
 
Jaime OlivaresSoftware ArchitectCommented:
define "no luck"
0
 
will110Author Commented:
I couldnt get the cell value
0
 
anarki_jimbelCommented:
Try:

string s = (string)this.dataGridView1.Rows[0].Cells[0].Value;
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
Jaime OlivaresSoftware ArchitectCommented:
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
 
anarki_jimbelCommented:
Agree jaime_olivares, my fault :)
0
 
will110Author Commented:
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
 
anarki_jimbelCommented:
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
 
anarki_jimbelCommented:
Try something like:

GridView1..Rows[0].Cells[0].Controls[0]).Text;
0
 
will110Author Commented:
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
 
anarki_jimbelCommented:
In this case GridView1..Rows[0].Cells[0].Controls[0]).Text; should work...
0
 
will110Author Commented:
I was having many controls on my page how to know the contol number?
0
 
anarki_jimbelCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 6
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now