IsNumeric C# equivalent

IsNumeric in C#, WHY NOT?

This is something that has bothered me for a long while now. Why doesn't C# have a IsNumeric(string num) function like VB.NET? I have used every kind of IsNumeric code you van think of... This is what I started with.

public bool IsNumeric(string s) { try { Int32.Parse(s); } catch { return false; } return true; }

But we all no the unspoken rule, NEVER use try, catch for functionality... So I tried this.

internal static bool IsNumeric(string numberString) { char [] ca = numberString.ToCharArray(); for (int i = 0; i <> if (ca[i] > 57 || ca[i] <> return false; } return true; }

Then I found char.IsNumber: (Why do they have a char.IsNumber, but no string.IsNumeric???)

internal static bool IsNumeric(string numberString) { char [] ca = numberString.ToCharArray(); for (int i = 0; i <> if (!char.IsNumber(ca[i])) return false; } return true; }

public static bool IsNumeric(object Expression) { bool isNum; double retNum; isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any,System.Globalization.NumberFormatInfo.InvariantInfo, out retNum ); return isNum; }

How To Add IsNumneric Function in C#

That's right, you can call VB's IsNumeric() function directly from C#. First add a reference to the Visual Basic compatibility assembly, Microsoft.VisualBasic.dll, which contains the function's implementation: import namespace using Microsoft.VisualBasic;
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="IsNumneric.aspx.cs" Inherits="IsNumneric" %>

<!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>
</head>
<body>
   <form id="form1" runat="server">
       <div>
           <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
           <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
           <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></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;
using Microsoft.VisualBasic;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string strValue = TextBox1.Text;
        if (!Information.IsNumeric(strValue))
        {
            Label1.Text = "Is Not Numeric";
        }
        else
        {
            Label1.Text = "Numeric";

        }
    }
}

Working With ObjectDataSource And GridView

1. Create Middle Tier class

l Create static, stateless methods

l Mark class and methods with DataObjectMethod attribute (requires using System.ComponentModel)

2. Configure Data Source

· Choose a business object

· Define data methods

3. Configure GridView

· Check “Enable Editing”, etc.

step1.Add a new project in VS.net

step2. Add a class CustomersDataObject in App_Code Folder and add following code

using System;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web;

/// <summary>
/// Summary description for CustomerDataObject
/// </summary>
[DataObject(true)]
public class CustomersDataObject
{
/// <summary>
/// 
/// </summary>
private DataSet _customers;

/// <summary>
/// 
/// </summary>
public CustomersDataObject()
{
  this._customers = HttpContext.Current.Session["Customers"] as DataSet;

  if (this._customers == null)
  {
      this._customers = new DataSet();
      this._customers.ReadXml(HttpContext.Current.Server.MapPath(@"App_Data\customers.xml"));

      HttpContext.Current.Session["Customers"] = this._customers;
  }
}

/// <summary>
/// 
/// </summary>
private DataTable CustomerTable
{
  get { return this._customers.Tables["customers"]; }
}

/// <summary>
/// 
/// </summary>
/// <returns></returns>
[DataObjectMethod(DataObjectMethodType.Select)]
public DataView Select()
{
  this.CustomerTable.DefaultView.Sort = "CustomerID";
  return this.CustomerTable.DefaultView;
}
}
step3 .Place the xml file in App_Data Folder
<?xml version="1.0" encoding="utf-8" ?>
<customers>
<customers>
   <CustomerID>ALFKI</CustomerID>
   <CompanyName>Alfreds Futterkiste</CompanyName>
   <ContactName>Maria Anders</ContactName>
   <ContactTitle>Sales Representative</ContactTitle>
   <Address>Obere Str. 57</Address>
   <City>Berlin</City>
   <PostalCode>12209</PostalCode>
   <Country>Germany</Country>
   <Phone>030-0074321</Phone>
   <Fax>030-0076545</Fax>
