2011-07-19 13 views
-1
protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight) 
{ 
    RectangleF textBounds;// Bounds of text 
    Object cellData;// Object to show in the cell 

    DrawBackground(g, bounds, rowNum, backBrush);// Draw cell background 

    bounds.Inflate(-2, -2);// Shrink cell by couple pixels for text. 

    textBounds = new RectangleF(bounds.X, bounds.Y, bounds.Width, bounds.Height); 
    // Set text bounds. 
    cellData = this.PropertyDescriptor.GetValue(source.List[rowNum]); // Get data for this cell from data source. 

    g.DrawString(FormatText(cellData), this.Owner.Font, foreBrush, textBounds, this.StringFormat); 
    // Render contents 
    this.updateHostedControl();// Update floating hosted control. 
} 

// Would reposition, hide and show hosted control as needed. 
protected void updateHostedControl() 
{ 
    Rectangle selectedBounds = this.Owner.GetCellBounds(this.Owner.CurrentCell.RowNumber, this.Owner.CurrentCell.ColumnNumber); 
    // Get selected cell bounds. 
    // We only need to show hosted control if column is not read only, 
    // selected cell is in our column and not occluded by anything. 

    if (!this.ReadOnly && (this.ColumnOrdinal == this.Owner.CurrentCell.ColumnNumber) && 
     this.Owner.HitTest(selectedBounds.Left, selectedBounds.Top).Type == DataGrid.HitTestType.Cell && 
     this.Owner.HitTest(selectedBounds.Right, selectedBounds.Bottom).Type == DataGrid.HitTestType.Cell) 
    { 
     if (selectedBounds != this._bounds)// See if control bounds are already set. 
     { 
      this._bounds = selectedBounds; // Store last bounds. Note we can't use control's bounds 
      // as some controls are not resizable and would change bounds as they pleased. 
      this.HostedControl.Bounds = selectedBounds;// Update control bounds. 

      this.HostedControl.Focus(); 
      this.HostedControl.Update();// And update control now so it looks better visually. 
     } 

     if (!this.HostedControl.Visible)// If control is not yet visible... 
     { 
      this.HostedControl.Show();// Show it 
      this.HostedControl.Focus(); 
     } 
    } 
    else if (this.HostedControl.Visible)// Hosted control should not be visible. Check if it is. 
    { 
     this.HostedControl.Hide();// Hide it. 
    } 

} 

protected virtual void DrawBackground(Graphics g, Rectangle bounds, int rowNum, Brush backBrush) 
{ 
    Brush background = backBrush;// Use default brush by... hmm... default. 

    if ((null != background) && ((rowNum & 1) != 0) && !Owner.IsSelected(rowNum)) 
    {                 
     // If have alternating brush, row is odd and not selected... 
     background = _alternatingBrush;// Then use alternating brush. 
    } 
    else 
     background = new SolidBrush(Color.White); 

    g.FillRectangle(background, bounds);// Draw cell background 
} 

カスタムデータグリッドとボタンが付いていますが、データグリッドからボタンにフォーカスを移すコードを書くことができません。デフォルトのデータグリッドまたはボタンにフォーカスがあります。誰でも助けてくれます。私はキーを押したときにフォーカスを移したいと思っています。DataGridからボタンにフォーカスを移動するにはどうすればいいですか?

アドバンスありがとうございます。

答えて

1

DataGridでKeyPressedイベントを使用し、Enterキーを押してからフォーカスボタンを押します。あなたが任意の特定の問題を抱えていけない場合、これは、動作するはずです:

private void datagrid_KeyPress(object sender, KeyPressEventArgs e) 
{ 
    if (e.KeyChar == (char)Keys.Enter) 
     button1.Focus(); 
} 
+0

を私は、ユーザーが値を編集できたり、データグリッドがなければならないことを望む特定の行の値を編集しますが、ボタン – Shilpa

+0

1にフォーカスを転送されていないキーEnterキーを押すと読み取り専用ですか? 2. DataGridをどのように埋めているかを示します。 3.あなたの古い質問への回答を受け入れることを考える。 – Reniuz

+0

DataGridは編集可能です(データグリッドがカスタマイズされています).1つのテキストボックスと1つのコンボボックスが追加されました – Shilpa

関連する問題