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.
Gridview
Page 1 of 110 Replies - 2618 Views - Last Post: 15 November 2010 - 08:14 AM
Replies To: Gridview
#2
Re: Gridview
Posted 07 November 2010 - 01:24 PM
This post has been edited by mido911: 07 November 2010 - 01:31 PM
#3
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:
Basically, I am typecasting the object that it returned as a label, and then access the .Text.
//BrairMoss
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
#4
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
#5
Re: Gridview
Posted 08 November 2010 - 03:12 PM
mido911, 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.
#6
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.
#7
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
#8
Re: Gridview
Posted 13 November 2010 - 10:37 PM
#9
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.
#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
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
#11
Re: Gridview
Posted 15 November 2010 - 08:14 AM
Nakor, 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.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|