mmpranathi
January 6th, 2008, 11:59 PM
how to get the text of a particular cell of selected row in grid view.
|
Click to See Complete Forum and Search --> : how to get text of a particular cell of selected row in grid view mmpranathi January 6th, 2008, 11:59 PM how to get the text of a particular cell of selected row in grid view. jmcilhinney January 7th, 2008, 12:19 AM Dim str As String = myGridView.SelectedRow.Cells(index).Text mmpranathi January 7th, 2008, 12:31 AM 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............. mmpranathi January 7th, 2008, 01:26 AM 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............. Tischnoetentoet January 7th, 2008, 02:15 AM You should first check if the selected row is a valid object. // 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. jmcilhinney January 7th, 2008, 05:47 AM 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? JonnyPoet January 7th, 2008, 12:34 PM .... 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 gv1.Rows[e.NewSelectedIndex].Cells[clumnIndex].Textotherwise 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 JonnyPoet January 7th, 2008, 01:05 PM 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. codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |