【问题标题】:How to Display data horizontally如何水平显示数据
【发布时间】:2021-01-07 11:31:45
【问题描述】:

我有四个表(员工、津贴、扣除和 Ajenda,这些是主表,当我保存员工的财务详细信息时,我将它们保存在不同的表中作为图像。 我想以水平方式显示员工详细信息和财务详细信息。 我正在使用 C# 和 SQLServer 2012 。 第一个表包含员工的主要详细信息,第二个包含员工 ID 以及津贴和其他表。

My tables

【问题讨论】:

  • 感谢您抽出宝贵时间分享您的问题。但是您的问题中缺少一些东西。你的目标和困难是什么?到目前为止你做了什么?请尝试更好地解释您的问题、开发环境和数据结构,并分享或多或少的代码(无屏幕截图)、一些示例、图像或屏幕草图以及用户故事或场景图。为了帮助您改进您的要求,请阅读How do I ask a good question & Writing the perfect question
  • 如果您发布新帖子,我有一个解决方案。没有理由关闭这个帖子。什么都没有。
  • 因此投票重新开放...
  • @Ragy 请根据您提供的表格数据举例说明预期结果。另外,这是 WinForms 还是 ASP.NET?另外,到目前为止,您研究或尝试了什么?你到底卡在哪里了?该请求非常广泛和模糊,我们真的不知道该过程的哪一部分给您带来了困难。例如,您是否正在努力编写必要的 SQL 查询?还是努力将数据绑定到 UI?或者创建一个看起来正确的用户界面?和/或别的什么?请提供清晰和详细信息,以便我们更好地为您提供帮助。
  • @jdweng 恕我直言,这篇文章太模糊了,很容易因为缺乏细节/重点而关闭。正如我上面提到的,我们并不确切知道需要什么,或者实现它的问题出在哪里。这里甚至没有一个实际的问题 - 只是“我想要”......请求创建一些模糊的东西,似乎从无到有。我们甚至不知道正在使用哪种 C# 应用程序。所以我很惊讶你说你有一个解决方案,因为你必须做出很多重要的假设。

标签: c# sql sql-server


【解决方案1】:

这是我在有人再次关闭之前的代码。我决定不使用组,因为会有很多左外连接,这会使解决方案复杂化。决定只创建一个表,然后将数据直接添加到结果表中。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable employees = new DataTable();
            employees.Columns.Add("empID", typeof(int));
            employees.Columns.Add("emp_name", typeof(string));
            employees.Columns.Add("emp_tele", typeof(string));

            employees.Rows.Add(new object[] { 1, "David", "025896325" });
            employees.Rows.Add(new object[] { 2, "John", "856985658" });
            employees.Rows.Add(new object[] { 3, "Micheal", "654687887" });

            DataTable allowances = new DataTable();
            allowances.Columns.Add("empID", typeof(int));
            allowances.Columns.Add("allow_name", typeof(string));
            allowances.Columns.Add("allow_Amount", typeof(int));

            allowances.Rows.Add(new object[] { 1, "allow1", 100 });
            allowances.Rows.Add(new object[] { 1, "allow2", 200 });
            allowances.Rows.Add(new object[] { 1, "allow3", 300 });
            allowances.Rows.Add(new object[] { 2, "allow1", 100 });
            allowances.Rows.Add(new object[] { 2, "allow2", 200 });

            DataTable deductions = new DataTable();
            deductions.Columns.Add("empID", typeof(int));
            deductions.Columns.Add("Dedu_name", typeof(string));
            deductions.Columns.Add("Dedu_Amount", typeof(int));

            deductions.Rows.Add(new object[] { 1, "ded1", 10 });
            deductions.Rows.Add(new object[] { 1, "ded2", 5 });
            deductions.Rows.Add(new object[] { 2, "ded1", 10 });

            DataTable ajenda = new DataTable();
            ajenda.Columns.Add("empID", typeof(int));
            ajenda.Columns.Add("ajenda_name", typeof(string));
            ajenda.Columns.Add("ajenda_Amount", typeof(int));

            ajenda.Rows.Add(new object[] { 1, "aj1", 200 });
            ajenda.Rows.Add(new object[] { 1, "aj1", 200 });
            ajenda.Rows.Add(new object[] { 1, "aj2", 300 });

            DataTable results = employees.Clone();

            string[] allow_names = allowances.AsEnumerable().Select(x => x.Field<string>("allow_name")).Distinct().OrderBy(x => x).ToArray();
            foreach (string name in allow_names)
            {
                results.Columns.Add(name, typeof(string));
            }


            string[] dedu_names = deductions.AsEnumerable().Select(x => x.Field<string>("Dedu_name")).Distinct().OrderBy(x => x).ToArray();
            foreach (string name in dedu_names)
            {
                results.Columns.Add(name, typeof(string));
            }

            string[] ajenda_names = ajenda.AsEnumerable().Select(x => x.Field<string>("ajenda_name")).Distinct().OrderBy(x => x).ToArray();
            foreach (string name in ajenda_names)
            {
                results.Columns.Add(name, typeof(string));
            }

            //add employees to result table
            foreach(DataRow row in employees.AsEnumerable())
            {
                results.Rows.Add(row.ItemArray);
            }

            var groupAllownaces = allowances.AsEnumerable().GroupBy(x => x.Field<int>("empID"));
            foreach (var group in groupAllownaces)
            {
                DataRow employeeRow = results.AsEnumerable().Where(x => x.Field<int>("empID") == group.Key).First();
                foreach (DataRow row in group)
                {
                    employeeRow[row.Field<string>("allow_name")] = row.Field<int>("allow_Amount");
                }
            }

            var groupDeductions = deductions.AsEnumerable().GroupBy(x => x.Field<int>("empID"));
            foreach (var group in groupDeductions)
            {
                DataRow employeeRow = results.AsEnumerable().Where(x => x.Field<int>("empID") == group.Key).First();
                foreach (DataRow row in group)
                {
                    employeeRow[row.Field<string>("Dedu_name")] = row.Field<int>("Dedu_Amount");
                }
            }

            var groupAjenda = ajenda.AsEnumerable().GroupBy(x => x.Field<int>("empID"));
            foreach (var group in groupAjenda)
            {
                DataRow employeeRow = results.AsEnumerable().Where(x => x.Field<int>("empID") == group.Key).First();
                foreach (DataRow row in group)
                {
                    employeeRow[row.Field<string>("ajenda_name")] = row.Field<int>("ajenda_Amount");
                 }
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多