4 Replies - 433 Views - Last Post: 21 December 2011 - 10:04 AM Rate Topic: -----

#1 mejohnm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 27-January 09

linking data to datagrid/table rows

Posted 15 December 2011 - 02:44 PM

Hello all,

I have a project which is nearly finished. I have a datagrid with a column for the project name and the rest of the columns are for the days of the month. My goal is to be able to display and let the user be able to change the hours worked for that day. Entered values will be saved to a database. I am having a hard time figuring out how to. Here is what I have for the code:

private void FillDays()
        {
            DaysView1.Columns.Clear();
            DaysView1.DataSource = null;

            var daysCount = DateTime.DaysInMonth(cyear, cmonth);
            DataRow DayColumn;
            string strSQLConnection = ConfigurationManager.ConnectionStrings["LocalDB"].ConnectionString;
            string strselect = "SELECT Projects.ProjectName, Activity.Day, Activity.HoursDone FROM Projects left join ";
            strselect += "(ProjectHours left join Activity on ProjectHours.ProjectHoursID = Activity.ProjectHoursID) ";
            strselect += "on ProjectHours.ProjectID = Projects.ProjectID ";
            strselect += " WHERE ProjectHours.Month = " + cmonth + " and ProjectHours.Year = " + cyear;
            SqlDataAdapter myAdapter = new SqlDataAdapter(strselect, strSQLConnection);
            SqlCommandBuilder myBuilder = new SqlCommandBuilder(myAdapter); 

            DataTable myTable = new DataTable();
            myAdapter.Fill(myTable);

            for (int i = 1; i <= daysCount; i++)
            {
                myTable.Columns.Add(i.ToString());               
            }
            DaysView1.DataSource = myTable;


            /*for (int i = 1; i <= daysCount; i++)
            {
                DaysView1.Columns.Add(new DataGridViewColumn() { HeaderText = i.ToString(), CellTemplate = new DataGridViewTextBoxCell() });
            }*/
            //DaysView1.Columns[1].Visible = false;

        }



Is This A Good Question/Topic? 0
  • +

Replies To: linking data to datagrid/table rows

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: linking data to datagrid/table rows

Posted 16 December 2011 - 09:41 AM

So what are you having problems with? Displaying the columns? Saving the data? What problems? Errors? Not working? Just don't know what to do?
Was This Post Helpful? 0
  • +
  • -

#3 mejohnm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 27-January 09

Re: linking data to datagrid/table rows

Posted 16 December 2011 - 02:14 PM

There are no errors in the above code. It just I cannot ... do not know how to get the data to show as I would like it to. With the current code it comes out so:

Posted Image

Learning by doing. But as you see, my goal is to get the value under "HoursDone" put into the "Day" that the work was done on.

Just realized the bottom of that pic got cut off. Day 5 has 3 HoursDone.
Was This Post Helpful? 0
  • +
  • -

#4 mejohnm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 27-January 09

Re: linking data to datagrid/table rows

Posted 16 December 2011 - 02:46 PM

I have a strong feeling as I play around with this, that the datatable values just need to be linked to the datagrid's column. I have a line like:

DataGridView1.Rows[0].Cells["1"].Value = 1;

Then I get the 1 into the Column "1". Now I just need to figure how I can get the datagrid to know which row has what value for the columns...

something like: foreach datatable row, if datatable["Day"] has a value, then datagridview also has that value for that day.
Was This Post Helpful? 0
  • +
  • -

#5 mejohnm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 27-January 09

Re: linking data to datagrid/table rows

Posted 21 December 2011 - 10:04 AM

OK... did some formatting and got this so far:

Posted Image


Would this be easier to get each value for the days under one row, to make another datatable and add the rows that include the value of the das for each project? That is my goal I am trying to achieve, like this:

Posted Image
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1