选中一行:
dataGrid1.Select(dataGrid1.CurrentRowIndex);你可以写在OnClick里面或者OnCurrentCellChanged事件里面

解决方案 »

  1.   

    gridMain.SelectedIndex = index ;
      

  2.   

    private void dgMain_Click(object sender, System.EventArgs e)
    {
    this.SetRow();
    } private void dgMain_CurrentCellChanged(object sender, System.EventArgs e)
    {
    this.SetRow();
    } private void SetRow()
    {
    index = this.dgMain.CurrentRowIndex;

    if( index > -1 )
    {
    this.dgMain.Select( index );
    }

    }
      

  3.   

    如果是winform可能要重写System.Windows.Forms.DataGridColumnStyle的Edit这个方法.web的没有做过.
      

  4.   

    "选中一行:
    dataGrid1.Select(dataGrid1.CurrentRowIndex);你可以写在OnClick里面或者OnCurrentCellChanged事件里面"这样以后,还是可以选中单个单元格(背景变灰),如何真正选择整行,就像在listview里一样
      

  5.   

    private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) 
     

     
         System.Drawing.Point pt = new Point(e.X, e.Y); 
     
         DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt); 
     
         if(hti.Type == DataGrid.HitTestType.Cell) 
     
         { 
     
              dataGrid1.CurrentCell = new DataGridCell(hti.Row, hti.Column); 
     
              dataGrid1.Select(hti.Row); 
     
         } 
     

      

  6.   

    问题初步解决,感谢飞鱼,不过DATAGRID第一次显示时默认为一行一列被选中变灰,如何解决,多谢各位相助!!!!
      

  7.   

    使用HitTestInfo 作楼主的工作简直就是用牛刀杀鸡的感觉
      

  8.   

    http://community.csdn.net/Expert/topic/3434/3434227.xml?temp=1.165408E-02
      

  9.   

    给你个参考:
    public class ViewColumnStyle : DataGridColumnStyle 
    {
    // The isEditing field tracks whether or not the user is
    // editing data with the hosted control.
    private bool isEditing;
    private int nSelectedRowIndex; public ViewColumnStyle() : base() 
    {
    nSelectedRowIndex = -1;
    } protected override void Abort(int rowNum)
    {
    isEditing = false;
    Invalidate();
    } protected override bool Commit
    (CurrencyManager dataSource, int rowNum) 
    {
    if (!isEditing)
    return true; isEditing = false; Invalidate();
    return true;
    } protected override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible) 
    {
    if (!this.ReadOnly)
    {
    return;
    }
    if(nSelectedRowIndex> -1 && nSelectedRowIndex< source.List.Count + 1)
    {
    this.DataGridTableStyle.DataGrid.UnSelect(nSelectedRowIndex);
    }
    this.DataGridTableStyle.DataGrid.Select(rowNum);
    nSelectedRowIndex= rowNum;
    } protected override Size GetPreferredSize(
    Graphics g, 
    object value) 
    {
    return new Size(100, 4);
    } protected override int GetMinimumHeight() 
    {
    return 4;
    } protected override int GetPreferredHeight(Graphics g, 
    object value) 
    {
    return 4;
    } protected override void Paint(Graphics g, 
    Rectangle bounds, 
    CurrencyManager source, 
    int rowNum) 
    {
    Paint(g, bounds, source, rowNum, false);
    }
    protected override void Paint(
    Graphics g, 
    Rectangle bounds,
    CurrencyManager source, 
    int rowNum,
    bool alignToRight) 
    {
    Paint(
    g,bounds, 
    source, 
    rowNum, 
    Brushes.Red, 
    Brushes.Blue, 
    alignToRight);
    }
    protected override void Paint(
    Graphics g, 
    Rectangle bounds,
    CurrencyManager source, 
    int rowNum,
    Brush backBrush, 
    Brush foreBrush,
    bool alignToRight) 
    {
    object objValue = GetColumnValueAtRow(source, rowNum);
    Rectangle rect = bounds;
    g.FillRectangle(backBrush,rect);
    rect.Offset(0, 2);
    rect.Height -= 2;
    g.DrawString(objValue.ToString(), 
    this.DataGridTableStyle.DataGrid.Font, 
    foreBrush, rect);
    } protected override void SetDataGridInColumn(DataGrid value) 
    {
    base.SetDataGridInColumn(value);
    }
    }
    public class ListDataGrid:System.Windows.Forms.DataGrid
    {
    private Point m_CurPoint;
    private DataView m_ListDataView;
    private DataGridTableStyle tableStyle;
    /// <summary>
    /// 指定数据绑定并显示
    /// </summary>
    /// <param name="dvw"></param>
    /// <param name="TemplateName"></param>
    public void SetListData(DataView dvw)
    {
    if (tableStyle==null)
    {
    tableStyle = new DataGridTableStyle();
    tableStyle.MappingName =dvw.Table.TableName;
    int numCols = dvw.Table.Columns.Count;
    ViewColumnStyle textColumn;
    for (int i=0;i<dvw.Table.Columns.Count;i++)
    {
    textColumn = new ViewColumnStyle();
    textColumn.ReadOnly=true;
    textColumn.NullText="";
    textColumn.HeaderText = dvw.Table.Columns[i].Caption;
    textColumn.MappingName = dvw.Table.Columns[i].ColumnName;
    tableStyle.GridColumnStyles.Add(textColumn);
    } tableStyle.AllowSorting=false;
    tableStyle.RowHeadersVisible=false;
    this.TableStyles.Clear();
    this.TableStyles.Add(tableStyle);
    }
    this.BeginInit();
    this.SetDataBinding(dvw,null);
    this.EndInit();
    dvw.AllowDelete=true;
    dvw.AllowEdit=true;
    m_ListDataView=dvw;
    }
    protected override bool ProcessDialogKey(System.Windows.Forms.Keys keyData)
    {
    if (this.DataSource==null)
    {
    return true;
    }
    switch (keyData)
    {
    case Keys.Space:
    if (OnSpace!=null)
    {
    OnSpace(this,this.CurrentRowIndex);
    }
    return true;
    case Keys.Return:
    if (OnReturn!=null)
    {
    OnReturn(this,this.CurrentRowIndex);
    }
    base.ProcessDialogKey(keyData);
    this.Select(this.CurrentRowIndex);
    return true;
    case Keys.Up:
    if (this.CurrentRowIndex==0)
    {
    this.Select(0);
    return true;
    }
    else
    {
    return false;
    }
    case Keys.Down:
    int nRowCount=0;
    if (this.ListDataView.AllowNew)
    {
    nRowCount=((DataView)this.DataSource).Table.Rows.Count;
    }
    else
    {
    nRowCount=((DataView)this.DataSource).Table.Rows.Count-1;
    }
    if (this.CurrentRowIndex==nRowCount && this.CurrentRowIndex!=-1)
    {
    this.Select(this.CurrentRowIndex);
    return true;
    }
    else
    {
    return false;
    }
    case Keys.Left:
    return true;
    case Keys.Right:
    return true;
    case Keys.Tab:
    base.ProcessDialogKey(keyData);
    return true;
    case Keys.Delete:
    return true;
    default:
    return false;
    }
    }
    }