</customers>
<customers>
   <CustomerID>ANATR</CustomerID>
   <CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
   <ContactName>Ana Trujillo</ContactName>
   <ContactTitle>Owner</ContactTitle>
   <Address>Avda. de la Constitución 2222</Address>
   <City>México D.F.</City>

   <PostalCode>05021</PostalCode>
   <Country>Mexico</Country>
   <Phone>(5) 555-4729</Phone>
   <Fax>(5) 555-3745</Fax>
</customers>
<customers>
   <CustomerID>ANTON</CustomerID>
   <CompanyName>Antonio Moreno Taquería</CompanyName>
   <ContactName>Antonio Moreno</ContactName>
   <ContactTitle>Owner</ContactTitle>
   <Address>Mataderos  2312</Address>
   <City>México D.F.</City>
   <PostalCode>05023</PostalCode>
   <Country>Mexico</Country>
   <Phone>(5) 555-3932</Phone>
</customers>
<customers>
   <CustomerID>AROUT</CustomerID>
   <CompanyName>Around the Horn</CompanyName>
   <ContactName>Thomas Hardy</ContactName>
   <ContactTitle>Sales Representative</ContactTitle>
   <Address>120 Hanover Sq.</Address>
   <City>London</City>
   <PostalCode>WA1 1DP</PostalCode>
   <Country>UK</Country>
   <Phone>(171) 555-7788</Phone>
   <Fax>(171) 555-6750</Fax>
</customers>
<customers>
   <CustomerID>BERGS</CustomerID>
   <CompanyName>Berglunds snabbköp</CompanyName>
   <ContactName>Christina Berglund</ContactName>
   <ContactTitle>Order Administrator</ContactTitle>
   <Address>Berguvsvägen  8</Address>
   <City>Luleå</City>
   <PostalCode>S-958 22</PostalCode>
   <Country>Sweden</Country>
   <Phone>0921-12 34 65</Phone>
   <Fax>0921-12 34 67</Fax>
</customers>
<customers>
   <CustomerID>BLAUS</CustomerID>
   <CompanyName>Blauer See Delikatessen</CompanyName>
   <ContactName>Hanna Moos</ContactName>
   <ContactTitle>Sales Representative</ContactTitle>
   <Address>Forsterstr. 57</Address>
   <City>Mannheim</City>
   <PostalCode>68306</PostalCode>
   <Country>Germany</Country>
   <Phone>0621-08460</Phone>
   <Fax>0621-08924</Fax>
</customers>
<customers>
   <CustomerID>BLONP</CustomerID>
   <CompanyName>Blondesddsl père et fils</CompanyName>
   <ContactName>Frédérique Citeaux</ContactName>
   <ContactTitle>Marketing Manager</ContactTitle>
   <Address>24, place Kléber</Address>
   <City>Strasbourg</City>
   <PostalCode>67000</PostalCode>
   <Country>France</Country>
   <Phone>88.60.15.31</Phone>
   <Fax>88.60.15.32</Fax>
</customers>
<customers>
   <CustomerID>BOLID</CustomerID>
   <CompanyName>Bólido Comidas preparadas</CompanyName>
   <ContactName>Martín Sommer</ContactName>
   <ContactTitle>Owner</ContactTitle>
   <Address>C/ Araquil, 67</Address>
   <City>Madrid</City>
   <PostalCode>28023</PostalCode>
   <Country>Spain</Country>
   <Phone>(91) 555 22 82</Phone>
   <Fax>(91) 555 91 99</Fax>
</customers>
<customers>
   <CustomerID>BONAP</CustomerID>
   <CompanyName>Bon app'</CompanyName>
   <ContactName>Laurence Lebihan</ContactName>
   <ContactTitle>Owner</ContactTitle>
   <Address>12, rue des Bouchers</Address>
   <City>Marseille</City>
   <PostalCode>13008</PostalCode>
   <Country>France</Country>
   <Phone>91.24.45.40</Phone>
   <Fax>91.24.45.41</Fax>
