Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi I have a list named Students, the init:
XML
List<student> Students = new List<student>();
Students = DataManager.allStudents;
</student></student>


I fill in the DataGridView as follows:
C#
if (Students.Count != 0)
for (int i = 0; i < Students.Count; i++)
{
    StudsTable.Rows.Add(new DataGridViewRow());
    StudsTable.Rows[i].Cells[0].Value = Students[i].fName; 
    StudsTable.Rows[i].Cells[1].Value = Students[i].Phone; 

    StudsTable.Rows[i].Tag = Students[i];
}


note: Except these 2 columns I have 3 more,
Button view type.

So it suddenly stopped working, and the exception sayes that it's impossible to add rows before columns.
Any suggestions??
Tal.
Posted
Updated 9-Jun-11 3:07am
v4

1 solution

You add Rows to the StudsTable (which I assume is a datagridview), but then you add values to a row in the ProgsTable (which I assume is another datagridview). Maybe I am missing something, but that does not look right. If ProgsTable does not have any columns defined trying to add rows will throw this exception.

[Addded]In response to your question below, here is how I would do this

C#
//Student list
        List<Student> students = new List<Student>();
        public Form2()
        {
            InitializeComponent();
            //Make sure DataGrid uses pre setup columns
            dataGridView1.AutoGenerateColumns = false;
            //subscribe to the datagrids CellClick event
            dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
        }
        void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // Ignore clicks that are not on button cells and not from Header Row.
            if (e.RowIndex < 0 || e.ColumnIndex != dataGridView1.Columns["Select"].Index)
                return;
            //Get selected row from eventargs and get respective student from students
            Student selectedStudent  = students[e.RowIndex];
            //Do whatever needs to be done
            MessageBox.Show(selectedStudent.Name + " " + selectedStudent.Surname);
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            //Test Data
            for (int i = 0; i < 10; i++)
            {
                students.Add(new Student() { Name = "Person" + i.ToString(), Surname = "Surname" + i.ToString()});
            }
            dataGridView1.DataSource = students;
        }
    }
    //Test Class
    public class Student
    {
        public string Name { get; set; }
        public string Surname { get; set; }
    }


This assumes a form containing a DataGridView(dataGridView1) with 3 Columns set up using the designer, 2 textboxcolumns and one buttoncolumn named Select. The text columns DataPropertyName property were each set to their respective property names(in this test case "Name" and "Surname")

[/Added]
 
Share this answer
 
v2
Comments
Member 7928594 9-Jun-11 9:03am    
no, it's a technic mistake, the dataGridView is called StudsTable, not ProgsTable. But this is not my problem yet...
Wayne Gaylard 9-Jun-11 9:22am    
I take it you have generated columns for StudsTable?
Member 7928594 9-Jun-11 9:29am    
Sure. I built them through the "Edit colums.." option of the DataGridView Tasks menu.
Wayne Gaylard 9-Jun-11 9:39am    
Have you tried binding the DataGridView direct to the List<student> like this dataGridView1.DataSource = Students;. Maybe try this temporarily to see if the error re-occurs. Otherwise everything looks alright. Though I would use a foreach rather than a for loop in this instance (but that is just getting pedantic :)),
Member 7928594 9-Jun-11 11:47am    
Thats basicly fine, but then - how will I use the Tag ?
It helps me to send the whole student's info when the user press the specific row's button. What will I do then??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900