Hi
I have attached image and below is the code . Button getting created on first student Hrithvi but not getting created on Srinithya
C#
protected void gr_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType != DataControlRowType.DataRow)
return;
e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
for (int i = 8; i < e.Row.Cells.Count-1; i++)
{
string cellValue = e.Row.Cells[i].Text;
var row = (DataRowView)e.Row.DataItem;
// Create a new LinkButton control
LinkButton button = new LinkButton();
button.Text = cellValue;
//button.OnClientClick = "showModalPopup('" + cellValue + "'); return false;"; // assuming there is a JavaScript function named 'showModalPopup' that takes the cell value as a parameter
// Replace the text of the cell with the LinkButton control
e.Row.Cells[i].Controls.Clear();
if (row[i].ToString().ToUpper() == "A")
{
button.CssClass = "badge badge-danger";
}
else if (row[i].ToString().ToUpper() == "B")
{
button.CssClass = "badge badge-warning";
}
else if (row[i].ToString().ToUpper() == "ND")
{
button.CssClass = "badge badge-secondary";
button.OnClientClick = "return false;";
}
else if (row[i].ToString().ToUpper() == "D")
{
button.CssClass = "badge badge-success";
}
else if (row[i].ToString().ToUpper() == "S")
{
button.CssClass = "badge badge-info";
}
else if (row[i].ToString().ToUpper() == "HB")
{
button.CssClass = "badge badge-primary";
}
e.Row.Cells[i].Controls.Add(button);
}
}
}

Mohamed Azarudeen ZRank #297 · 8.5K reputationPosted May 9, 2023, 8:04 AM EDT
The loop appears to start at index 8, which may not be the appropriate column index for the data, and this appears to be the problem with the code. Additionally, only if the condition is true—which might not be the case for the second row—is the button added. Try changing the code as follows:
protected void gr_RowDataBound(object sender, GridViewRowEventArgs e) { try { if (e.Row.RowType != DataControlRowType.DataRow) return; e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center; for (int i = 0; i < e.Row.Cells.Count - 1; i++) { string cellValue = e.Row.Cells[i].Text; var row = (DataRowView)e.Row.DataItem; // Create a new LinkButton control LinkButton button = new LinkButton(); button.Text = cellValue; //button.OnClientClick = "showModalPopup('" + cellValue + "'); return false;"; // assuming there is a JavaScript function named 'showModalPopup' that takes the cell value as a parameter // Replace the text of the cell with the LinkButton control e.Row.Cells[i].Controls.Clear(); if (row[i].ToString().ToUpper() == "A") { button.CssClass = "badge badge-danger"; } else if (row[i].ToString().ToUpper() == "B") { button.CssClass = "badge badge-warning"; } else if (row[i].ToString().ToUpper() == "ND") { button.CssClass = "badge badge-secondary"; button.OnClientClick = "return false;"; } else if (row[i].ToString().ToUpper() == "D") { button.CssClass = "badge badge-success"; } else if (row[i].ToString().ToUpper() == "S") { button.CssClass = "badge badge-info"; } else if (row[i].ToString().ToUpper() == "HB") { button.CssClass = "badge badge-primary"; } e.Row.Cells[i].Controls.Add(button); } } }Give it a try hope this fixes.
Aravind GovindarajRank #430 · 5.8K reputationPosted May 9, 2023, 8:05 AM EDT
could you please navigate to the developer tool and check is this CSS style sheet works("badge badge-danger"), on top of that please check is this style applied to your html page .
Ramco RamcoRank #462 · 5.4K reputationPosted May 9, 2023, 10:11 AM EDT
Hi Mohammed
On second student CssClass not getting implmented
for (int i = 8; i < e.Row.Cells.Count - 1; i++) { string cellValue = e.Row.Cells[i].Text; var row = (DataRowView)e.Row.DataItem; LinkButton button = new LinkButton(); button.Text = cellValue; if (row[i].ToString().ToUpper().Trim() == "A") { button.CssClass = "badge badge-danger"; } else if (row[i].ToString().ToUpper().Trim() == "B") { button.CssClass = "badge badge-warning"; } else if (row[i].ToString().ToUpper().Trim() == "ND") { button.CssClass = "badge badge-secondary"; button.OnClientClick = "return false;"; } else if (row[i].ToString().ToUpper().Trim() == "D") { button.CssClass = "badge badge-success"; } e.Row.Cells[i].Controls.Add(button); e.Row.Cells[i].HorizontalAlign = HorizontalAlign.Center; }Thanks