Home Backend Development C#.Net Tutorial How to implement row-column conversion in DataTable in C#

How to implement row-column conversion in DataTable in C#

Jan 18, 2017 am 09:39 AM

The example of this article describes the method of realizing row-column conversion in DataTable in C#. Share it with everyone for your reference. The specific implementation method is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

protected void Page_Load(object sender, EventArgs e)

{

  if (!IsPostBack)

  {

   DataTable tt = GetCrossTable(CreateDT());

   GridView1.DataSource = tt;

   GridView1.DataBind();

  }

}

//创建DataTable

protected DataTable CreateDT()

{

  DataTable tblDatas = new DataTable("Datas");

  //数据列

  tblDatas.Columns.Add("姓名", Type.GetType("System.String"));

  tblDatas.Columns.Add("科目", Type.GetType("System.String"));

  tblDatas.Columns.Add("分数", Type.GetType("System.Int32"));

  tblDatas.Rows.Add(new object[] { "张三", "语文", 89 });

  tblDatas.Rows.Add(new object[] { "张三", "数学", 90 });

  tblDatas.Rows.Add(new object[] { "张三", "英语", 79 });

  tblDatas.Rows.Add(new object[] { "张三", "地理", 70 });

  tblDatas.Rows.Add(new object[] { "张三", "生物", 95 });

  tblDatas.Rows.Add(new object[] { "李四", "语文", 87 });

  tblDatas.Rows.Add(new object[] { "李四", "英语", 86 });

  tblDatas.Rows.Add(new object[] { "李四", "地理", 82 });

  tblDatas.Rows.Add(new object[] { "王五", "语文", 81 });

  tblDatas.Rows.Add(new object[] { "王五", "数学", 70 });

  tblDatas.Rows.Add(new object[] { "王五", "英语", 88 });

  tblDatas.Rows.Add(new object[] { "王五", "生物", 96 });

  return tblDatas;

}

/// <summary>

/// 将DataTable的第二列的值转化为列

//(即将原来的行表,转化成交叉表,没有对应值则默认"0")

/// </summary>

/// <param name="dt">必须三列,第三列为值</param>

/// <returns></returns>

public static DataTable GetCrossTable(DataTable dt)

{

  if (dt == null || dt.Columns.Count != 3 || dt.Rows.Count == 0)

  {

   return dt;

  }

  else

  {

   DataTable result = new DataTable();

   result.Columns.Add(dt.Columns[0].ColumnName);

   DataTable dtColumns = dt.DefaultView.ToTable("dtColumns", true, dt.Columns[1].ColumnName);

   for (int i = 0; i < dtColumns.Rows.Count; i++)

   {

    string colName;

    if (dtColumns.Rows[1][0] is DateTime)

    {

     colName = Convert.ToDateTime(dtColumns.Rows[i][0]).ToString();

    }

    else

    {

     colName = dtColumns.Rows[i][0].ToString();

    }

    result.Columns.Add(colName);

    result.Columns[i + 1].DefaultValue = "0";

   }

   DataRow drNew = result.NewRow();

   drNew[0] = dt.Rows[0][0];

   string rowName = drNew[0].ToString();

   foreach (DataRow dr in dt.Rows)

   {

    string colName = dr[1].ToString();

    double dValue = Convert.ToDouble(dr[2]);

    if (dr[0].ToString().Equals(rowName, StringComparison.CurrentCultureIgnoreCase))

    {

     drNew[colName] = dValue.ToString();

    }

    else

    {

     result.Rows.Add(drNew);

     drNew = result.NewRow();

     drNew[0] = dr[0];

     rowName = drNew[0].ToString();

     drNew[colName] = dValue.ToString();

    }

   }

   result.Rows.Add(drNew);

   return result;

  }

}

Copy after login

I hope this article will be helpful to everyone’s C# programming.

For more related articles on how to implement row-column conversion in DataTable in C#, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to add next-level C compiler How to add next-level C compiler Mar 03, 2025 pm 05:44 PM

How to add next-level C compiler

What are the alternatives to NULL in C language What are the alternatives to NULL in C language Mar 03, 2025 pm 05:37 PM

What are the alternatives to NULL in C language

Method of copying code by C language compiler Method of copying code by C language compiler Mar 03, 2025 pm 05:43 PM

Method of copying code by C language compiler

Which C language compiler is better? Which C language compiler is better? Mar 03, 2025 pm 05:39 PM

Which C language compiler is better?

What are the web versions of C language compilers? What are the web versions of C language compilers? Mar 03, 2025 pm 05:42 PM

What are the web versions of C language compilers?

Is NULL still important in modern programming in C language? Is NULL still important in modern programming in C language? Mar 03, 2025 pm 05:35 PM

Is NULL still important in modern programming in C language?

C language online programming website C language compiler official website summary C language online programming website C language compiler official website summary Mar 03, 2025 pm 05:41 PM

C language online programming website C language compiler official website summary

C language compiler installation tutorial (computer version) C language compiler installation tutorial (computer version) Mar 03, 2025 pm 05:41 PM

C language compiler installation tutorial (computer version)

See all articles