datagridview columns databindings

Hi all.

I've this table in database:

columnDay, columnTotal

Now, in the columnDay, are stored data like:

columnDay columnTotal
Mon 01 01:00:00
Tue 02 01:00:00
Wed 03 01:00:00
Thu 04 01:00:00
Fre 05 01:00:00
Sat 06 01:00:00
Sun 07 01:00:00

The number of days are directly retrieved from the month.

Now, i need to design a datagridview, that have the name and number of days as columns:

For ex.:
int myColumn = Thread.CurrentThread.CurrentCulture.Calendar.GetDaysInMonth(myYear, datetimepicker1.Value.Month);

datagridView1.ColumnCount = myColumn;

for(int i = 0; i < datagridView1.Column.Count; i++)
{
DateTime dt = new DateTime(year, datetimepicker1.Value.Month, (i + 1));
datagridview1.ColumnsIdea.HeaderText = dt.ToString("ddd \ndd");
}

This works perfectly, but i dunno how to bind the data of the days of my table.

For ex.: (trying to design a datagridview)

-------- --------
Mon 01 Tue 02
-------- --------
01:00:00 01:00:00
03:05:00 02:15:00

etcetera...

Plz, may you suggest me right way to solve this

Thank you all so much in advance.



Answer this question

datagridview columns databindings

  • Mikkel Christensen

    I think that the link is the right hint for me.

    I was only unable to translate the code in link from vb to c#.

    May you help me

    Really thx. :)

  • YongZai

    Forgot [ y ] will convert to an icon Yes in the forums.


  • Cosmin Cojocar

    this is because i dont have explained very well my problem :(

    I have not problem with formatting datagridview columns and to sum cells value, but in bindings.

    in database i have the table "Hours" with this column and the data are stored like:

    ID_Hours --- Day ---- TotalHours
    1                    Sun         05:30:00
    2                    Mon       03:00:00
    3                    Wed       12:35:00
    4                     Sun        03:00:00
    5                     Mon      18:00:00
    6                     Wed     01:00:00

    My problem is to bind the datagrid to view data like this:

    Sun             Mon         Tue         Wed             Thu         Fry             Sat
    05:30:00     03:00:00                 12:35:00
    03:00:00     18:00:00                 01:00:00


    Hope this help me in explain. Sorry and thx for help.


  • John Bock

    Nevermind....

    Your help was really helpful.

    Thx :)

  • jibotang

    If you are getting the data from sql server 2005 you can use the pivot statement. Otherwise you will have to rotate the table with reflection. There is a sample on the VB-Tips website.

  • WineNCheese

  • HotKeeper

    is code:

    drYes = dt1.RowsYes[x];



  • Christian Mol

    im still getting confusion :(

    Your code works perfectly with in-code data, but i was unable when try to bind to database table.

    maybe i have to bind the "dt original" to table in database in CreateTable()


  • Alexei_shk

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    namespace Pivot
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    DataTable dt1 = CreateTable();
    DataTable dt2 = new DataTable("Reflected");
    for (int i = 0; i < dt1.Rows.Count; i++)
    {
    dt2.Columns.Add(i.ToString());
    }

    for (int x = 0; x < dt1.Rows.Count; x++)
    {
    DataRow dr = dt2.NewRow();
    for (int y = 0; y < dt1.Columns.Count; y++)
    {
    drYes = dt1.RowsYes[x];
    }
    dt2.Rows.Add(dr);
    }

    dataGridView1.DataSource = dt2;
    }

    public DataTable CreateTable()
    {
    DataTable dt = new DataTable("Orginal");
    dt.Columns.Add("Name");
    dt.Columns.Add("State");
    dt.Columns.Add("Country");

    Object[] arRow = new Object[3];
    arRow[0] = "Ken";
    arRow[1]="Florida";
    arRow[2] = "US";

    dt.LoadDataRow(arRow,true);

    arRow[0] = "Cor";
    arRow[1] = "Holland";
    arRow[2] = "EU";

    dt.LoadDataRow(arRow, true);

    arRow[0] = "Michael";
    arRow[1] = "New Jeresy";
    arRow[2] = "US";

    dt.LoadDataRow(arRow, true);
    return dt;
    }
    }


    }



  • Mortsdeh

    I believe that this was posted and answered under another section here
  • Gravy

    May you link me where

    Thx.

  • datagridview columns databindings