Data Grid View Reversing Members
I have been working on a form that presents some specific elements in a Data Grid View. Everything was implemented, and working correctly; then I decided to change things. I need many different version of the this one specific form, so I created a base form that my other forms can derive from. When I implement the inheritance of this base form, the data is listed in reverse. In other words before I made the change the columns were listed (Question Number, Marked For Review, Is Answered) no though, for what ever reason the data is listed (Is Answered, Marked for Review, Question Number). The reason that this matters is that my code assumes the question number is the first row, and that information is used to look up and display that specific question.
The base form looks like this
public partial class DataFormBase : FormBase
{
/// <summary>
/// Initializes a new instance of the <see cref="DataFormBase"/> class.
/// </summary>
public DataFormBase()
{
InitializeComponent();
DrawGUI();
}
protected virtual void PopulateDataGrid() {}
protected virtual void dgvData_CellClick(object sender, DataGridViewCellEventArgs e){}
private void dgvData_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
dgvData.ClearSelection();
}
}
And the implementation looks like this
public partial class SessionReviewForm : Core.DataFormBase
{
public QuestionSessionForm ParentSession
{
get; set;
}
public Session Session
{
get; set;
}
public SessionPart SessionPart
{
get; set;
}
/// <summary>
/// Shows the dialog form.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="sessionPart">The session part.</param>
/// <param name="parent">The parent.</param>
/// <returns></returns>
public static DialogResult ShowDialogForm(Session session, SessionPart sessionPart, QuestionSessionForm parent)
{
开发者_如何学Go // if any of the params are null get the hell out!
if (session == null || sessionPart == null || parent == null)
return DialogResult.None;
// create the new form, and populate its params
SessionReviewForm form = new SessionReviewForm()
{
Session = session,
SessionPart = sessionPart,
ParentSession = parent,
};
// populate the forms data grid
form.PopulateDataGrid();
form.Size = new System.Drawing.Size(400,400);
// show the form
return form.ShowDialog(parent);
}
/// <summary>
/// Populates the data grid with the required information
/// </summary>
/// <param name="instance">The instance for the w</param>
protected override void PopulateDataGrid()
{
// Get all of the questions that are marked for review
SessionQuestions questionsToDisplay = SessionPart.SessionQuestions.GetMarkedForReview();
// add to the list all of the questions that have not yet been answered
questionsToDisplay.AddRange(SessionPart.SessionQuestions.GetNotAnswered());
// create a list of objects for the data grid view
List<SessionReviewData> objectList = new List<SessionReviewData>();
// for each question in the session question list, populate a new member of
// the object list
foreach (SessionQuestion sq in questionsToDisplay)
{
SessionReviewData temp = new SessionReviewData
{
QuestionNumber = sq.Sequence + 1,
MarkedForReview = sq.MarkForReview,
IsAnswered = sq.IsAnswered
};
objectList.Add(temp);
}
// bind the data grid view to the object list
dgvData.DataSource = objectList;
// format the column headers so that they have a space between words
for (int i = 0; i < dgvData.Columns.Count; i++)
{
dgvData.Columns[i].HeaderText = Utilities.AddSpacesToSentence(dgvData.Columns[i].Name);
}
}
/// <summary>
/// Handles the CellClick event of the dgvQuestions control.
/// populates the parent form with the selected question
/// then closes the form.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Forms.DataGridViewCellEventArgs"/> instance containing the event data.</param>
protected override void dgvData_CellClick(object sender, DataGridViewCellEventArgs e)
{
// if the user selects the header column, then get out.
if (e.RowIndex == -1)
return;
// send the question data to the parentSession form to display the question
ParentSession.DisplayQuestionByIndex((int)(dgvData.Rows[e.RowIndex].Cells[0].Value) - 1);
// close this form
Close();
}
Solved: Turns out Narrange was alphabetizing my internal data structure, and that caused them to be listed in the wrong order.
Code that 'assumes' that something is going to be somewhere may cause you problems.
Simple solution would be to add the columns yourself as long as you always want the same columns in a certain order;
dataGridView1.AutoGenerateColumns = false;
DataGridViewColumn column = new DataGridViewColumn();
column.DataPropertyName = "Question Number";
column.HeaderText = "Question Number";
dataGridView1.Columns.Add(column);
... so on and so forth for each column you want
精彩评论