列的循序调整
View Code
1 Private Function AdjustFirstColumn(ByVal dsData As DataSet, ByVal firstColName As String) As DataSet
2 Dim bl As Boolean = False
3 For Each dt As DataTable In dsData.Tables
4 For Each drpt As DataColumn In dt.Columns
5 If drpt.ColumnName = firstColName Then
6 bl = True
7 End If
8 Next
9 If bl Then
10 dt.Columns(firstColName).SetOrdinal(0)
11 End If
12 Next
13 Return dsData
14
15 End Function
2 Dim bl As Boolean = False
3 For Each dt As DataTable In dsData.Tables
4 For Each drpt As DataColumn In dt.Columns
5 If drpt.ColumnName = firstColName Then
6 bl = True
7 End If
8 Next
9 If bl Then
10 dt.Columns(firstColName).SetOrdinal(0)
11 End If
12 Next
13 Return dsData
14
15 End Function
按一定数量行进行切割
View Code
1 Private Function splitDataTable(ByVal dt As DataTable, ByVal ColNumb As Integer) As DataSet
2
3 Dim leftThird As Integer = dt.Rows.Count Mod ColNumb
4 Dim timesThird As Integer = (dt.Rows.Count - leftThird) / ColNumb 这个有4舍五入现象 所以 要减掉 这个 余数 先,再开始整除
5
6 Dim ds As New DataSet()
7
8 For i As Integer = 1 To timesThird
9 Dim dt1 As DataTable = dt.Clone()
10 Dim j As Integer
11
12 For j = (i - 1) * ColNumb To (i * ColNumb - 1)
13 dt1.Rows.Add(dt.Rows(j).ItemArray)
14 Next
15
16
17
18 dt1.TableName = dt.TableName + i.ToString()
19 ds.Tables.Add(dt1)
20 Next
21
22 If leftThird > 0 Then
23 Dim dt2 As DataTable = dt.Clone()
24 Dim j As Integer
25
26 For j = timesThird * ColNumb To leftThird + timesThird * ColNumb - 1
27 dt2.Rows.Add(dt.Rows(j).ItemArray)
28 Next
29
30
31
32 dt2.TableName = dt.TableName + (timesThird + 1).ToString()
33 ds.Tables.Add(dt2)
34 End If
35 Return ds
36 End Function
2
3 Dim leftThird As Integer = dt.Rows.Count Mod ColNumb
4 Dim timesThird As Integer = (dt.Rows.Count - leftThird) / ColNumb 这个有4舍五入现象 所以 要减掉 这个 余数 先,再开始整除
5
6 Dim ds As New DataSet()
7
8 For i As Integer = 1 To timesThird
9 Dim dt1 As DataTable = dt.Clone()
10 Dim j As Integer
11
12 For j = (i - 1) * ColNumb To (i * ColNumb - 1)
13 dt1.Rows.Add(dt.Rows(j).ItemArray)
14 Next
15
16
17
18 dt1.TableName = dt.TableName + i.ToString()
19 ds.Tables.Add(dt1)
20 Next
21
22 If leftThird > 0 Then
23 Dim dt2 As DataTable = dt.Clone()
24 Dim j As Integer
25
26 For j = timesThird * ColNumb To leftThird + timesThird * ColNumb - 1
27 dt2.Rows.Add(dt.Rows(j).ItemArray)
28 Next
29
30
31
32 dt2.TableName = dt.TableName + (timesThird + 1).ToString()
33 ds.Tables.Add(dt2)
34 End If
35 Return ds
36 End Function
按某列进行分组
View Code
1 IEnumerable<IGrouping<string, DataRow>> result = dt.Rows.Cast<DataRow>().GroupBy<DataRow, string>(dr => dr[strGroupBy1].ToString());
2 int j = 0;
3 foreach (IGrouping<string, DataRow> ig in result)
4 {
5 DataTable dt1 = dt.Clone();
6 j++;
7 foreach (var dr_gp1 in ig){
8 dt1.Rows.Add(dr_gp1.ItemArray);
9 }
10 dt1.TableName = dt.TableName + j.ToString();
11
12 if (dt1.Rows.Count > 0)
13 {
14 dsGroup1.Tables.Add(dt1);
15 }
2 int j = 0;
3 foreach (IGrouping<string, DataRow> ig in result)
4 {
5 DataTable dt1 = dt.Clone();
6 j++;
7 foreach (var dr_gp1 in ig){
8 dt1.Rows.Add(dr_gp1.ItemArray);
9 }
10 dt1.TableName = dt.TableName + j.ToString();
11
12 if (dt1.Rows.Count > 0)
13 {
14 dsGroup1.Tables.Add(dt1);
15 }
过滤
View Code
1 dt.DefaultView.RowFilter = "colname=" + xxxx;
2 if (dt.DefaultView.Count > 0)
3 {
4 string str = dt.DefaultView[0]["ColumnsName"].ToString());
5 }
2 if (dt.DefaultView.Count > 0)
3 {
4 string str = dt.DefaultView[0]["ColumnsName"].ToString());
5 }