トップ回答者
dataGridViewでカレントセルの位置座標(ピクセル単位)を取得する方法?

質問
-
入力時にカレントセルの横に入力候補をリストに持つlistBoxを表示するためにカレントセルの座標を知りたくてお尋ねします。どうぞよろしくお願いします。
<今までの方法> dataGridView1.Rows[0].Height * DataGridView1.CurrentCell.RowIndex
<今までの方法の欠点> 入力ミスを訂正する際,スクロールして対象のセルをカレントセルにしたときにlistBoxの表示位置が大きく下にずれてしまいます。
<対策>listbox1.Top = (カレントセルのY座標) とできたらいいのですが,
<次善の対策>”現在表示されている先頭の行”の行番号がわかるとよいのですが,
zen73
回答
-
DataGridView.GetCellDisplayRectangle メソッドを使ってはどうでしょう?
CurrentCell のロケーションを下記のようにして取得できます。
if (dataGridView1.CurrentCell != null) { var r = dataGridView1.GetCellDisplayRectangle( dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex, false); var y = r.Location.Y; }
なかむら(http://d.hatena.ne.jp/griefworker) -
リファレンスですので最初は少し取っつき難いかもしれませんが、MSDNと仲良くなることをお勧めします。
DataGridView メンバ
http://msdn.microsoft.com/ja-jp/library/system.windows.forms.datagridview_members(VS.80).aspxここから<次善の対策>に関することも見つけることができます。
FirstDisplayedScrollingRowIndex プロパティ
http://msdn.microsoft.com/ja-jp/library/system.windows.forms.datagridview.firstdisplayedscrollingrowindex(VS.80).aspx
★良い回答には回答済みマークを付けよう! わんくま同盟 MVP - Visual C# http://d.hatena.ne.jp/trapemiya/- 回答としてマーク zen73 2010年8月16日 5:34
すべての返信
-
DataGridView.GetCellDisplayRectangle メソッドを使ってはどうでしょう?
CurrentCell のロケーションを下記のようにして取得できます。
if (dataGridView1.CurrentCell != null) { var r = dataGridView1.GetCellDisplayRectangle( dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex, false); var y = r.Location.Y; }
なかむら(http://d.hatena.ne.jp/griefworker) -
リファレンスですので最初は少し取っつき難いかもしれませんが、MSDNと仲良くなることをお勧めします。
DataGridView メンバ
http://msdn.microsoft.com/ja-jp/library/system.windows.forms.datagridview_members(VS.80).aspxここから<次善の対策>に関することも見つけることができます。
FirstDisplayedScrollingRowIndex プロパティ
http://msdn.microsoft.com/ja-jp/library/system.windows.forms.datagridview.firstdisplayedscrollingrowindex(VS.80).aspx
★良い回答には回答済みマークを付けよう! わんくま同盟 MVP - Visual C# http://d.hatena.ne.jp/trapemiya/- 回答としてマーク zen73 2010年8月16日 5:34