我一直致力于在数据网格视图中显示某些特定元素的表单。一切都已实施,并正常运作;然后我决定改变一切。我需要这个特定形式的许多不同版本,所以我创建了一个基本形式,我的其他形式可以从中派生出来。当我实现此基本表单的继承时,数据反向列出。换句话说,在我进行更改之前列出了列(问题编号,标记为已审查,已回答)但是,无论出于什么原因列出了数据(已回答,标记为要审核,问题编号)。这很重要的原因是我的代码假定问题编号是第一行,并且该信息用于查找并显示该特定问题。
基本形式如下所示
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();
}
}
实现看起来像这样
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)
{
// 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();
}
解决: 事实证明,Narrange将我的内部数据结构按字母顺序排列,这导致它们以错误的顺序列出。
答案 0 :(得分:1)
“假设”某些事情会在某处出现的代码可能会给您带来问题。
简单的解决方案是自己添加列,只要您始终希望按特定顺序使用相同的列;
dataGridView1.AutoGenerateColumns = false;
DataGridViewColumn column = new DataGridViewColumn();
column.DataPropertyName = "Question Number";
column.HeaderText = "Question Number";
dataGridView1.Columns.Add(column);
......等等你想要的每一栏