Answered by:
GridView RowBound Issue

Question
-
User-1641513764 posted
Hi Forum,
I am getting one issue in my GridView.
In my GridView i am using two events one is OnRowCommand and OnRowDataBound events.
While when the page is loaded first time i am getting the below screenshot
I am using the RowCommand event for highlight color with some condition of that rows.
When i click the "Select" button inside the GridView, the Highlighted color is disappeared.
When i hit the url of that page the highlight rows will be shown.
Plz suggest me this issue.
The
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="EmpId" DataSourceID="SqlDataSource1" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound"> <Columns> <asp:BoundField DataField="EmpId" HeaderText="EmpId" InsertVisible="False" ReadOnly="True" SortExpression="EmpId" /> <asp:BoundField DataField="EmpName" HeaderText="EmpName" SortExpression="EmpName" /> <asp:BoundField DataField="Salary" HeaderText="Salary" SortExpression="Salary" /> <asp:BoundField DataField="DateOfBirth" HeaderText="DateOfBirth" SortExpression="DateOfBirth" DataFormatString="{0:d}" /> <asp:ButtonField CommandName="Select" Text="Select" ButtonType="Button" /> </Columns> </asp:GridView> protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Select") { //Determine the RowIndex of the Row whose Button was clicked. int rowIndex = Convert.ToInt32(e.CommandArgument); //Reference the GridView Row. GridViewRow row = GridView1.Rows[rowIndex]; //Access Cell values. int EmployeeId = Convert.ToInt32(row.Cells[0].Text); string Empname = row.Cells[1].Text; string EmpDOB = row.Cells[3].Text; ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Employee Id: " + EmployeeId + "\\nEmployee Name: " + Empname + "\\DateOfBirth: " + EmpDOB + "');", true); // ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Employee Name: " + Empname + "\\DateOfBirth: " + EmpDOB + "');", true); } } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { // TableCell statuscell = Convert.ToDateTime(e.Row.Cells[3].Text).ToString(); if (Convert.ToDateTime(e.Row.Cells[3].Text) <= DateTime.Today) { e.Row.Cells[3].BackColor = System.Drawing.Color.FromName("#ffccff"); } } }
Thursday, September 15, 2016 11:34 AM
Answers
-
User1724605321 posted
Hi ramasharepoint2011,
What is the issue ?
I am using the RowCommand event for highlight color with some condition of that rows.Refer to below code :
public void yourGridview_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { // do your stuffs here, for example if column risk is your third column: if (e.Row.Cells[2].Text == 'high') { e.Row.BackColor = Color.Red; } } }
When i click the "Select" button inside the GridView, the Highlighted color is disappeared.That is the issue or that is your requirement , if you want to show highlight the current row ,you could use OnSelectedIndexChanged event code below is for your reference :
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" runat="server" AutoGenerateColumns="false" OnSelectedIndexChanged = "OnSelectedIndexChanged"> <Columns> <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" /> <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" /> <asp:ButtonField Text = "Select" CommandName = "Select" /> </Columns> </asp:GridView>
Code behind :
protected void OnSelectedIndexChanged(object sender, EventArgs e) { foreach (GridViewRow row in GridView1.Rows) { if (row.RowIndex == GridView1.SelectedIndex) { row.BackColor = ColorTranslator.FromHtml("#A1DCF2"); } else { row.BackColor = ColorTranslator.FromHtml("#FFFFFF"); } } }
When i hit the url of that page the highlight rows will be shown.Could you please explain more about it ?
Best Regards,
Nan Yu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, September 16, 2016 5:48 AM