string FeeId = grdvw.Rows[index].Cells[0].Text;
Is this correct way to retrieve the content of the cell in the selected row? If I use this, I do not get value. So, please suggest the best way.
string FeeId = grdvw.Rows[index].Cells[0].Text;
Is this correct way to retrieve the content of the cell in the selected row? If I use this, I do not get value. So, please suggest the best way.
Yes, that's a way to do it, you can also do it like:
string FeeId = grdvw.Rows[index].Cells[0].Value;
Also, if you want to get the values from all the rows you could iterate through the Datagridview like this:
foreach (DataGridViewRow row in grdvw.Rows)
{
string column0 = row.Cells[0].Value;
string column1 = row.Cells[1].Value;
//More code here
}
I am getting the value when I try. Here is a sample where I am adding Rows and Columns at runtime:
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataColumn dCol1 = new DataColumn("Col1", typeof(System.String));
DataColumn dCol2 = new DataColumn("Col2", typeof(System.String));
dt.Columns.Add(dCol1);
dt.Columns.Add(dCol2);
for (int i = 0; i < 2; i++)
{
DataRow row1 = dt.NewRow();
row1["Col1"] = "One";
row1["Col2"] = "Two";
dt.Rows.Add(row1);
}
foreach (DataColumn col in dt.Columns)
{
BoundField bField = new BoundField();
bField.DataField = col.ColumnName;
bField.HeaderText = col.ColumnName;
GridView1.Columns.Add(bField);
}
GridView1.DataSource = dt;
//Bind the datatable with the GridView.
GridView1.DataBind();
string str = GridView1.Rows[0].Cells[0].Text;
}
}
string FeeId = (grdvw.Rows[index].Cells[0].FindControl("lblfee") as Label).Text;
if we use like this we get the answer