请问如何让一个dataGrid只能选中一行?
即不能用Ctrl或Shift组和使用,以选择多行
谢谢~

解决方案 »

  1.   

    DataSet ds=Class_Comm.GetServiceAsswConf().GetAllNwTgtAlmPrd(this.TgtTypeseq,this.Sid,this.Tid);
    DataTable dt=ds.Tables["ttable"];
    this.dgdMain.DataSource=dt;
    this.dgdMain.Tag=dt;
    if (dt.Rows.Count>0)
    {
    this.dgdMain.Select(0);
    ShowDetail();
    }
    主要是用select()方法就可以确保你只选一个
      

  2.   

    public class DataGridNoActiveCellColumn : DataGridTextBoxColumn
     {
      private int SelectedRow = -1;
      protected override void Edit(System.Windows.Forms.CurrencyManager
       source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string
       instantText, bool cellIsVisible)
      {
       //make sure the selectrow is valid before trying to unselect
       if(SelectedRow > -1 && SelectedRow < source.List.Count + 1)
        this.DataGridTableStyle.DataGrid.UnSelect(SelectedRow);
       SelectedRow = rowNum;
       this.DataGridTableStyle.DataGrid.Select(SelectedRow);
      }
     }
      

  3.   

    谢谢laser_cs_cn(laser) ,但是我后台没有连接数据库
    谢谢tt5201(追星望月),你的方法看起来不错,待我再研究研究。
    直接把   if(SelectedRow > -1 && SelectedRow < source.List.Count + 1)
        this.DataGridTableStyle.DataGrid.UnSelect(SelectedRow);
       SelectedRow = rowNum;
       this.DataGridTableStyle.DataGrid.Select(SelectedRow);拷贝到相应的dataGrid里就可以了吗?(刚刚开始接触C#,不要笑话,谢谢)
      

  4.   

    最好能告诉我,可以加在    private void dataGrid1_Click(object sender, System.EventArgs e)
        { }里的代码
      

  5.   

    One way to do this is to derive a DataGrid, override its OnMouseDown and OnMouseMove methods. In the OnMouseDown, handle selecting and unselecting in your code without calling the base class if the click is on the header. In the OnMouseMove, don't call the baseclass to avoid dragging selections. Below is a code snippet for a sample derived DataGrid. You can download a full project (C#, VB).
    public class MyDataGrid : DataGrid
    {
         private int oldSelectedRow = -1;
         
         protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
         {
              //don't call the base class if left mouse down
              if(e.Button != MouseButtons.Left)
                   base.OnMouseMove(e);
         }     protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
         {
              //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(oldSelectedRow > -1)
                        this.UnSelect(oldSelectedRow);
                   oldSelectedRow = -1;
                   base.OnMouseDown(e);
              }
              else if(hti.Type == DataGrid.HitTestType.RowHeader)
              {
                   if(oldSelectedRow > -1)
                        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;
              }
         }

    ======================================
    http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q839q
      

  6.   

    zhzuo(秋枫) 你的方法,确实解决了只让用户可以选择单行的问题。但是列宽却被锁死,不能拉动。
    请问如何修改呢?
      

  7.   

    http://www.syncfusion.com/FAQ/WindowsForms
    找找看.
      

  8.   

    cdo(VC?我才刚学) : 谢谢
      你推荐的网站真不错,等我有时间了详细看看