By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,827 Members | 2,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,827 IT Pros & Developers. It's quick & easy.

how to retrieve cell value from grid view using button column in c#

P: 1
I am using grid view and a button column in it using <itemTemplate>
but I am not able to rerive cell value of a selected row from grid view.
I have tried the following to methods in Row_command event of gridview but it gives Null reference exception error while clicking the button in button column of grid view :
Expand|Select|Wrap|Line Numbers
  1.  int index = gdvOdetails.SelectedIndex;
  2.         object oid = (object)gdvOdetails.SelectedDataKey.Values["o_id"];
  3.         txtMsg.Text = oid.ToString();
  4.  
  5.  for (int i = 0; i < gdvOdetails .Rows .Count ; i++)
  6.             {
  7.                 if (gdvOdetails.SelectedIndex == i)
  8.                 {
  9.                     txtMsg .Text  =gdvOdetails.Rows[i].Cells[1].Text; 
  10.                 }
  11.             }
  12.  
can any one help to retrieve cell value of grid in any variable using button column
Feb 5 '08 #1
Share this Question
Share on Google+
2 Replies


Frinavale
Expert Mod 5K+
P: 9,731
I am using grid view and a button column in it using <itemTemplate>
but I am not able to rerive cell value of a selected row from grid view.
I have tried the following to methods in Row_command event of gridview but it gives Null reference exception error while clicking the button in button column of grid view :
Expand|Select|Wrap|Line Numbers
  1.  int index = gdvOdetails.SelectedIndex;
  2.         object oid = (object)gdvOdetails.SelectedDataKey.Values["o_id"];
  3.         txtMsg.Text = oid.ToString();
  4.  
  5.  for (int i = 0; i < gdvOdetails .Rows .Count ; i++)
  6.             {
  7.                 if (gdvOdetails.SelectedIndex == i)
  8.                 {
  9.                     txtMsg .Text  =gdvOdetails.Rows[i].Cells[1].Text; 
  10.                 }
  11.             }
  12.  
can any one help to retrieve cell value of grid in any variable using button column
Unless your button is a Select button, your gdvOdetails.SelectedIndex will be nothing (because no row is selected). Try using a "select" button instead of a command button in your GridView.

-Frinny
Feb 5 '08 #2

Plater
Expert 5K+
P: 7,872
If the button they clicked is IN the row that you want, you can get the row/column indexes from the event.
Feb 5 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.