none
Put data indatagridview RRS feed

  • Question

  • Hi all,
    I do not know how to display some data.
    I have a set of data in DB like this, where the number are the hours worked and the job is the type of work done:

    paul 1  jobA
    john 2  jobA
    mark 3  jobA
    paul 1  jobA
    paul 1  jobA
    
    paul 2	jobB
    john 2	jobB
    mark 2	jobB
    
    paul 3	jobC
    john 2	jobC
    mark 1	jobC
    
    paul 5  jobD
    
    mark 3  jobE

    I then grouped it with linq, getting this:

    paul 
        paul 1  jobA
        paul 1  jobA
        paul 1  jobA
        paul 2  jobB
        paul 3  jobC
        paul 5  jobD
    john 
        john 2  jobA
        john 2  jobB
        john 2  jobC
    mark 
        mark 3  jobA
        mark 2  jobB
        mark 1  jobC
        mark 3  jobE

    At this point I make a distinct job by getting:

    jobA
    jobB
    jobC
    jobD
    jobE

    with this above I can create the datagridview columns using column.Add...

    But how do I hit the right datagriview cells?

    In practice I would like this:

    	jobA	jobB	jobC	jobD	jobE
    paul	3	2	3	5
    john	2	2	2
    mark	3	2	1		3
    But I can not figure out how to put them in the datagridview at the right place.
    any suggestion?

    thanks

    d.



    • Edited by duppino Wednesday, November 22, 2017 7:58 PM
    Wednesday, November 22, 2017 3:39 PM

All replies

  • Hi duppino,

    The datagridview can display some data in a table, and this table is stored in the database, so if you want to display the data as you said above in datagridview, I suggest you define the datatable within database as below:

    CREATE TABLE [dbo].[T_Jobs]
    (
      [Id] INT identity NOT NULL PRIMARY KEY,
      [Name] NVARCHAR(50) NULL,
        [jobA] INT NULL,
        [jobB] INT NULL,
        [jobC] INT NULL,
        [jobD] INT NULL,
        [jobE] INT NULL
    )

    Then add some records:

    Then the code:

            private DataTable dt = new DataTable();
            private DataSet ds = new DataSet();
    
            private void Form1_Load(object sender, EventArgs e)
            {
                string sqlCon = @"sqlConnection";
    
                string sqlSelect = @"select Name,jobA,jobB,jobC,jobD,jobE from T_Jobs";
                using (SqlConnection conn = new SqlConnection(sqlCon))
                {
                    using (SqlCommand cmd = new SqlCommand(sqlSelect, conn))
                    {
                        using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                        {
                            adapter.Fill(ds);
                            dt = ds.Tables[0];
                            conn.Close();
                        }
                    }
                }
                dataGridView1.DataSource = dt;
            }

    Result:

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 23, 2017 1:44 AM
    Moderator