我一直在研究一个在数据网格视图中呈现一些特定元素的表单。 一切都实现了,并正常工作; 然后我决定改变事情。 我需要这个特定表单的许多不同版本,所以我创build了一个基本表单,我的其他表单可以从中派生出来。 当我实现这个基础表单的inheritance时,数据是反向列出的。 换句话说,在我做出改变之前,列出了列出的问题(问题编号,标记为回顾,回答是否定),因为列出数据的原因是什么(回答,标记为回顾,问题编号)。 这很重要的原因是我的代码假定问题编号是第一行,并且这些信息用于查找和显示特定的问题。
基本forms看起来像这样
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是alphabetizing我的内部数据结构,并导致他们被列在错误的顺序。
“假定”某个事情将会在某个地方的代码可能会导致你的问题。
简单的解决办法是自己添加列,只要你总是希望以相同的顺序相同的列;
dataGridView1.AutoGenerateColumns = false; DataGridViewColumn column = new DataGridViewColumn(); column.DataPropertyName = "Question Number"; column.HeaderText = "Question Number"; dataGridView1.Columns.Add(column);
…等等等等你想要的每一列