This article will show you how you can make the row Italic of
a gridview based on condition or data in asp.net using c#.net.
Some of my previous articles are as follows: Bind HyperLink Control To GridView In Asp.net
Using C#, Enable Or Disable All GridView Button On
Check Of CheckBox Using jQuery, Bind HyperLink Control With URL Parameter To
GridView In Asp.net Using C#, Ajax GridView Shorting Without Page Refresh
Using jQuery In Asp.net and C#.Net, How To Use Hyper Link Tag In Gridview In
Asp.Net C#, Enable Or Disable GridView Button By Row
CheckBox Using jQuery In Asp.Net, Browse, Read and Populate or Show or Bind CSV
File Data In GridView Using C#.Net in Asp.net, Bind Class Property to GeidView Using C#.net
In Asp.net, Gridview Auto Generate Row Number In Asp.Net
Using C#.Net, Bind TextBox Control Inside TemplateField Of
GridView Using C#.Net In Asp.Net, Short GridView Data From Header Using C#.Net
In Asp.Net, Export GridView Or Table Data Into PDF By
C#.Net In Asp.Net Using jQuery, Asp.net
Gridview Row Bold Based on Condition or Data Using C#.Net, Asp.net
Gridview Row Color Change Based on Condition or Data Using C#.Net.
So for this article first we will create a new asp.net
application and add a gridview control on the page and bind the fields.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm10.aspx.cs" Inherits="WebApplication7.WebForm10" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Asp.net Gridview
Row Italic Based on Condition or Data
Using C#.Net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1"
runat="server" AutoGenerateColumns="False" CssClass="tablesorter" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="CountryName"
HeaderText="Country Name"
/>
<asp:BoundField DataField="Population"
HeaderText="Population"
/>
<asp:BoundField DataField="Code" HeaderText="Code" />
<asp:BoundField DataField="Status" HeaderText="Status" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
|
Now we will check the code. So here is the code.
using System;
using
System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using
System.Web.UI.WebControls;
namespace WebApplication7
{
public partial class WebForm10 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt =
GetCountryData();
GridView1.DataSource = dt;
GridView1.DataBind();
}
private DataTable GetCountryData()
{
DataTable dt = new DataTable();
dt.Clear();
dt.Columns.Add("Id");
dt.Columns.Add("CountryName");
dt.Columns.Add("Population");
dt.Columns.Add("Code");
dt.Columns.Add("Status");
DataRow dataRow1 =
dt.NewRow();
dataRow1["Id"] = 1;
dataRow1["CountryName"] = "India";
dataRow1["Population"]
= "125 Cr";
dataRow1["Code"] = "IN";
dataRow1["Status"] = "1";
dt.Rows.Add(dataRow1);
DataRow dataRow2 =
dt.NewRow();
dataRow2["Id"] = 2;
dataRow2["CountryName"] = "Pakistan";
dataRow2["Population"] = "50
Cr";
dataRow2["Code"] = "PK";
dataRow2["Status"] = "0";
dt.Rows.Add(dataRow2);
DataRow dataRow3 =
dt.NewRow();
dataRow3["Id"] = 3;
dataRow3["CountryName"] = "United
States";
dataRow3["Population"] = "25
Cr";
dataRow3["Code"] = "US";
dataRow3["Status"] = "1";
dt.Rows.Add(dataRow3);
DataRow dataRow4 =
dt.NewRow();
dataRow4["Id"] = 4;
dataRow4["CountryName"] = "Japan";
dataRow4["Population"] = "20
Cr";
dataRow4["Code"] = "JP";
dataRow4["Status"] = "0";
dt.Rows.Add(dataRow4);
return dt;
}
protected void
GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (Convert.ToInt16(e.Row.Cells[4].Text)
== 1)
{
e.Row.Font.Italic = true;
}
}
}
}
}
|
In above code I have created a data table and bind it with
the gridview control. Now we will generate the RowDataBound event of grid view
control and add use the below code.
protected void
GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (Convert.ToInt16(e.Row.Cells[4].Text)
== 1)
{
e.Row.Font.Italic = true; }
}
}
|
In above code I have checked the row cell value and then
make the row Italic. Now we have done run the application and check the output.
0 comments:
Please let me know your view