Hello All
I Need to bind gridview template label filed in the loop i am getting list
My Grid view UI code :
Code
code logic for Gridview :
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//foreach (GridViewRow gvrow in GridView1.Rows)
//{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label name = (Label)e.Row.Cells[2].FindControl("lblemailtext");
for (int j = 0; j = 0)
//{
// Label name = (Label)GridView1.Rows[countnew].Cells[2].FindControl("lblemailtext");
//foreach (GridViewRow gvrow in GridView1.Rows)
//{
//Label lblEmail = (Label)gvrow.FindControl("lblemailtext");
TableCell cellnetid = e.Row.Cells[3];
BWusername = cellnetid.Text;
TableCell cell = e.Row.Cells[5];
int index = cell.Text.IndexOf('(');
if (index >= 0)
{
cell.Text = cell.Text.Substring(0, index);
}
lssEmailId.Add(dataTable.Rows[j]["NetID"] + "@****.com");
}
}
}
Naimish MakwanaRank #324 · 7.7K reputationPosted Apr 14, 2023, 5:49 AM EDT
It seems like you want to bind the email value to the
lblemailtextlabel inside theGridViewusing theListlssEmailId. You can achieve this by getting the index of the current row usinge.Row.RowIndexand then using that index to get the email value from thelssEmailIdlist.Here's an updated version of your
GridView1_RowDataBoundmethod with the changesprotected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { Label name = (Label)e.Row.Cells[2].FindControl("lblemailtext"); // Get the email value from the lssEmailId list using the current row index string email = lssEmailId[e.Row.RowIndex]; // Bind the email value to the lblemailtext label name.Text = email; // Your other code logic here... } }Note that you need to make sure that the
ListlssEmailIdhas the same number of items as the number of rows in theGridView, otherwise, you will get an index out of range exception.Tuhin PaulRank #7 · 71.9K reputationPosted Apr 15, 2023, 9:30 PM EDT
To set the email value from the list
lssEmailIdto the labellblemailtextin theGridView, you can modify theGridView1_RowDataBoundevent handler.Tuhin PaulRank #7 · 71.9K reputationPosted Apr 15, 2023, 9:30 PM EDT
we are accessing the label lblemailtext using FindControl and setting its Text property to the value of the corresponding item in the lssEmailId list using the RowIndex property of the current row.