0
我對自動排序的datagridview有問題。 當我點擊列標題一次,它的值被排序,但三角形標記保持不變,所以它指向錯誤的方向。 第二次點擊時,它的三角形發生了變化(它現在將指向不同的方向,但在與datagridview中的值進行比較時是正確的) 然後,排序過程重複 - 因此,如果我想對列進行排序,則必須單擊標題兩次:首先要更改方向標記/字形,然後再排序值。爲什麼datagridview需要雙擊來對列進行排序
我並不認爲這很重要,但是對於這個DGV,我使用了BindingList作爲數據源。
設計師代碼:
//
// dgv
//
this.dgv.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dgv.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.monthColumn,
this.colorColumn});
this.dgv.Location = new System.Drawing.Point(22, 127);
this.dgv.Name = "dgv";
this.dgv.Size = new System.Drawing.Size(468, 164);
this.dgv.TabIndex = 0;
this.dgv.CellBeginEdit += new System.Windows.Forms.DataGridViewCellCancelEventHandler(this.dgv_CellBeginEdit);
this.dgv.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgv_CellClick);
this.dgv.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgv_CellEndEdit);
this.dgv.RowsAdded += new System.Windows.Forms.DataGridViewRowsAddedEventHandler(this.dgv_RowsAdded);
//
// monthColumn
//
this.monthColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells;
dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
this.monthColumn.DefaultCellStyle = dataGridViewCellStyle3;
this.monthColumn.HeaderText = "Miesiące";
this.monthColumn.Name = "monthColumn";
this.colorColumn.ValueType = typeof(int);
this.monthColumn.Width = 72;
//
// colorColumn
//
this.colorColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
dataGridViewCellStyle4.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
this.colorColumn.DefaultCellStyle = dataGridViewCellStyle4;
this.colorColumn.HeaderText = "Barwa";
this.colorColumn.Name = "colorColumn";
this.colorColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
this.colorColumn.ValueType = typeof(System.Drawing.Color);
綁定過程:
dgv.AutoGenerateColumns = false;
monthColumn.DataPropertyName = "Months";
colorColumn.DataPropertyName = "Color";
colorColumn.ReadOnly = false;
dgv.DataSource = bcolors;//BindingList
dgv.Sort(monthColumn, ListSortDirection.Ascending);
事件方法用於應用的backgroundColor行和禁止編輯colorColumn(只讀不工作,但是這不是問題的關鍵?這裏)
這並不在我的情況下改變什麼,我也真的不想此列可排序,這就是爲什麼我用NotSortable設置。 –