How to find a cell value

If you find yourself wanting to parse through a dataset for a particular cellvalue and you need to do this programatically, this might help a little..
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewCellValue.aspx.cs"
   Inherits="GridViewCellValue" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title>Untitled Page</title>
   <style type="text/css">
   .Hide{visibility:hidden};
   </style>
</head>
<body>
   <form id="form1" runat="server">
       <div>
           <asp:GridView ID="GridView1" runat="Server" OnRowDataBound="GridView1_RowDataBound">
           </asp:GridView>
       </div>
   </form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class GridViewCellValue : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            BindGridViewView();
        }


    }
    private void BindGridViewView()
    {
        if (Session["strTemp"] != null)
        {

            GridView1.DataSource = Session["strTemp"] as DataTable;
            GridView1.DataBind();

        }
        else
        {
            GridView1.DataSource = GetCustomMadeDataTable();
            GridView1.DataBind();
        }
    }
    public DataTable GetCustomMadeDataTable()
    {
        //Create a new DataTable object
        System.Data.DataTable objDataTable = new System.Data.DataTable();
        //Create three columns with string as their type
        objDataTable.Columns.Add("ISBN", typeof(int));
        objDataTable.Columns.Add("Title", typeof(string));
        objDataTable.Columns.Add("Publisher", typeof(string));
        objDataTable.Columns.Add("Year", typeof(string));
        DataColumn[] dcPk = new DataColumn[1];
        dcPk[0] = objDataTable.Columns["ISBN"];
        objDataTable.PrimaryKey = dcPk;
        objDataTable.Columns["ISBN"].AutoIncrement = true;
        objDataTable.Columns["ISBN"].AutoIncrementSeed = 1;
        //Adding some data in the rows of this DataTable
        DataRow dr;
        for (int i = 1; i <= 5; i++)
        {
            dr = objDataTable.NewRow();
            dr[1] = "Title" + i.ToString();
            dr[2] = "Publisher" + i.ToString();
            dr[3] = "200" + i.ToString();
            objDataTable.Rows.Add(dr);
        }
        for (int i = 6; i <= 8; i++)
        {
            dr = objDataTable.NewRow();
            dr[1] = "Computer" + i.ToString();
            dr[2] = "TMH" + i.ToString();
            dr[3] = "200" + i.ToString();
            objDataTable.Rows.Add(dr);
        }
        Session["strTemp"] = objDataTable;
        return objDataTable;
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //Create the array/collection of rows containing your data
        GridViewRowCollection data = GridView1.Rows;

        //Iterate through each row to...
        foreach (GridViewRow row in data)
        {

            //Iterate through each cell in each row...
            foreach (DataControlFieldCell cellResult in row.Cells)
            {

                //Create a variable to store the data control field required
                //to reference the header text
                DataControlField cellDCF = cellResult.ContainingField;

                //Create a variable to store the header text that identifies your cell
                string cellID = cellDCF.HeaderText;

                //Check to see if your header text is the one you want
                if (cellID.ToString() == "ISBN" || cellID.ToString()=="Year")
                {

                    //If it is, then grab its value (data in the cell) and store it in
                    //a variable to perform actions against
                    int cellValue = int.Parse(cellResult.Text);

                    //Perform whatever actions you need to here...
                    if (cellValue % 2 == 0)
                    {

                        cellResult.CssClass = "Hide";
                       

                    }
                    else
                    {
                        cellResult.Text = (cellValue + 2).ToString();
                        cellResult.BackColor = System.Drawing.Color.Red;
                    }
                }
            }
        }
    }
}

No comments:

Post a Comment