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
List<Student> students = new List<Student>();
public Form2()
{
InitializeComponent();
dataGridView1.AutoGenerateColumns = false;
dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
}
void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0 || e.ColumnIndex != dataGridView1.Columns["Select"].Index)
return;
Student selectedStudent = students[e.RowIndex];
MessageBox.Show(selectedStudent.Name + " " + selectedStudent.Surname);
}
private void Form2_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
students.Add(new Student() { Name = "Person" + i.ToString(), Surname = "Surname" + i.ToString()});
}
dataGridView1.DataSource = students;
}
}
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]