C# 如何填充表中的DataGrid列和其他表SQL数据库中的行--C

C# 如何填充表中的DataGrid列和其他表SQL数据库中的行--C,c#,C#,//如何从其他表填充此列您想要一个仅使用一个表的透视表。我用c语言创建了表以进行测试。您应该从适配器中填写我注释掉的代码表。我更改了你的Select语句 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; namespace ConsoleApplication1 { cl

//如何从其他表填充此列

您想要一个仅使用一个表的透视表。我用c语言创建了表以进行测试。您应该从适配器中填写我注释掉的代码表。我更改了你的Select语句

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

namespace ConsoleApplication1
{
    class Program
    {
        
        static void Main(string[] args)
        {
            //SqlDataAdapter da = new SqlDataAdapter("select * from tbTotal", conn);
            //DataTable dt = new DataTable();
            //da.Fill(dt);

            //  heere is what results would look like
            DataTable dt = new DataTable();
            dt.Columns.Add("userName", typeof(string));
            dt.Columns.Add("total", typeof(int));

            dt.Rows.Add(new object[] { "admin",20});
            dt.Rows.Add(new object[] { "admin",30});
            dt.Rows.Add(new object[] { "ali",10});
            dt.Rows.Add(new object[] { "sam",7});
            dt.Rows.Add(new object[] { DBNull.Value,DBNull.Value});

            string[] users = dt.AsEnumerable().Select(x => x.Field<string>("userName")).Distinct().OrderBy(x => x).Where(x => x != null).ToArray();

            DataTable pivotTable = new DataTable();
            foreach (string user in users)
            {
                pivotTable.Columns.Add(user, typeof(int));
            }

            Dictionary<string, List<DataRow>> dict = dt.AsEnumerable()
                .Where(x => x.Field<object>("userName") != null)
                .GroupBy(x => x.Field<string>("userName"), y => y)
                .ToDictionary(x => x.Key, y => y.ToList());
            int max = dict.Max(x => x.Value.Count());

            for (int i = 0; i < max;i++)
            {
                DataRow newRow = pivotTable.Rows.Add();
                foreach (string user in users)
                {
                    List<DataRow> userRows = dict[user];
                    if (userRows.Count() >= i + 1)
                    {
                        newRow[user] = userRows[i].Field<int>("total");
                    }
                }
            }
        }
    }
 
}

使用加入:你能看到我的附件吗
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
    class Program
    {
        
        static void Main(string[] args)
        {
            //SqlDataAdapter da = new SqlDataAdapter("select * from tbTotal", conn);
            //DataTable dt = new DataTable();
            //da.Fill(dt);

            //  heere is what results would look like
            DataTable dt = new DataTable();
            dt.Columns.Add("userName", typeof(string));
            dt.Columns.Add("total", typeof(int));

            dt.Rows.Add(new object[] { "admin",20});
            dt.Rows.Add(new object[] { "admin",30});
            dt.Rows.Add(new object[] { "ali",10});
            dt.Rows.Add(new object[] { "sam",7});
            dt.Rows.Add(new object[] { DBNull.Value,DBNull.Value});

            string[] users = dt.AsEnumerable().Select(x => x.Field<string>("userName")).Distinct().OrderBy(x => x).Where(x => x != null).ToArray();

            DataTable pivotTable = new DataTable();
            foreach (string user in users)
            {
                pivotTable.Columns.Add(user, typeof(int));
            }

            Dictionary<string, List<DataRow>> dict = dt.AsEnumerable()
                .Where(x => x.Field<object>("userName") != null)
                .GroupBy(x => x.Field<string>("userName"), y => y)
                .ToDictionary(x => x.Key, y => y.ToList());
            int max = dict.Max(x => x.Value.Count());

            for (int i = 0; i < max;i++)
            {
                DataRow newRow = pivotTable.Rows.Add();
                foreach (string user in users)
                {
                    List<DataRow> userRows = dict[user];
                    if (userRows.Count() >= i + 1)
                    {
                        newRow[user] = userRows[i].Field<int>("total");
                    }
                }
            }
        }
    }
 
}
 SqlDataAdapter da = new SqlDataAdapter("select username from Users", conn);
        DataTable dt = new DataTable();
        da.Fill(dt);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dataGridView1.Columns.Add("col" + i.ToString(), dt.Rows[i][0].ToString());
            SqlDataAdapter da2 = new SqlDataAdapter("select tot_net from inv where username ='" + dt.Rows[i][0].ToString() + "' ", conn);
            DataTable dt2 = new DataTable();
            da2.Fill(dt2);
            
            for (int y = 0; y < dt2.Rows.Count; y++)

            {
                dataGridView1.Rows.Add();
                dataGridView1.Rows[y].Cells[i].Value = dt2.Rows[y]["tot_net"];
               
            }
          
        }