Answered by:
Help with Binding silverlight Datagrid coulmns

Question
-
Hi All,
I have 2 classes following are them:
public class StudentAttendanceClass { public string Stu_EnrollmentNum { get; set; }//1 public string Stu_Name { get; set; }//2 public List<SubjectCollection> subjectColl { get; set; } }
And Another one the child class looks like this:
public class SubjectCollection { public string SubjectName { get; set; } public int LectureCount { get; set; } public double AttendedLectures { get; set; } public double PercentageAttendance { get; set; } }
Basically what I am trying to do here is1. Create a new observable collection based on my first class i.e. "StudentAttendanceClass" =>oabsAttendanceAnalysis
2. Create a new instance of StudentAttendanceClass & populate name & enrollment num=> attendanceClass
3. Create multiple instences of SubjectCollection based on number of subjects available => sc
4. Add all objctes of SubjectCollection (sc) to attendanceClass object=> attendanceClass.subjectColl.Add(sc);
5. Create a new Datagrid & its columns & bind them to the observable collection.
6. Now the issue is how do i bind one of these dynamically generated colums to the SubjectCollection class for each of the subjects?
I tried doing this the following way:
for (int i = 0; i < subjectCount; i++) { string header = obsAttendanceAnalysis[i].subjectColl[i].SubjectName + " (" + obsAttendanceAnalysis[i].subjectColl[i].LectureCount + ")"; Binding bind = new Binding(); bind.Source = obsAttendanceAnalysis.Where (s => s.subjectColl[i].SubjectName == obsAttendanceAnalysis[i].subjectColl[i].SubjectName).FirstOrDefault(). subjectColl[i].AttendedLectures.ToString(); dg.Columns.Add(new GridViewDataColumn { Header = header, DataMemberBinding = bind });
But this basically copies the values from my first record in all Data Grid rows.
I hope I was able to properly describe the issue.
Let me know if any further details are requiered.
Any help is appreciated.
Thanks & Regards
Supreet
SupreetSaturday, June 11, 2011 4:50 PM
Answers
-
Hello Supreet,
I have one solution for you which will resolve the problem statement. I tried to implement it here and succeeded using a Converter while binding the Collection to the column. I blogged about the solution. You can find it here: http://www.kunal-chowdhury.com/2011/06/solution-for-binding-silverlight.html
Let me know, if you need any further assistance on this.
Cheers...
Kunal
Remember: Please click on "Mark As Answer", if this answered your query partially or fully.
Regards - Kunal Chowdhury | Software Developer | Chennai | India | My Blog | My Tweets | My Articles | Silverlight Tutorial- Proposed as answer by Kunal Chowdhury Wednesday, June 15, 2011 1:49 AM
- Marked as answer by Supreet tare Wednesday, June 15, 2011 3:32 PM
Wednesday, June 15, 2011 1:49 AM
All replies
-
Maybe I'm missing something in your scenario and probably I did not understand correctly, but populating a DataGrid is so simple. Instead of creating an instance of the Binding class, simply assign the ItemsSource property of the DataGrid with the ObservableCollection. You could set the AutoGenerateColumns property of the DataGrid as True, if you did not define columns manually in your XAML code.
Alessandro Del Sole
Microsoft MVP - Visual Basic: development
http://community.visual-basic.it/alessandro/Saturday, June 11, 2011 10:12 PM -
Hi Alessandro,
Thanks for your reply. Yes you are correct in plain simple scenarios binding the ItemSource of the grid to Observable Collection would have solved it.
But in this case my observable collection is based on a class which in turn has a child class as you can see above
my StudentAttendanceClass has following properties
string Name, string enrollmentNum, list<SubjectCollection> SubjectColl
where SubjectCollection is another child class having following properties
string subName, string TotalLectures.... so on
Each instance of StudentAttendanceClass can have multiple instances of SubjectCollection see I have a *LIST* property in first collection.
Now if I just set the itemSource = my observable Collection I get a UI that looks something like this..
http://twitpic.com/5aedv1/full
Instead I want my grid to look something like this
http://twitpic.com/5aeepd/full
You see in second pic I get nice new columns based on Subject Names & Number of Subjects also totalLectures in brackets with Subject Names
Here is a problem if you check the values, they are all copying the first record, apart from the first two columns.
Each row should display his attendance in a particular subject under the column showing same subject name. *Please see the second pic to understand this*
Thus I am using dynamic binding Hope I was better able to describe my case
& I also hope someone could help me with how exactly I can tackle this.
Thanks again for your response :)
Regards
P.S. Just refresh the twitpic url page if you can not see the pic at first..
Supreet
Sunday, June 12, 2011 5:07 AM -
Hello Supreet,
I have one solution for you which will resolve the problem statement. I tried to implement it here and succeeded using a Converter while binding the Collection to the column. I blogged about the solution. You can find it here: http://www.kunal-chowdhury.com/2011/06/solution-for-binding-silverlight.html
Let me know, if you need any further assistance on this.
Cheers...
Kunal
Remember: Please click on "Mark As Answer", if this answered your query partially or fully.
Regards - Kunal Chowdhury | Software Developer | Chennai | India | My Blog | My Tweets | My Articles | Silverlight Tutorial- Proposed as answer by Kunal Chowdhury Wednesday, June 15, 2011 1:49 AM
- Marked as answer by Supreet tare Wednesday, June 15, 2011 3:32 PM
Wednesday, June 15, 2011 1:49 AM -
Hi Kunal,
Thanks a ton for your help & patience to keep up with me.
Your answer undoubtedly fit my scenario & based on this learning I will be able to create one more even complex report.
Thanks a lot :)
Regards
Supreet
SupreetWednesday, June 15, 2011 3:34 PM