none
GridView RowDataBound Issue RRS feed

  • Question

  • 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.

    
    
    
    <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");
                        }
                    }
                    
                
            }

    


    Rama

    Wednesday, September 14, 2016 10:08 AM

Answers

  • Hi Long,

    As far as I know, it seems that it has not a event when i hit the url of that page, I would suggest that you could add a button to show the highlight rows. like this:

    #Page Code:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridViewWithHighlight.aspx.cs" Inherits="ADONETDEMO.GridViewDemo.GridViewWithHighlight" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="EmpId" 
                 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>
    
    
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
    
    
        </div>
        </form>
    </body>
    </html>
    

    #Code Behind

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace ADONETDEMO.GridViewDemo
    {
        public partial class GridViewWithHighlight : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindData();
                }
            }
    
            private void BindData()
            {
                string connstr = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=AspDotNetDemo;Integrated Security=True";
                SqlConnection connection = new SqlConnection(connstr);
                string sqlStrig = @"select * from Employees";
                DataTable dt = new DataTable();
                using (SqlConnection conn = new SqlConnection(connstr))
                {
                    try
                    {
                        connection.Open();
                        SqlDataAdapter sda = new SqlDataAdapter(sqlStrig,conn);
                        sda.Fill(dt);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
    
                GridView1.DataSource = dt;
               int count = dt.Rows.Count;
                GridView1.DataBind();
            }
    
            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);
                }
            }
    
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (Convert.ToDateTime(e.Row.Cells[3].Text) <= DateTime.Today)
                    {
                        e.Row.Cells[3].BackColor = System.Drawing.Color.FromName("#ffccff");
                    }
                }
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                foreach (GridViewRow gr in GridView1.Rows)
                {
                    if (Convert.ToDateTime(gr.Cells[3].Text) <= DateTime.Today)
                    {
                        gr.Cells[3].BackColor = System.Drawing.Color.FromName("#ffccff");
                    }
                }
            }
        }
    }

    In addition, According to your description and code snippet, it's related to asp.net. I would suggest you could post your issue on asp.net forum for support.

    http://forums.asp.net/18.aspx/1?Web+Forms

    Thank you for your understanding.

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, September 15, 2016 2:01 AM
    Moderator