临时数据表DataTable selected方法的使用

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class dataTable : System.Web.UI.Page
{
    protected DataTable dtYxzt;
    protected DataRow row;
    protected DataRow[] rows;
    private bool IsExist(int id1, int id2)
    {      
        rows = dtYxzt.Select("id1=" + id1 + " and id2=" + id2);
        if (rows.Length > 0)
            return true ;
        else
            return false;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Int16 id1, id2;
        dtYxzt = new DataTable("yxzt");
        dtYxzt.Columns.Add("id1", typeof(Int16));
        dtYxzt.Columns.Add("id2", typeof(Int16));
        dtYxzt.Columns.Add("sglx", typeof(string));
        dtYxzt.Columns.Add("cb", typeof(string));
        dtYxzt.Columns.Add("wsg", typeof(Int32));
        dtYxzt.Columns.Add("zzsg", typeof(Int32));
        dtYxzt.Columns.Add("sgwc", typeof(Int32));

        row = dtYxzt.NewRow();
        row["id1"] = 1;
        row["id2"] = 1;
        row["sglx"] = "新井射孔";
        row["cb"] = "采油二厂";
        row["wsg"] = 66;
        row["zzsg"] = 66;
        row["sgwc"] = 66;
        dtYxzt.Rows.Add(row);
        row = dtYxzt.NewRow();
        row["id1"] = 1;
        row["id2"] = 2;
        row["sglx"] = "新井射孔2";
        row["cb"] = "采油二厂2";
        row["wsg"] = 66;
        row["zzsg"] = 66;
        row["sgwc"] = 66;
        dtYxzt.Rows.Add(row);
        row = dtYxzt.NewRow();
        row["id1"] = 1;
        row["id2"] = 3;
        row["sglx"] = "新井射孔3";
        row["cb"] = "采油二厂3";
        row["wsg"] = 66;
        row["zzsg"] = 66;
        row["sgwc"] = 66;
        dtYxzt.Rows.Add(row);
        dtYxzt.AcceptChanges();      


        id1 = 1;
        id2 = 1;
        rows = dtYxzt.Select("id1=" + id1 + " and id2=" + id2);
        Response.Write("<br>(1,1)first:" + rows[0]["wsg"].ToString() + "<br>");
        id1 = 1;
        id2 = 4;
        if (!IsExist(id1, id2))
        {
            row = dtYxzt.NewRow();
            row["id1"] = 1;
            row["id2"] = 4;
            row["sglx"] = "新井射孔4";
            row["cb"] = "采油二厂4";
            row["wsg"] = 66;
            row["zzsg"] = 66;
            row["sgwc"] = 66;
            dtYxzt.Rows.Add(row);
           
        }
        else
        {
            rows = dtYxzt.Select("id1=" + id1 + " and id2=" + id2);
            rows[0]["wsg"] = 77;
        }      
        dtYxzt.AcceptChanges();
        rows = dtYxzt.Select("id1=" + id1 + " and id2=" + id2);
        Response.Write("<br>new(1,4)" + rows[0]["wsg"].ToString() + "<br>");

        id1 = 1;
        id2 = 1;      
        if (!IsExist(id1, id2))
        {
            row = dtYxzt.NewRow();
            row["id1"] = 1;
            row["id2"] = 4;
            row["sglx"] = "新井射孔4";
            row["cb"] = "采油二厂4";
            row["wsg"] = 66;
            row["zzsg"] = 66;
            row["sgwc"] = 66;
            dtYxzt.Rows.Add(row);

        }
        else
        {
            rows = dtYxzt.Select("id1=" + id1 + " and id2=" + id2);
            rows[0]["wsg"] = 77;
        }
        dtYxzt.AcceptChanges();
        rows = dtYxzt.Select("id1=" + id1 + " and id2=" + id2);
        Response.Write("<br>更新(1,1)" + rows[0]["wsg"].ToString() + "<br>");
        this.GridView1.DataSource = dtYxzt;
        this.DataBind();
    }


}

原文地址:https://www.cnblogs.com/macavalier/p/1305557.html