sorry,而不能用!
也就是说不让用户选中多行纪录!

解决方案 »

  1.   

    想选中多行,可加一CheckBox列!
      

  2.   

    我现在是想选中一行,是不是只能选中一行?
    是因为我在mouseup中作了处理,使当前行变蓝
    测试部门看到多条蓝数据,认为选中了多行?
      

  3.   

    1. 自定义DataGrid
    public class MyDataGrid : System.Windows.Forms.DataGrid
    2. 定义一个属性AllowMultiSelect, (假如你需要控制是否多选)
    private bool allowMultiSelect = false;
    public bool AllowMultiSelect
    {
       get
       {
          return this.allowMultiSelect;
       }
       set
       {
          this.allowMultiSelect = value;
       }
    }
    3. 定义一个变量储存原来选中的行号
    private int oldSelectedRow = -1;
    4. 重载OnMouseDown
    protected override void OnMouseDown(MouseEventArgs e)
    {
    // Record the time mouse is clicked
    dataGridMouseDownTime = DateTime.Now; //don't call the base class if in header 
    DataGrid.HitTestInfo hti = this.HitTest(new Point(e.X, e.Y));  if(hti.Type == DataGrid.HitTestType.Cell) 

    if( !allowMultiSelect && oldSelectedRow > -1) 
    {
    this.UnSelect(oldSelectedRow); 
    oldSelectedRow = hti.Row;
    } base.OnMouseDown(e); 

    else if(hti.Type == DataGrid.HitTestType.RowHeader) 

    if( allowMultiSelect )
    {
    base.OnMouseDown(e);
    }
    else
    {
    if(oldSelectedRow > -1 && oldSelectedRow < this.BindingContext[this.DataSource, this.DataMember].Count)  this.UnSelect(oldSelectedRow); 
    if((Control.ModifierKeys & Keys.Shift) == 0) 
    base.OnMouseDown(e); 
    else 
    this.CurrentCell = new DataGridCell(hti.Row, hti.Column); 
    this.Select(hti.Row); 
    oldSelectedRow = hti.Row; 
    }
    }
    else
    {
    base.OnMouseDown(e);
    }
    }
    5. 重载OnMouseMove
    protected override void OnMouseMove(MouseEventArgs e)
    {
    //don't call the base class if left mouse down 
    if( allowMultiSelect 
    || (!allowMultiSelect && e.Button != MouseButtons.Left) )
    base.OnMouseMove(e);
    }
    6. 重载OnCurrentCellChanged
    protected override void OnCurrentCellChanged(EventArgs e)
    {
    if( this.RowCount > 0 && !allowMultiSelect )
    {
    for(int i = 0; i < this.RowCount; i++)
    {
    if( this.IsSelected(i) )
    this.UnSelect(i);
    }
    } int rowIndex = this.CurrentRowIndex;
    if( rowIndex == this.oldCellIndex )
    return; this.oldCellIndex = rowIndex;
    base.OnCurrentCellChanged(e);
    }
      

  4.   

    Sorry, 上面的代码涉及到一些其他的功能.
    修改如下:
    6. 重载OnCurrentCellChanged
    protected override void OnCurrentCellChanged(EventArgs e)
    {
    if( this.RowCount > 0 && !allowMultiSelect )
    {
    for(int i = 0; i < this.RowCount; i++)
    {
    if( this.IsSelected(i) )
    this.UnSelect(i);
    }
    } base.OnCurrentCellChanged(e);
    }
    4. 去掉一下代码:
    // Record the time mouse is clicked
    dataGridMouseDownTime = DateTime.Now;
    另外上面用到了this.RowCount, 可以参考如下:
    public int RowCount
    {
    get
    {
    int count = 0;
    if( this.DataSource != null )
    {
    Type type = this.DataSource.GetType();
    if( type == typeof(System.Data.DataSet)
    || type.BaseType == typeof(System.Data.DataSet) )
    {
    count = this.BindingContext[this.DataSource, this.DataMember].Count;
    }
    else if( type == typeof(System.Data.DataView) )
    {
    count = ((System.Data.DataView)this.DataSource).Count;
    }
    else if( type == typeof(System.Data.DataTable) )
    {
    count = ((System.Data.DataTable)this.DataSource).Rows.Count;
    }
    } return count;
    }
    }
      

  5.   

    谢谢 lsfyfan(Jackfan) 老兄!
    还没有使你的代码,但无论如何,非常感谢
      

  6.   

    楼主,能不能告诉我如何改变DataGrid行的颜色,谢谢。
      

  7.   

    谢谢billten(海上星) 
    其实我是想改变DataGrid的每一行的颜色,颜色很多种哦
    datagrid.select(i);好像不行吧哪位能见教