10 Replies - 4618 Views - Last Post: 15 November 2010 - 08:14 AM Rate Topic: -----

#1 mido911  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 89
  • Joined: 29-October 10

Gridview

Posted 07 November 2010 - 12:46 PM

I have a question regarding gridview. If I have a table of employees with id,name,address and in the gridview i enable the selection control. If I select a row how can i get for example the name of the employee in this selected row. I am trying to do that in GridView1_SelectedIndexChanged
GridView1.SelectedRow.FindControl("name").ToString() but when i am selecting it is giving Object reference not set to an instance of an object.
Is This A Good Question/Topic? 0
  • +

Replies To: Gridview

#2 mido911  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 89
  • Joined: 29-October 10

Re: Gridview

Posted 07 November 2010 - 01:24 PM

:helpsmilie: :helpsmilie:

This post has been edited by mido911: 07 November 2010 - 01:31 PM

Was This Post Helpful? 0
  • +
  • -

#3 BrairMoss  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 81
  • Joined: 13-April 10

Re: Gridview

Posted 07 November 2010 - 03:24 PM

Alright I was able to solve the problem using C# in ASP.NET.

The problem with the gridview is that you do not want to specify the column name, but rather the label inside. If you convert the Name column into a template field, and then edit template and find out what the "ItemTemplate" is, this will help. It will most likely be a label of some generic name.

In your event for SelectedIndexChanged you can then pull the data out of that label. Here is your problem, you found nothing by that column name I am assuming.

Anyway, if you typecast the line of code, you should be able to access a .Text property and be able to set that to a label outside.

I did a quick little test and found that the name in my grid view was populated using "Label2" so my line of code looked like this:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtName.Text = ((Label)GridView1.SelectedRow.FindControl("Label2")).Text;
        }



Basically, I am typecasting the object that it returned as a label, and then access the .Text.

//BrairMoss
Was This Post Helpful? 2
  • +
  • -

#4 mido911  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 89
  • Joined: 29-October 10

Re: Gridview

Posted 07 November 2010 - 10:27 PM

I don't understand what is txtName.Text and Label2 why not findcontrol("name")??? Need some more explanation please
Was This Post Helpful? 0
  • +
  • -

#5 BrairMoss  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 81
  • Joined: 13-April 10

Re: Gridview

Posted 08 November 2010 - 03:12 PM

View Postmido911, on 07 November 2010 - 09:27 PM, said:

I don't understand what is txtName.Text and Label2 why not findcontrol("name")??? Need some more explanation please



the txtName.Text is just the name of the textbox I am putting the string in. Replace this with whatever you are displaying in.

For the control, you are not finding the column of the name, but rather the control it displays it on.

A grid view is just a collection of labels and textboxes databound to show what you need them to from the database. So what you need is the name of the label, not the name of the database field.
Was This Post Helpful? 0
  • +
  • -

#6 thinkdevcode  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 27
  • Joined: 24-September 10

Re: Gridview

Posted 12 November 2010 - 06:39 PM

GridViewRow row = CustomersGridView.SelectedRow;
row.Cells[0].Text


0 index = 1st column
1 index = 2nd column, etc, etc.

Its better to do it this way than BrairMosses way so your not manually finding the label controls id every time.
Was This Post Helpful? 1
  • +
  • -

#7 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: Gridview

Posted 12 November 2010 - 09:46 PM

Of course using the FindControl method, if you happen to change the order of your columns then you don't need to go back and change your code to point to the correct cell index
Was This Post Helpful? 0
  • +
  • -

#8 thinkdevcode  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 27
  • Joined: 24-September 10

Re: Gridview

Posted 13 November 2010 - 10:37 PM

View PostNakor, on 12 November 2010 - 08:46 PM, said:

Of course using the FindControl method, if you happen to change the order of your columns then you don't need to go back and change your code to point to the correct cell index


Label ID's change too.
Was This Post Helpful? 0
  • +
  • -

#9 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: Gridview

Posted 14 November 2010 - 12:02 AM

I usually find I'm a lot more likely to change the columns in a grid, either by adding a column I hadn't thought I needed, or removing a column that I don't need, than I am to just randomly change the id on a label, but hey, that's just me.
Was This Post Helpful? 0
  • +
  • -

#10 BrairMoss  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 81
  • Joined: 13-April 10

Re: Gridview

Posted 14 November 2010 - 07:05 AM

Yeah, I guess using the rows would work as well, providing you don't play on changing the gridview anymore.

If you go into the template control, and say change the label once to "lblGridName" or something, then you can refer to it in the code without worry of it ever really changing. Even leaving it label2, there isn't going to be another label2, and I highly doubt that you would write the code, then change the label name.

I don't know, I find the FindControl method personally better, simply for not changing/removing columns.

Personal Preference.

Doesn't writting BrairMosses sorta mean there is more then one of me?

//BrairMoss
Was This Post Helpful? 0
  • +
  • -

#11 thinkdevcode  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 27
  • Joined: 24-September 10

Re: Gridview

Posted 15 November 2010 - 08:14 AM

View PostNakor, on 13 November 2010 - 11:02 PM, said:

I usually find I'm a lot more likely to change the columns in a grid, either by adding a column I hadn't thought I needed, or removing a column that I don't need, than I am to just randomly change the id on a label, but hey, that's just me.


Thats if you are using a custom gridview and manually getting/setting the controls values in the grid... Having the gridview automatically build the table and set the labels for you - if you change the columns, the corresponding id's will change as well. This is all considering you add a column to someplace before the end. If your just appending columns then really either way will work and gets the job done regardless. I use FindControl when im manually building my gridviews, but if im not i'd rather use the way I described above.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1