I think you're following the wrong approach here. Your data is probably coming from a datasource. That datasource probably contains the question, the answers options, and the correct answer. The logic to determine the score per question, or the score for the entire test, belongs in your business object, not in your GUI layer. I would advice you to do the following:
- Create a Question class, looking something like this (you can change your answers to be a collection if needed, but I wanted to keep the example simple
public class Question
{
public string Question { get; set; }
public string AnswerOne { get; set; }
public string AnswerTwo { get; set; }
public string AnswerThree { get; set; }
public string AnswerFour { get; set; }
public int CorrectAnswer { get; set; }
public bool AnswerOneSelected { get; set; }
public bool AnswerTwoSelected { get; set; }
public bool AnswerThreeSelected { get; set; }
public bool AnswerFourSelected { get; set; }
public bool IsAnswerCorrect
{
get
{
bool[] currentAnswers = new bool[] { AnswerOneSelected, AnswerTwoSelected, AnswerThreeSelected, AnswerFourSelected };
return currentAnswers[CorrectAnswer - 1];
}
}
public int ScoreForCorrectAnswer { get; set; }
public int Score
{
get
{
return IsAnswerCorrect ? ScoreForCorrectAnswer : 0;
}
}
}
Bind a list of these objects to your GridView, binding each Answer...Selected property to each of the Radiobuttons.
Next, you can either implement INotifyPropertyChanged on your objects to know when a different answer is selected, or continue to use the CheckedChanged event that you are already using.
You can get the score for the test by querying your collection of objects like this:
var score = questions.Where(q => q.IsAnswerCorrect).Count();
If you want to use a different scoring mechanism, use the Score property instead, and do something like:
var score = questions.Sum(q => q.Score);