954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Accessing Datagrid selected row cell value (c# .NET)

Hi all!

I would like some help for getting a value out of a selected row of a datagrid.

To do this i used the following code but it returns an empty string
(DG is my datagrid)

string val = DG.SelectedItem.Cells[1].Text;
Response.Write("*"+val+:*");

The rouput of this its: **
although there is a value on the cell.
The datagrid is created and filled in the Page_Load when !Page.IsPostBack and the above code is called when a button is pressed.

Does anyone know how to fix this?

Best regards

George

madcloud
Newbie Poster
4 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

Cells are zero baseed so the first cell is Cell[0]. Cell[1] is the second column. Maybe that's your issue?

hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
 

No unfortunately its not this. I have tried to display something using all the indexes but it doesn work.
In the select event I have also tried using:
e.Item.Cells[1].Text; and yet again its displayed empy.
Any ideas?

Best regardes,

George

madcloud
Newbie Poster
4 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

I found the solution:


string val = ((Label)DG.SelectedItem.Cells[1].FindControl("id")).Text;

madcloud
Newbie Poster
4 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 
I found the solution: string val = ((Label)DG.SelectedItem.Cells[1].FindControl("id")).Text;




here is how.

this is the event functionprivatevoid Select_myDataGrid(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
lblLabel.Text = e.Item.Cells[1].Text;

}
e.Item.Cells[1].Text;

it tested it and it worked like a charm.:)

zmegh
Newbie Poster
1 post since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

Hi...
Try This may this will work

int Rowcount = dgTicket.Rows.Count ;

this.dgTicket.Rows[Rowcount].Cells[1].Text;// add the array item of //which column do u want the text
just add this two lines of code may this will work it out..

Kapil Vaichalka
Newbie Poster
2 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

how to insert the value to the datagrid view

spoorti
Newbie Poster
1 post since Feb 2010
Reputation Points: 10
Solved Threads: 0
 
how to insert the value to the datagrid view


hi spoorti you can insert the values into into data grid by simple sql command...there are multiple ways to insert the values
simple one is to bind the datasource.

can u show me the exact code what u have written for that i can help you...

Kapil Vaichalka
Newbie Poster
2 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You