【问题标题】:Why datagridview needs double click to sort column为什么datagridview需要双击对列进行排序
【发布时间】:2011-10-07 10:34:07
【问题描述】:

我对自动排序的 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(只读不起作用?,但这不是重点)

【问题讨论】:

    标签: c# winforms .net-4.0


    【解决方案1】:

    您可以尝试更改列的排序模式吗?

    改变这个:

    this.colorColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
    

    进入这个:

    this.colorColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
    

    【讨论】:

    • 这不会改变我的情况,而且我真的不希望此列可排序,这就是我使用 NotSortable 设置的原因。
    猜你喜欢
    • 2022-12-05
    • 2018-11-30
    • 2011-10-10
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    相关资源
    最近更新 更多