简体   繁体   中英

how to fill DataGrid Columns from table and rows from other table SQL DataBase -- C#

enter image description here

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());
        } 

// what i can do to fill this column from other table

You want a Pivot Table using only one table. I created the table in c# for testing. You should fill the table from the adapter the code I have commented out. I changed your Select statement.

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"];
               
            }
          
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM