How to get text selected row of grid view
how to get the text of a particular cell of selected row in grid view.
Re: How to get text selected row of grid view
Code:
Dim str As String = myGridView.SelectedRow.Cells(index).Text
Re: How to get text selected row of grid view
Thank u for your reply
same thing i have tried.like........
protected void gv1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
string strText = gv1.SelectedRow.Cells[0].Text.ToString();
}
but it is giving error like "object reference not set to an instance of object".
in which event i should write this code.............
how to get text of a particular cell of selected row in grid view
here is my code........
protected void myGridview_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
string strText = myGridview.SelectedRow.Cells[0].Text.ToString();
}
it is giving error like "object reference not set to an instance of object".
in which event i should write this code.............
Re: how to get text of a particular cell of selected row in grid view
You should first check if the selected row is a valid object.
Code:
// Get the row
Row selectedRow = myGridView.SelectedRow;
// Check if we have a valid row
if (selectedRow != null)
{
// Get the first column (only directly call to a column index if you are SURE it exists)
string text = selectedRow.Cells[0].Text;
}
Disclaimer:
I wrote this out of my head, so it's possible it contains some typos.
Re: How to get text selected row of grid view
Which reference is null? gv1? gv1.SelectedRow? gv1.SelectedRow.Cells? gv1.SelectedRow.Cells[0]?
Also, why are you calling the ToString method of a String?
Re: How to get text selected row of grid view
Quote:
Code:
.... gv1.SelectedRow.Cells[0]?
Why are you using zero instead of the index of the column you need to get ? or do you want to get index zero ??
The other point is: Using this web control the event SelectedIndexChanging you are using is called before the change occurs. So in the first occurance of it this event will not have a selected row so it is null and you will have an Exception thrown. On the other side,you cannot use SelectedRow.Cells as you will get no or a former selection which is of no use. Using this event you need to do
Code:
gv1.Rows[e.NewSelectedIndex].Cells[clumnIndex].Text
otherwise it wouldn't work
This event is used before changing the selection !! So normally you use it for checking and eventually cancelling the selections change. If you want to use selectedRow you need to use the SelectedIndexChanged event
Hope this ends your troubles :D
Re: how to get text of a particular cell of selected row in grid view
Please dont double Post. the solution I have done in your other post. You are using the wrong event ! See your other post for why and how to handle that.