</customers>
<customers>
   <CustomerID>BOTTM</CustomerID>
   <CompanyName>Bottom-Dollar Markets</CompanyName>
   <ContactName>Elizabeth Lincoln</ContactName>
   <ContactTitle>Accounting Manager</ContactTitle>
   <Address>23 Tsawassen Blvd.</Address>
   <City>Tsawassen</City>
   <Region>BC</Region>
   <PostalCode>T2F 8M4</PostalCode>
   <Country>Canada</Country>
   <Phone>(604) 555-4729</Phone>
   <Fax>(604) 555-3745</Fax>
</customers>
<customers>
   <CustomerID>BSBEV</CustomerID>
   <CompanyName>B's Beverages</CompanyName>
   <ContactName>Victoria Ashworth</ContactName>
   <ContactTitle>Sales Representative</ContactTitle>
   <Address>Fauntleroy Circus</Address>
   <City>London</City>
   <PostalCode>EC2 5NT</PostalCode>
   <Country>UK</Country>
   <Phone>(171) 555-1212</Phone>
</customers>
<customers>
   <CustomerID>CACTU</CustomerID>
   <CompanyName>Cactus Comidas para llevar</CompanyName>
   <ContactName>Patricio Simpson</ContactName>
   <ContactTitle>Sales Agent</ContactTitle>
   <Address>Cerrito 333</Address>
   <City>Buenos Aires</City>
   <PostalCode>1010</PostalCode>
   <Country>Argentina</Country>
   <Phone>(1) 135-5555</Phone>
   <Fax>(1) 135-4892</Fax>
</customers>
<customers>
   <CustomerID>CENTC</CustomerID>
   <CompanyName>Centro comercial Moctezuma</CompanyName>
   <ContactName>Francisco Chang</ContactName>
   <ContactTitle>Marketing Manager</ContactTitle>
   <Address>Sierras de Granada 9993</Address>
   <City>México D.F.</City>
   <PostalCode>05022</PostalCode>
   <Country>Mexico</Country>
   <Phone>(5) 555-3392</Phone>
   <Fax>(5) 555-7293</Fax>
</customers>
<customers>
   <CustomerID>CHOPS</CustomerID>
   <CompanyName>Chop-suey Chinese</CompanyName>
   <ContactName>Yang Wang</ContactName>
   <ContactTitle>Owner</ContactTitle>
   <Address>Hauptstr. 29</Address>
   <City>Bern</City>
   <PostalCode>3012</PostalCode>
   <Country>Switzerland</Country>
   <Phone>0452-076545</Phone>
</customers>
<customers>
   <CustomerID>COMMI</CustomerID>
   <CompanyName>Comércio Mineiro</CompanyName>
   <ContactName>Pedro Afonso</ContactName>
   <ContactTitle>Sales Associate</ContactTitle>
   <Address>Av. dos Lusíadas, 23</Address>
   <City>Sao Paulo</City>
   <Region>SP</Region>
   <PostalCode>05432-043</PostalCode>
   <Country>Brazil</Country>
   <Phone>(11) 555-7647</Phone>
</customers>
<customers>
   <CustomerID>CONSH</CustomerID>
   <CompanyName>Consolidated Holdings</CompanyName>
   <ContactName>Elizabeth Brown</ContactName>
   <ContactTitle>Sales Representative</ContactTitle>
   <Address>Berkeley Gardens 12  Brewery</Address>
   <City>London</City>
   <PostalCode>WX1 6LT</PostalCode>
   <Country>UK</Country>
   <Phone>(171) 555-2282</Phone>
   <Fax>(171) 555-9199</Fax>
</customers>
</customers>
step4. Last and final step drag a gridview on web page and assign DataSourceID="odsCustomers"
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ObjectDataSource.aspx.cs"
Inherits="ObjectDataSource" %>

