Sunday 10 March 2013

C#- Change Gridview Column Row Color Dynamically Based on Condition in Asp.net

By: Pradeep Chava Mar 10, 2013
Categories: Asp.net, C#.Net, General, Gridview, VB.NET
Introduction:

Here I will explain how to change gridview column, row back color dynamically based on particular condition in asp.net using c#, vb.net.

Description:

In previous articles I explained Display Gridview Columns as Rows, SQL Server Convert Rows to columns, Set Gridview column width dynamically, jQuery Menu with Bounce Effect, Different type of constraints in SQL Server and many articles relating to jQuery, asp.net, CSS. Now I will explain how to change gridview column background color dynamically based on particular condition in asp.net using c#, vb.net.

To change gridview column background color dynamically we need to write the code like as shown below

C# Code

protected void gvdata_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].CssClass = "gridcss";
e.Row.Cells[2].CssClass = "gridcss";
}
}
VB Code

protected void gvdata_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells(0).CssClass = " columncss"
e.Row.Cells(2).CssClass = " columncss"
}
}
If you want to know more ways to change column width style check this article

Set Gridview Column width dynamically in asp.net
If you want to see it in complete example check below code




Convert Gridview Columns as Rows in Asp.net







Now in code behind add the following namespace references

C# Code

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
After that write the following code in code behind

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridviewData();
}
}
protected void BindGridviewData()
{
DataTable dt = new DataTable();
dt.Columns.Add("UserId", typeof(Int32));
dt.Columns.Add("UserName", typeof(string));
dt.Columns.Add("Education", typeof(string));
dt.Columns.Add("Location", typeof(string));
DataRow dtrow = dt.NewRow(); // Create New Row
dtrow["UserId"] = 1; //Bind Data to Columns
dtrow["UserName"] = "Pradeep.Chava";
dtrow["Education"] = "B.Tech";
dtrow["Location"] = "Khammam";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow(); // Create New Row
dtrow["UserId"] = 2; //Bind Data to Columns
dtrow["UserName"] = "Praveen";
dtrow["Education"] = "B.Tech";
dtrow["Location"] = "Khammam";
dt.Rows.Add(dtrow);
gvdata.DataSource = dt;
gvdata.DataBind();
}
protected void gvdata_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].CssClass = "gridcss";
e.Row.Cells[2].CssClass = "gridcss";
}
}
VB.NET Code

Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGridviewData()
End If
End Sub
Protected Sub BindGridviewData()
Dim dt As New DataTable()
dt.Columns.Add("UserId", GetType(Int32))
dt.Columns.Add("UserName", GetType(String))
dt.Columns.Add("Education", GetType(String))
dt.Columns.Add("Location", GetType(String))
Dim dtrow As DataRow = dt.NewRow()
' Create New Row
dtrow("UserId") = 1
'Bind Data to Columns
dtrow("UserName") = "Pradeep.Chava"
dtrow("Education") = "B.Tech"
dtrow("Location") = "Khammam"
dt.Rows.Add(dtrow)
dtrow = dt.NewRow()
' Create New Row
dtrow("UserId") = 2
'Bind Data to Columns
dtrow("UserName") = "Praveen"
dtrow("Education") = "B.tech"
dtrow("Location") = "Khammam"
dt.Rows.Add(dtrow)
gvdata.DataSource = dt
gvdata.DataBind()
End Sub
Protected Sub gvdata_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Cells(0).CssClass = "gridcss"
e.Row.Cells(2).CssClass = "gridcss"
End If
End Sub
End Class

No comments:

Post a Comment