Hello all,

I'm writing a program to keep track of my swim team's test set times. I would like to display the information in a table like form to edit all of the swimmers and times at once. I'm using this object:

public class TestSetDisplay : INotifyPropertyChanged
    {
        string testSetName;
        string swimmerName;
        List<Time> times;
        Time average;

        public event PropertyChangedEventHandler PropertyChanged;

        public TestSetDisplay(Swimmer s, TestSet t)
        {
            testSetName = t.Name;
            swimmerName = s.Name;

            foreach (TestSet ts in s.TestSet)
            {
                if (ts.Name == testSetName)
                {
                    times = ts.Times;
                }
            }
        }

        public Time Average
        {
            get { return average; }
            //set { average = value; }
        }

        public string SwimmerName
        {
            get { return swimmerName; }
            set { swimmerName = value;
                this.NotifyPropertyChanged("SwimmerName");
            }
        }

        public List<Time> TimeList
        {
            get { return times; }
            set { times = value; }
        }

        private void NotifyPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }

You don't really need to know too much about the Swimmer and TestSet objects, I just get the names and the times for the test set out of those.

So here is my problem, I want to display a table in this form:

Name................time1............time2.................average
swimmer1......1:00.70..........1:01.65...............average of all the times
swimmer2........:58.54..........1:00.33
....

***How do I get each time from the List of times into the datagridview?

***Maybe a more simple question, how do I make a new column for each time in the datagridview?

Here is what I'm trying to do so far:

table = new DataTable("display");

            DataColumn nameCol = new DataColumn("Swimmer Name");
            nameCol.DataType = System.Type.GetType("System.String");

            table.Columns.Add(nameCol);

            // One column for each time
            DataColumn timeCol;
            for (int i = 0; i < theSet.Times.Count; i++)
            {
                string num = Convert.ToString(i + 1);
                timeCol = new DataColumn(num);
                timeCol.DataType = System.Type.GetType("System.String");
                
                table.Columns.Add(timeCol);
            }

            // Make the Name column the primary key column.
            DataColumn[] PrimaryKeyColumns = new DataColumn[1];
            PrimaryKeyColumns[0] = table.Columns["Swimmer Name"];
            table.PrimaryKey = PrimaryKeyColumns;

            DataRow row;// = new DataRow();

            foreach (TestSetDisplay td in displayList)
            {
                row = table.NewRow();
                row["Swimmer Name"] = td.SwimmerName;
                int i = 1;
                foreach (Time t in td.TimeList)
                {
                    row[Convert.ToString(i)] = t;
                    i++;
                }
                table.Rows.Add(row);
            }

            dataGridView1.DataSource = displayList;

Please help!

Recommended Answers

All 4 Replies

Since this is more of a parent:child relationship would it be acceptable to have a second grid that would list the swimmer's times when you chose a name in the main grid?

Since this is more of a parent:child relationship would it be acceptable to have a second grid that would list the swimmer's times when you chose a name in the main grid?

seconded, i would either populate a gridview or a combobox with the swimmer names, then put their times into a gridview.

But you want all of the swimmers and their times visible at once, is that essential? You can only edit one swimmer at a time and their times arent affected by the times fo others so you dont need to be able to see swimmer a's times whilst entering swimmer b's.

If you really want them all visible at once, i'll need more info on tyour classes. If i've read this right, your Swimemr class contains a collection of TestSets. Each TestSet contains a collection of their times for that set.
Your TestSetDisplay method searches a Swimmers TestSets for the TestSet parsed to it and retrieves the times for that Set.
right so far?

At this point, your list 'times' only contains the times for one swimmer in one set so you are perfectly positioned to do as sknake suggested.

Are you doing anything else to get the rest of the swimmers times? You have a reference to 'theSet.Times' when you are building your table...what is that variable?

If you could post your full code it would make it easier :)

I found my problem. I set the data source wrong!

dataGridView1.DataSource = displayList;

should be

dataGridView1.DataSource = table;

duh...Thanks for all your help!

I'm glad you found a solution to your problem, and thank you for sharing the fix.

Please mark this thread as solved (since you answered your own question :P) and good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.