Guruji Point - Code You Want To Write

  • Home
  • Asp.Net
  • WCF
  • SQL
  • Type Script
  • AngularJS
  • Tech News
  • Blog
  • Earn Online

Sunday, 15 January 2017

Change Grid View Row Color Dynamically Based on Condition

 Asp Grid, asp.net, C#, Condition on Grid, Dynamic Grid Row, Grid View, Row Color     No comments   

Introduction

In this article i will explain about how to change the Grid row color dynamically based on condition.

Practice

For performing Grid operation you can directly drag and drop grid control from Toolbox and do code according your need or for better understanding you can copy the code which i am sharing below.

Design View ___

    <asp:GridView ID="gvEmployeeInfo" runat="server" AutoGenerateColumns="false"
                AllowPaging="true" OnRowDataBound="gvEmployeeInfo_RowDataBound">
                <Columns>
                    <asp:BoundField DataField="EmpId" HeaderText="Employee Id" />
                    <asp:BoundField DataField="Name" HeaderText="Employee Name" />
                    <asp:BoundField DataField="Salary" HeaderText="Salary" />
                </Columns>
            </asp:GridView>



In above Grid Code I used OnRowDataBound  Event to handle dynamic change on Grid Row. RowDataBound Event is fire while DataSource assigning to Grid and when the GridView row selection change performs during the postback

Code View ___

protected void Page_Load(object sender, EventArgs e)
{
BindGrid();
}

private void BindGrid()
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Employee;Integrated Security=True");
SqlCommand cmd = new SqlCommand("SELECT * FROM employee", con);
con.Open();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
gvEmployeeInfo.DataSource = ds;
gvEmployeeInfo.DataBind();
}

protected void gvEmployeeInfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (Convert.ToInt32(e.Row.Cells[2].Text) > 15000)
{
e.Row.BackColor = Color.AliceBlue;
}
else if (Convert.ToInt32(e.Row.Cells[2].Text) < 50000)
{
e.Row.BackColor = Color.Red;
}
else
{
e.Row.BackColor = Color.LightGray;
}

}
}

In the above code i directly fetch the data from database using simple sql query but it is not a good way to perform big operation.
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Facebook Updates

Guruji Point

Categories

Comman table Expression in SQL Dynamic Grid Row IQueryable in c# Learn AngularJS Scope_Identity() Use of indexes comma seperated string in sql row_number() and dense_rank() salary table structure. while loop in sql why do we need cursor why tuple used

About Us

This blog is about the Programming ,Tech News and some intresting facts related Contents. Here you can find fundamental and expert level topic of programming languages very practically related to C# , Asp.Net ,Sql-Server, JavaScript, Jquery, WebServices And also Web-Api, WCF ,WPF, Angular Js.

Contact Us

Email Us - gurujipoints@gmail.com
Contact- 8077657477

Reach Us

Copyright © Guruji Point - Code You Want To Write