C# lists and datagridviews -
I am trying to populate a datagrid with information out of a list, and then pressing a button This saves the datagrid turns back to the list.
int pos = MainMenu.myList.FindIndex (x => x.id == valid ID); Var tempStu = MainMenu.myList [pos]; If (tempStu is DormStudent) {tempStu as DormStudent tempDorm = DormStudent; NameTextBox.Text = tempDorm.Name; Var blist = new binding list & lt; Students & gt; (TempDorm.Grades); Var source = new binding source (pod, faucet); GradeDataGridView.DataSource = source; } And {nameTextBox.Text = tempStu.Name; Var blist = new binding list & lt; Students & gt; (TempStu.Grades); Var source = new binding source (pod, faucet); GradeDataGridView.DataSource = source; }
and when you press the save button (I do not have as good as a saved part)
int pos = MainMenu.myList.FindIndex (X => x.ID == valid ID); Var tempStu = MainMenu.myList [pos]; If (tempStu is DormStudent) {tempStu as DormStudent tempDorm = DormStudent; TempDorm.Grades = gradesDataGridView; MainMenu.myList [pos] = tempDorm; } And {tempStu.Grades = gradesDataGridView; MainMenu.myList [pos] = tempStu; }
Here is a student class which is in grade.
Students of public classes {Public Ent ID}; Set; } Public string name {get; Set; } Public listing & lt; Int & gt; Grade; Public student () {id = 0; Name = "no student"; Grade = new list & lt; Int & gt; (); } Public Student (int i, string n) {id = i; Name = n; Grade = new list & lt; Int & gt; (); }}
When I press either the button, the check button is pressed or the datagrid can not be converted to the list. Can anyone help with that?
Note, however, that I am away from being an expert in data binding!
I think you can type the binding list
Typing for Student
. I hope you really want to display a student's grade top down.
If you really want to show all the students at the bottom and their grades are left to right in the column, then you should turn some serious.
Now the binding list should be bound to grade
not a student
, so the first idea is to call it the binding list & Lt; Int & gt;
.
Although the afaik anonymous variables make the wrong DataMembers
. They must have data to display properties
.
So I created a new class grade
which wraps the integer. For good measure, I also added a string test to keep the details of the test. Leave it if you do not want it!
public class grades {public at issue {get; Set; } Public string test {get; Set; } Public Grade () {Issue = 0; Test = "no test"; } Public grade (int scores, string test) {number = points; Test = test; }}
This requires a change in student class:
public list & lt; Grade & gt; Grade;
and (twice)
grade = new list & lt; Grade & gt; ();
By creating a bidding list, two rows now look like this:
var blist = new binding list & lt; Grade & gt; (TempDorm.Grades); Var blist = new binding list & lt; Grade & gt; (TempStu.Grades);
The good news is that not only the data is now loading properly in DataGridView
; No need to save two-way and , because the data is bound to two-sided and every change actually Is in the list!
You will need some more coding if only one undo function is required, but this is one more thing ..
Comments
Post a Comment