datatable 的基本操作

DataTable myTable=DS.Tables["表名"];     --->添加
  DataRow myNewRow=myTable.NewRow();
  myNewRow["你要添加的列名"]="";
  myNewRow["你要添加的列名"]="";
  myTable.Rows.Add(myNewRow);
  dg2.DataSource=myTable.DefaultView;
  dg2.DataBind(); //将新添的数据邦定


DataRow[] myNewRow2=myTable.Select("id_jldw=11 AND name_jldw='内容'");   ----->修改
  //myNewRow2[0]["要修改的列名"]=值;
  myNewRow2[0]["要修改的列名"]=值;
  myNewRow2[0]["要修改的列名"]=值;
  dg3.DataSource=myTable.DefaultView;
  dg3.DataBind();//将新添的数据邦定



   myTable.Rows[0].Delete();//删除第一行             ---->删除
  dg4.DataSource=myTable.DefaultView;
  dg4.DataBind();
 protected void Button1_Click(object sender, EventArgs e)
    {

        string getname = this.DropDownList1.SelectedItem.Text.ToString();
        string getcode = this.DropDownList1.SelectedItem.Value.ToString();

        if (ViewState["ptable"] == null)
        {
            pipetable = new DataTable();
            pipetable.Columns.Add(new DataColumn("pid"typeof(int)));
            pipetable.Columns.Add(new DataColumn("pname"typeof(string)));
            DataColumn column = new DataColumn();
            column.ColumnName = "ID";
            column.AutoIncrement = true;
            column.AutoIncrementSeed = 1;
            column.AutoIncrementStep = 1;
            pipetable.Columns.Add(column);
            ViewState["ptable"] = pipetable;
        }
        else
        {
            pipetable = (DataTable)ViewState["ptable"];
        }

        DataRow[] myNewRow = pipetable.Select("pid='" + getcode + "'");
        if (myNewRow.Length.ToString() == "0")
        {
            DataRow row;
            row = pipetable.NewRow();
            row[0] = " " + getcode.ToString() + " ";
            row[1] = " " + getname.ToString() + " ";
            pipetable.Rows.Add(row);
        }
        BindGridView(pipetable);
        BindLine();
    }
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        pipetable = (DataTable)ViewState["ptable"];
        pipetable.Rows[e.RowIndex].Delete();
        pipetable.AcceptChanges();
        BindGridView(pipetable);
        BindLine();
    }
 public void BindLine()
    { 
        DateTime dt = DateTime.Now;
        string DateFormats = Convert.ToDateTime(dt).ToString("yyyy-MM-dd");

        pipetable = (DataTable)ViewState["ptable"];
        string allpipes = "";
        if (pipetable.Rows.Count > 0)
        {
            for (int k = 0; k < pipetable.Rows.Count; k++)
            {
                string  ppid = pipetable.Rows[k]["pid"].ToString();
                allpipes += ppid + ",";
                
            }

            allpipes = allpipes.TrimEnd(',');

            string sql1 = "select  PID,Pname from HeatLine where PID in (" + allpipes + ")  and convert(varchar(10),DateAndTime,120) = '" + DateFormats + "";
            DataSet ds1 = newdb.CommonDataSet(sql1);//取得所有管线

        }
    }
原文地址:https://www.cnblogs.com/tiger8000/p/2235948.html