<%# Container.DataItemIndex+1 %>
2,4 row is hiding.
after hiding 2 rows it will be 1,3,5,67 like that
how to correct this in 1,2,3,4,5 like that
<%# Container.DataItemIndex+1 %>
2,4 row is hiding.
after hiding 2 rows it will be 1,3,5,67 like that
how to correct this in 1,2,3,4,5 like that
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Deepak TewatiaRank #143 · 15.9K reputationPosted Dec 18, 2022, 11:20 AM EST
Hi lijo,
Based on your problem statement to fix the row numbers after hiding some rows, you can use the RowDataBound event to update the row numbers in the template field. Use the below code snippet to do this:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { // Update the row numbers after hiding some rows int rowNumber = e.Row.RowIndex + 1; if (e.Row.RowIndex > 1) { rowNumber -= 2; } if (e.Row.RowIndex > 4) { rowNumber--; } Label lblRowNumber = (Label)e.Row.FindControl("lblRowNumber"); lblRowNumber.Text = rowNumber.ToString(); } }we are using a Label control with an ID of "lblRowNumber" in the template field to display the row number. You can adjust the code to fit your specific requirements.
Aravind GovindarajRank #430 · 5.8K reputationPosted Dec 18, 2022, 5:05 AM EST
Create a RowNumber as a property and apply the logic before getting into the grid view. Change the code to use new Property RowNumber instead of <%# Container.DataItemIndex+1 %>