moe
moe

Reputation: 5249

how to get values in GridView using C#

How can I access a value in a GridView, in this case i have a target_date in there and would like to get that value and convert it to string. Here is what I have:

foreach (GridViewRow gr in GridView1.Rows)
{
   CheckBox cb = (CheckBox)gr.FindControl("chkItem");
   if (cb.Checked)
   {
      string strTargetDate = "???"; // TODO
   }
}

Upvotes: 0

Views: 403

Answers (2)

TTT
TTT

Reputation: 1885

The must be something like GridView1.Rows[(int)rowIndex].Cells[(int)ColIndex].Value; or GridView1.Rows[(int)rowIndex].Cells[(string)ColName].Value;

Upvotes: 0

Sirwan Afifi
Sirwan Afifi

Reputation: 10824

use this code :

foreach (GridViewRow gr in GridView1.Rows)
{
   var cells = gr.Cells;
   CheckBox cb = (CheckBox)gr.FindControl("chkItem");
   if (cb.Checked)
   {
      string strTargetDate = cells[0].Text;
   }
}

Upvotes: 2

Related Questions