How to implement row-column conversion in DataTable in C#
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:
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; } }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# is widely used in enterprise-level applications, game development, mobile applications and web development. 1) In enterprise-level applications, C# is often used for ASP.NETCore to develop WebAPI. 2) In game development, C# is combined with the Unity engine to realize role control and other functions. 3) C# supports polymorphism and asynchronous programming to improve code flexibility and application performance.

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

How to build applications using .NET? Building applications using .NET can be achieved through the following steps: 1) Understand the basics of .NET, including C# language and cross-platform development support; 2) Learn core concepts such as components and working principles of the .NET ecosystem; 3) Master basic and advanced usage, from simple console applications to complex WebAPIs and database operations; 4) Be familiar with common errors and debugging techniques, such as configuration and database connection issues; 5) Application performance optimization and best practices, such as asynchronous programming and caching.

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.
