Hi trims30,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="GridPmDue" runat="server" AllowPaging="true" OnPageIndexChanging="GridPmDue_PageIndexChanging"
PageSize="5">
</asp:GridView>
<asp:Label ID="lblProgName" runat="server"></asp:Label>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
public DataTable dt = new DataTable();
public string[,] EquipPm = new string[11, 4];
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
EquipPm[0, 0] = "John";
EquipPm[0, 1] = "1";
EquipPm[0, 2] = "Dallas";
EquipPm[0, 3] = "N";
EquipPm[1, 0] = "John";
EquipPm[1, 1] = "2y";
EquipPm[1, 2] = "Kansas city";
EquipPm[1, 3] = "Y";
EquipPm[2, 0] = "John";
EquipPm[2, 1] = "3";
EquipPm[2, 2] = "Dallas";
EquipPm[2, 3] = "N";
EquipPm[3, 0] = "JohnY";
EquipPm[3, 1] = "4y";
EquipPm[3, 2] = "Dallas";
EquipPm[3, 3] = "Y";
EquipPm[4, 0] = "John";
EquipPm[4, 1] = "5";
EquipPm[4, 2] = "Dallas";
EquipPm[4, 3] = "N";
EquipPm[5, 0] = "John";
EquipPm[5, 1] = "6";
EquipPm[5, 2] = "Dallas";
EquipPm[5, 3] = "N";
EquipPm[6, 0] = "John";
EquipPm[6, 1] = "7y";
EquipPm[6, 2] = "Dallas";
EquipPm[6, 3] = "Y";
EquipPm[7, 0] = "John";
EquipPm[7, 1] = "8y";
EquipPm[7, 2] = "Dallas";
EquipPm[7, 3] = "Y";
EquipPm[8, 0] = "John";
EquipPm[8, 1] = "9y";
EquipPm[8, 2] = "Dallas";
EquipPm[8, 3] = "Y";
EquipPm[9, 0] = "John";
EquipPm[9, 1] = "10";
EquipPm[9, 2] = "Dallas";
EquipPm[9, 3] = "N";
EquipPm[10, 0] = "John";
EquipPm[10, 1] = "11";
EquipPm[10, 2] = "Dallas";
EquipPm[10, 3] = "Y";
dt.Columns.Add("EquipId", Type.GetType("System.String"));
dt.Columns.Add("EquipName", Type.GetType("System.String"));
dt.Columns.Add("EqSerial", Type.GetType("System.String"));
dt.Columns.Add("IsSched", Type.GetType("System.String"));
for (int i = 0; i <= 10; i++)
{
dt.Rows.Add();
dt.Rows[dt.Rows.Count - 1]["EquipId"] = EquipPm[i, 0];
dt.Rows[dt.Rows.Count - 1]["EquipName"] = EquipPm[i, 1];
dt.Rows[dt.Rows.Count - 1]["EqSerial"] = EquipPm[i, 2];
dt.Rows[dt.Rows.Count - 1]["IsSched"] = EquipPm[i, 3];
}
GridPmDue.DataSource = dt;
GridPmDue.DataBind();
//GridPmDue.Columns[4].Visible = false;
//lblProgName.Text = Session["Company"].ToString();
}
protected void GridPmDue_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridPmDue.PageIndex = e.NewPageIndex;
BindGrid();
}
protected void GridPmDue_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (TableCell cell in e.Row.Cells)
{
if (e.Row.Cells[3].Text == "Y")
{
cell.BackColor = System.Drawing.Color.LightGreen;
}
else
{
cell.BackColor = System.Drawing.Color.LightPink;
}
}
}
}
VB.Net
Public dt As DataTable = New DataTable()
Public EquipPm As String(,) = New String(10, 3) {}
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGrid()
End If
End Sub
Private Sub BindGrid()
EquipPm(0, 0) = "John"
EquipPm(0, 1) = "1"
EquipPm(0, 2) = "Dallas"
EquipPm(0, 3) = "N"
EquipPm(1, 0) = "John"
EquipPm(1, 1) = "2y"
EquipPm(1, 2) = "Kansas city"
EquipPm(1, 3) = "Y"
EquipPm(2, 0) = "John"
EquipPm(2, 1) = "3"
EquipPm(2, 2) = "Dallas"
EquipPm(2, 3) = "N"
EquipPm(3, 0) = "JohnY"
EquipPm(3, 1) = "4y"
EquipPm(3, 2) = "Dallas"
EquipPm(3, 3) = "Y"
EquipPm(4, 0) = "John"
EquipPm(4, 1) = "5"
EquipPm(4, 2) = "Dallas"
EquipPm(4, 3) = "N"
EquipPm(5, 0) = "John"
EquipPm(5, 1) = "6"
EquipPm(5, 2) = "Dallas"
EquipPm(5, 3) = "N"
EquipPm(6, 0) = "John"
EquipPm(6, 1) = "7y"
EquipPm(6, 2) = "Dallas"
EquipPm(6, 3) = "Y"
EquipPm(7, 0) = "John"
EquipPm(7, 1) = "8y"
EquipPm(7, 2) = "Dallas"
EquipPm(7, 3) = "Y"
EquipPm(8, 0) = "John"
EquipPm(8, 1) = "9y"
EquipPm(8, 2) = "Dallas"
EquipPm(8, 3) = "Y"
EquipPm(9, 0) = "John"
EquipPm(9, 1) = "10"
EquipPm(9, 2) = "Dallas"
EquipPm(9, 3) = "N"
EquipPm(10, 0) = "John"
EquipPm(10, 1) = "11"
EquipPm(10, 2) = "Dallas"
EquipPm(10, 3) = "Y"
dt.Columns.Add("EquipId", Type.[GetType]("System.String"))
dt.Columns.Add("EquipName", Type.[GetType]("System.String"))
dt.Columns.Add("EqSerial", Type.[GetType]("System.String"))
dt.Columns.Add("IsSched", Type.[GetType]("System.String"))
For i As Integer = 0 To 10
dt.Rows.Add()
dt.Rows(dt.Rows.Count - 1)("EquipId") = EquipPm(i, 0)
dt.Rows(dt.Rows.Count - 1)("EquipName") = EquipPm(i, 1)
dt.Rows(dt.Rows.Count - 1)("EqSerial") = EquipPm(i, 2)
dt.Rows(dt.Rows.Count - 1)("IsSched") = EquipPm(i, 3)
Next
GridPmDue.DataSource = dt
GridPmDue.DataBind()
End Sub
Protected Sub GridPmDue_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
GridPmDue.PageIndex = e.NewPageIndex
BindGrid()
End Sub
Protected Sub GridPmDue_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
For Each cell As TableCell In e.Row.Cells
If e.Row.Cells(3).Text = "Y" Then
cell.BackColor = System.Drawing.Color.LightGreen
Else
cell.BackColor = System.Drawing.Color.LightPink
End If
Next
End If
End Sub
Screenshot