WinForm中如何让Datagrid的某一列不能自动排序,其他列可以自动排序?

解决方案 »

  1.   

    没有太明白楼主的意思。
    这个好像跟Datagrid的某一列没什么联系吧,你从数据库中查询出来的数据是什么样的顺序,绑定到DataGrid中就是什么样的。
      

  2.   

    在属性生成器里取消排序该field
      

  3.   

    Datagrid好像不支持单独某一列不让它排序。想让它不能自动排序,就是所有的烈都不能排序。
    就把AllowSorting属性设成false就可以了。
      

  4.   

    Datagrid好像不支持单独某一列不让它排序。想让它不能自动排序,就是所有的烈都不能排序。
    就把AllowSorting属性设成false就可以了。如果你实在要实现这样的功能,可以重写DataGrid
      

  5.   

    我是说windows应用程序开发,让DataGrid的某一列不能自动排序,其他的可以自动排序。为什么要这样要求呢?我遇到的情况是:一个DataGrid,有一CheckBox列,如果你点击过他的ColumnHeader,那么他就处于自动排序状态(升序/降序),然后如果你点击某一checkBox,那么DataGrid就会闪一下后按照checkBox列进行重新排序,那么你就会不知道你刚才点击的那一行跑到哪里去了,让用户很不爽,用户不知道自己是选中了它刚才点击的复选框。
      

  6.   

    我说的是Windows应用程序开发哟,大家不要想成Web开发了。哈哈
      

  7.   

    大家可以去测试。目前的解决方案是:点击前记住你修改的行,修改后DataGrid会给你排序,排序后再去找该行,然后再把他设为当前行。一般用户不会去点击该列的ColumnHeader,所以我说的这种情况发生的可能性不大,不过我还是希望大家能给我一个好的解决方案。谢谢了,
      

  8.   

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    namespace QPManage.QPControls
    {
    /// <summary>
    /// qpDataGrid 的摘要说明。
    /// </summary>
    public class qpDataGrid:DataGrid
    {
    private const int WM_SETCURSOR = 32;
    private string _strMessge="",_strMessageBoxTitle="";
    int _intColumn=-1;

    /// <summary>
    /// 设置禁止排序列的属性
    /// </summary>
    /// <param name="intintColumn">禁止排序的列的索引</param>
    /// <param name="strMessge">提示消息内容</param>
    /// <param name="strMessageBoxTitle">提示消息框标题</param>
    public void SetSortAttribute(int intintColumn,string strMessge,string strMessageBoxTitle)
    {
    _intColumn=intintColumn;
    _strMessge=strMessge;
    _strMessageBoxTitle=strMessageBoxTitle;
    } protected override void WndProc(ref Message m)
    {
    if(m.Msg != WM_SETCURSOR)
    base.WndProc(ref m);
    else
    {
    // //see if you want the cursor - in col 1, rows 2, 3, 4
    Point pt = this.PointToClient(Control.MousePosition);
    DataGrid.HitTestInfo hti = this.HitTest(pt.X, pt.Y);
    // if(hti.Column == 1 && hti.Row > 1 && hti.Row < 5) 
    if(hti.Type == DataGrid.HitTestType.Cell) 
    Cursor.Current = Cursors.Hand;
    else //if not, call the baseclass
    base.WndProc(ref m);
    }
    } protected override void OnMouseDown(MouseEventArgs e)  
    {  
    Point pt = new Point(e.X, e.Y);  
    DataGrid.HitTestInfo hti = this.HitTest(pt);  
    if(hti.Type == HitTestType.ColumnHeader && hti.Column == _intColumn)  
    {  
    MessageBox.Show(_strMessge,_strMessageBoxTitle);
    return; //don't call baseclass  
    }  
    base.OnMouseDown(e);  
    }  }
    }
      

  9.   

    可以设置那一列的属性AllowSorting=false;
      

  10.   

    q_po_o() 的方案有用,不错,不过公司可能不采取自定义控件,结贴了