c# datatable 添删改datarow

1)前台

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication14._Default" %>
<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gvwTest" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField HeaderText="id" DataField="id" />
            <asp:BoundField HeaderText="name" DataField="name" />
        </Columns>
        </asp:GridView>
        <asp:Button ID="btnAdd" runat="server" Text="add" onclick="btnAdd_Click" />
        <asp:Button ID="btnUpdate" runat="server" Text="update" 
            onclick="btnUpdate_Click" />
        <asp:Button ID="btnDelete" runat="server" Text="delete" onclick="btnDelete_Click" />
    </div>
    </form>
</body>
</html>


2)后台

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace WebApplication14
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                ViewState["dtTest"]=null;
                ViewState["id"]=1;
                InitTable();
                BindGridView();
            }
        }
        private void BindGridView()
        {
            gvwTest.DataSource = (DataTable)ViewState["dtTest"];
            gvwTest.DataBind();
        }
        private void InitTable()
        {
            DataTable dtTest = new DataTable();
            dtTest.Columns.Add(new DataColumn("id"));
            dtTest.Columns.Add(new DataColumn("name"));
            dtTest.PrimaryKey = new DataColumn[] { dtTest.Columns["id"] };
            ViewState["dtTest"] = dtTest;
        }
        /// <summary>
        /// add
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            DataTable dtTest = (DataTable)ViewState["dtTest"];
            DataRow dr = dtTest.NewRow();
            dr["id"] = ViewState["id"];
            ViewState["id"] = Convert.ToInt32(ViewState["id"]) + 1;
            dr["name"] = "zhangsan";
            dtTest.Rows.Add(dr);
            BindGridView();
        }
        /// <summary>
        /// update
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            DataTable dtTest = (DataTable)ViewState["dtTest"];
            DataRow dr = dtTest.Rows.Find(1);
            dr.BeginEdit();
            dr["name"] = "lisi";
            dr.EndEdit();
            BindGridView();
        }
        /// <summary>
        /// delete
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            DataTable dtTest = (DataTable)ViewState["dtTest"];
            dtTest.Rows.Find(1).Delete();
            BindGridView();
        }
       
    }
}


 

你可能感兴趣的:(c# datatable 添删改datarow)