3

リストボックスを使用すると、選択したアイテムを抽出する次のコードがあります。

    private void inventoryList_SelectedIndexChanged(object sender, EventArgs e)
    {
        String s = inventoryList.SelectedItem.ToString();
        s = s.Substring(0, s.IndexOf(':'));
        bookDetailTable.Rows.Clear();
        ...
        more code
        ...
    }

つまり、選択が変更されたときに、選択された行の最初のセルの内容を取得します。問題は、そのデータ要素にアクセスする方法がわからないことです。

どんな助けでも大歓迎です。

4

2 に答える 2

15

これがあなたが探しているものだと思います。しかし、そうでない場合は、うまくいけば、それがあなたの出発点になるでしょう.

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    DataGridView dgv = (DataGridView)sender;

    //User selected WHOLE ROW (by clicking in the margin)
    if (dgv.SelectedRows.Count> 0)
       MessageBox.Show(dgv.SelectedRows[0].Cells[0].Value.ToString());

    //User selected a cell (show the first cell in the row)
    if (dgv.SelectedCells.Count > 0)
        MessageBox.Show(dgv.Rows[dgv.SelectedCells[0].RowIndex].Cells[0].Value.ToString());

    //User selected a cell, show that cell
    if (dgv.SelectedCells.Count > 0)
        MessageBox.Show(dgv.SelectedCells[0].Value.ToString());
}
于 2009-01-01T06:56:20.283 に答える