<!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>
</head>
<body>
<form id="form1" runat="server">
    <div>
        <asp:ObjectDataSource ID="odsCustomers" runat="server" SelectMethod="Select" TypeName="CustomersDataObject" />
        <asp:GridView ID="GridView1" runat="Server" DataSourceID="odsCustomers">
        </asp:GridView>
    </div>
</form>
</body>
</html>

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;
                    }
                }
            }
        }
    }
}

sp.Net Ajax HoverMenu Control On GridView with Database Manipulations

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewHover.aspx.cs" Inherits="GridViewHover" %>

<%@ Register TagPrefix="cc1" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit" %>
<!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">
       .popupMenu {
           position:absolute;
           visibility:hidden;
           background-color:#F5F7F8;
           opacity:.9;
           filter: alpha(opacity=90);
       } 
       .popupHover {
           background-image:url(img/header-opened.png);
           background-repeat:repeat-x;
           background-position:left top;
           background-color:#F5F7F8;
       }         
   </style>
</head>
<body>
   <form id="form1" runat="server">
       <asp:ScriptManager ID="ScriptManger1" runat="Server">
       </asp:ScriptManager>
       <div>
           <asp:GridView ID="GridView1" DataKeyNames="ISBN" runat="server" AutoGenerateColumns="False"
               Caption="Publisher List List" EmptyDataText="You have deleted all records in customer list"
               OnRowEditing="GridView1_RowEditing" OnRowDeleting="GridView1_RowDeleting" OnRowCommand="GridView1_RowCommand"
               OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating">
               <Columns>
                   <asp:TemplateField>
                       <ItemTemplate>
                           <asp:Panel ID="Panel1" runat="server" Width="500px">
                               <asp:Label ID="Label1" runat="server" Text='<%# Eval("ISBN") %>' Visible="False"></asp:Label>
                               <asp:Label ID="Label2" runat="server" Text='<%# Eval("Title") %>'></asp:Label>
                               <asp:Label ID="Label3" runat="server" Text='<%# Eval("Publisher") %>'></asp:Label>
                               <asp:Label ID="Label4" runat="server" Text='<%# Eval("Year") %>'></asp:Label>
                           </asp:Panel>
                           <cc1:HoverMenuExtender ID="HoverMenuExtender1" runat="server" HoverCssClass="popupHover"
                               PopupControlID="PopupMenu" TargetControlID="Panel1" PopupPosition="Left">
                           </cc1:HoverMenuExtender>
                           <asp:Panel ID="PopupMenu" runat="server" CssClass="popupMenu">
                               <div style="border: 1px outset white; padding: 2px;">
                                   <asp:LinkButton ID="LinkButton3" runat="server" CommandName="AddNew" Text="Add New"></asp:LinkButton>
                                   <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton>
                                   <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Delete" Text="Delete"></asp:LinkButton>
                               </div>
                           </asp:Panel>
                       </ItemTemplate>
                       <EditItemTemplate>
                           <asp:Panel ID="Panel1" runat="server" Width="400px">
                               <asp:Label ID="lblISBN" runat="server" Text='<%# Eval("ISBN") %>' Visible="false"></asp:Label>
                               <asp:TextBox ID="txtTitle" runat="server" Text='<%# Eval("Title") %>'></asp:TextBox>
                               <asp:TextBox ID="txtPub" runat="server" Text='<%# Eval("Publisher") %>'></asp:TextBox>
                               <asp:TextBox ID="txtYear" runat="server" Text='<%# Eval("Year") %>'></asp:TextBox>
                           </asp:Panel>
                           <cc1:HoverMenuExtender ID="HoverMenuExtender2" runat="server" HoverCssClass="popupHover"
                               PopupControlID="PopupMenu" PopupPosition="Right" TargetControlID="Panel1">
                           </cc1:HoverMenuExtender>
                           <asp:Panel ID="PopupMenu" runat="server" CssClass="popupMenu" Width="80">
                               <div style="border: 1px outset white; padding: 2px;">
                                   <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
                                       Text="Update"></asp:LinkButton>
                                   <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
                                       Text="Cancel"></asp:LinkButton>
                               </div>
                           </asp:Panel>
                       </EditItemTemplate>
                   </asp:TemplateField>
               </Columns>
           </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 GridViewHover : 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(string));
        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_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindGridViewView();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string ISBN = GridView1.DataKeys[e.RowIndex].Value.ToString();
        DataTable dt = Session["strTemp"] as DataTable;
        DataRow dr;
        dr = dt.NewRow();
        dt.Rows.Find(ISBN).Delete();
        dt.GetChanges();
        Session["strTemp"] = dt;
        BindGridViewView();


    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("AddNew"))
        {
            InsertBlanRow();
            GridView1.EditIndex = GridView1.Rows.Count;
            BindGridViewView();
        }
        //BindGridViewView();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindGridViewView();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string ISBN = GridView1.DataKeys[e.RowIndex].Value.ToString();
        DataTable dt = Session["strTemp"] as DataTable;
        DataRow dr;
        dr = dt.NewRow();
        dr = dt.Rows.Find(ISBN);
        GridViewRow row = GridView1.Rows[e.RowIndex];

        if (row != null)
        {

            TextBox txtTitle = row.FindControl("txtTitle") as TextBox;
            TextBox txtPub = row.FindControl("txtPub") as TextBox;
            TextBox txtYear = row.FindControl("txtYear") as TextBox;
            dr["Title"] = txtTitle.Text;
            dr["Publisher"] = txtPub.Text;
            dr["Year"] = txtYear.Text;

            dt.AcceptChanges();
            Session["strTemp"] = dt;

        }
        GridView1.EditIndex = -1;
        BindGridViewView();

    }
    protected void InsertBlanRow()
    {

        DataTable dt = Session["strTemp"] as DataTable;
        DataRow dr;
        dr = dt.NewRow();
        dr["Title"] = "";
        dr["Publisher"] = "";
        dr["Year"] = "";
        dt.Rows.Add(dr);
        Session["strTemp"] = dt;


    }
}

