DataTable汇总

一、排序

1 获取DataTable的默认视图

2 对视图设置排序表达式

3 用排序后的视图导出的新DataTable替换就DataTable

(Asc升序可省略,多列排序用","隔开)

DataView dv = dt.DefaultView;

dv.Sort = "id Asc,name Desc";

dt = dv.ToTable();

二、检索

1 设置查询字符串

2 使用Select方法获取到所有满足条件的数据行对象数组

(多项查询条件间,用and隔开.模糊查询使用 like %)

DataRow[] matches = dt.Select("(id>=20) and (title='会议') and (yearstr like '%2007%')");

string strName = matches[0]["name"].ToString();

另外也可以用下面这种方法,将检索出来的新表绑定到GridView

DataView dv = dt.DefaultView;

dv.RowFilter = "id>=2";

this.GridView1.DataSource = dv;

this.GridView1.DataBind();

三、合并

假定有2个DataTable:Dt1 , Dt2。表结构一样

将Dt2接在Dt1后可采用此方法

dt1.Merge(dt2);

四、分页

PagedDataSource pds = new PagedDataSource();

pds.AllowPaging = true;

pds.DataSource = dvIntegralExpense;

pds.AllowPaging = true;

pds.PageSize = pager.PageSize;

pds.CurrentPageIndex = pager.PageIndex;

rptIntegralExpense.DataSource = pds;

rptIntegralExpense.DataBind();

 

DataTable查询:

这里说到的查询有两种。

1.DataTable.Select
2.DataTable.Rows.Find

 

        /// <summary>
        /// 获取单张表
        /// </summary>
        /// <returns></returns>
        public static DataTable GetTable()
        {
            DataTable dt = new DataTable();
 
            dt.Columns.Add(new DataColumn("Id", typeof(int)));
            dt.Columns.Add(new DataColumn("Name", typeof(string)));
            dt.Columns.Add(new DataColumn("Age", typeof(int)));
 
            DataRow dr = dt.NewRow();
            dr["Id"] = 1;
            dr["Name"] = "tangh";
            dr["Age"] = 20;
            dt.Rows.Add(dr);
 
            dr = dt.NewRow();
            dr["Id"] = 2;
            dr["Name"] = "tangha";
            dr["Age"] = 19;
            dt.Rows.Add(dr);
 
            dr = dt.NewRow();
            dr["Id"] = 3;
            dr["Name"] = "tanghao";   
            dr["Age"] = 21;
            dt.Rows.Add(dr);
 
            return dt;
        }

 

2.然后我们来使用第一种Select方法。

/// <summary>
/// 使用DataTable.Select方法检索
/// </summary>
public static void VilideDataTableSelect()
{
DataTable dt = GetTable();

// 类似where条件查询,可以使用Id=2 and Name=ddd
DataRow[] rows = dt.Select("Id=2 and Name like 'tang%'"); // select类似where条件

foreach (DataRow dr in rows)
{
Console.Write(string.Format("Id={0},Name={1},Age={2}", dr["Id"], dr["Name"], dr["Age"]));
}
}

3.最后使用以下,Find方法。

/// <summary>
/// 使用DataTable.Rows.Find方法检索
/// </summary>
public static void VilideDataRowFind()
{
DataTable dt = GetTable();

// 设置主键,可以使用复合主键
dt.PrimaryKey = new DataColumn[] { dt.Columns["Id"] };

DataRow dr = dt.Rows.Find(2); // 查找Id=2的。即,这里检索主键。

Console.Write(string.Format("Id={0},Name={1},Age={2}", dr["Id"], dr["Name"], dr["Age"]));
}

总结:到现在才发现原来本身有很多方法可以检索的,所以也不必每次都循环遍历查找了

 

 

DataTable添加行或者插入行时显示"改行已属于另一表"的解决方案

1.运用add方法的解决方案:

DataTable dt = new DataTable(); 
dt = ds.Tables["All"].Clone();//把All的结构传递给dt 
DataRow[] dr=this.dataSet31.Tables["Product"].Select("bc=1"); 
for(int i=0;i<dr.Length;i++) 

//将数组元素加入表 
dt.Rows.Add(dr[i]);//出错提示为:该行已经属于另一个表 

//解决方法 
dt.Rows.Add(dr[i].ItemArray); 
//这样就好了!

 


2.运用insertAt方法的解决方案:


假设有table1和table2 
你想把table1的内容插入到table2 
那么

DataRow row = table2.NewRow(); 
row.ItemArray = table1.Rows[你需要的Row的索引].ItemArray; 
table2.Rows.InsertAt(row,你插入的索引);

 

 

DataTable 修改列名 删除列 调整列顺序

DataTable myDt =dt;
//删除列
myDt.Columns.Remove("minArea");
myDt.Columns.Remove("maxArea");

//调整列顺序 ,列排序从0开始
myDt.Columns["num"].SetOrdinal(1);

//修改列标题名称
dt.Columns["num"].ColumnName = "搜索量";
dt.Columns["rate"].ColumnName = "百分比";

 

 

posted @ 2015-12-15 18:05  陈海波  阅读(1796)  评论(0编辑  收藏  举报