GridView SelectedRow

G

Guest

Hi,

I've a radiobutton in my gridview and I want to dislpay the value in a cell
of the selected value on the RadioButton click using this code :
TextBox.Text = MyGridView.SelectedRow.Cells[1].ToString();
but it's giving me this error:
"Object reference not set to an instance of an object"
how can I fix it


Thanks
 
G

Guest

I'm doing it in a CheckedChanged event of a RadioButton in a TemplateField of
MyGridView

protected void MyRadioButton_CheckedChanged(object sender, EventArgs e)
{
TextBox.Text = MyGridView.SelectedRow.Cells[1].ToString();
}

Patrick.O.Ige said:
Badis maybe try posting some more snippet code
Patrick

Badis said:
Hi,

I've a radiobutton in my gridview and I want to dislpay the value in a cell
of the selected value on the RadioButton click using this code :
TextBox.Text = MyGridView.SelectedRow.Cells[1].ToString();
but it's giving me this error:
"Object reference not set to an instance of an object"
how can I fix it


Thanks
 
P

Patrick.O.Ige

Badis where is the textbox?
Post your aspx page etc..
Elaborate more
Patrick



Badis said:
I'm doing it in a CheckedChanged event of a RadioButton in a TemplateField of
MyGridView

protected void MyRadioButton_CheckedChanged(object sender, EventArgs e)
{
TextBox.Text = MyGridView.SelectedRow.Cells[1].ToString();
}

Patrick.O.Ige said:
Badis maybe try posting some more snippet code
Patrick

Badis said:
Hi,

I've a radiobutton in my gridview and I want to dislpay the value in a cell
of the selected value on the RadioButton click using this code :
TextBox.Text = MyGridView.SelectedRow.Cells[1].ToString();
but it's giving me this error:
"Object reference not set to an instance of an object"
how can I fix it


Thanks
 
P

Patrick.O.Ige

Also try looking at:
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.selectedrow.aspx
Hope that helps
Patrick


Badis said:
I'm doing it in a CheckedChanged event of a RadioButton in a TemplateField of
MyGridView

protected void MyRadioButton_CheckedChanged(object sender, EventArgs e)
{
TextBox.Text = MyGridView.SelectedRow.Cells[1].ToString();
}

Patrick.O.Ige said:
Badis maybe try posting some more snippet code
Patrick

Badis said:
Hi,

I've a radiobutton in my gridview and I want to dislpay the value in a cell
of the selected value on the RadioButton click using this code :
TextBox.Text = MyGridView.SelectedRow.Cells[1].ToString();
but it's giving me this error:
"Object reference not set to an instance of an object"
how can I fix it


Thanks
 
G

Guest

It doesn't mater because even if I do this:
string MyString = MyGridView.SelectedRow.Cells[1].ToString();
it will give me the same error!!! and it looks like in the click event of
the RadioButton the "SelectedRow" is not set or equal to null !!!?

Patrick.O.Ige said:
Badis where is the textbox?
Post your aspx page etc..
Elaborate more
Patrick



Badis said:
I'm doing it in a CheckedChanged event of a RadioButton in a TemplateField of
MyGridView

protected void MyRadioButton_CheckedChanged(object sender, EventArgs e)
{
TextBox.Text = MyGridView.SelectedRow.Cells[1].ToString();
}

Patrick.O.Ige said:
Badis maybe try posting some more snippet code
Patrick

Hi,

I've a radiobutton in my gridview and I want to dislpay the value in a
cell
of the selected value on the RadioButton click using this code :
TextBox.Text = MyGridView.SelectedRow.Cells[1].ToString();
but it's giving me this error:
"Object reference not set to an instance of an object"
how can I fix it


Thanks
 
P

Patrick.O.Ige

Badis have you looked at the msdn link i posted earlier?
Patrick

Badis said:
It doesn't mater because even if I do this:
string MyString = MyGridView.SelectedRow.Cells[1].ToString();
it will give me the same error!!! and it looks like in the click event of
the RadioButton the "SelectedRow" is not set or equal to null !!!?

Patrick.O.Ige said:
Badis where is the textbox?
Post your aspx page etc..
Elaborate more
Patrick



Badis said:
I'm doing it in a CheckedChanged event of a RadioButton in a
TemplateField
of
MyGridView

protected void MyRadioButton_CheckedChanged(object sender, EventArgs e)
{
TextBox.Text = MyGridView.SelectedRow.Cells[1].ToString();
}

:

Badis maybe try posting some more snippet code
Patrick

Hi,

I've a radiobutton in my gridview and I want to dislpay the value in a
cell
of the selected value on the RadioButton click using this code :
TextBox.Text = MyGridView.SelectedRow.Cells[1].ToString();
but it's giving me this error:
"Object reference not set to an instance of an object"
how can I fix it


Thanks
 
P

Phil H

Badis said:
I'm doing it in a CheckedChanged event of a RadioButton in a TemplateField of
MyGridView

protected void MyRadioButton_CheckedChanged(object sender, EventArgs e)
{
TextBox.Text = MyGridView.SelectedRow.Cells[1].ToString();
}

Hi,

You need to make sure that the SelectedIndex property is not set to -1
when you try to access the SelectedRow.

There has to be a command type control somewhere in your template (e.g.
button or LinkButton) that generates a postback event and is assigned
the string value "Select" to its CommandName property (a RadioButton
control can't be used for this). That will raise a SelectIndexChanged
event and the SelectedRow property will then point to an actual row of
the grid.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top