Tips -Master Page

ASP.NET master pages are pretty handy and here is a simple tip to make them even easier to use.

Using the MasterType reference in your content page allows you to refer to your master page properties without having to cast it to your master page type everytime.

For example...


<%@ MasterType TypeName="myMasterType" %>

allows me to write...


this.Master.myProperty = "value";

rather than...


((myMasterType)this.Master).myProperty = "value";

Which saves you a little typing and provides intellisense

Select GridView Row on Double Click

Start with an existing GridView that is populated from a data source In the ItemDataBound event handler of GridView, you assign a JavaScript ondblclick method to the GridViewRow
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewDblClick.aspx.cs"
   Inherits="GridViewDblClick" %>

<!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>
</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 GridViewDblClick : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      

        //Insure that the __doPostBack() JavaScript method is created...
        this.ClientScript.GetPostBackEventReference(this, string.Empty);
        if (!IsPostBack)
        {

            BindGridViewView();
        }
        else
        {
            if (!(Request.Form["_EventTarget"] != null && Request.Form["_EventTarget"] == "myDblclick"))
            {
                GridView1.SelectedIndex = int.Parse(Request.Form["__EVENTARGUMENT"].ToString());
                Response.Write(Request.Form["__EVENTARGUMENT"].ToString());


            }


        }
    }
    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(string));
        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)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Each row will then behave like a link, and when you select one it
            //can drive the behavior of another control(s) on your page
            //other way to select
            //e.Row.Attributes["ondblclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);

            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';this.style.color='red'";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';this.style.color='black';";
            e.Row.Attributes.Add("ondblclick", "Javascript:__doPostBack('myDblClick','" + e.Row.RowIndex + "');");

        }
    }
}