VB.NETでDataGridViewの選択セルの値を取得

書式
DataGridView名 dataGrdLst
Button名 cmdShow
選択セルの値
DataGridView名.SelectedCells(0).Value
選択セルの行番号
DataGridView名.SelectedCells(0).RowIndex
選択セルの列番号
DataGridView名.SelectedCells(0).ColumnIndex

1. DataGridViewの初期値設定

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'列数3 行数2
dataGrdLst.ColumnCount = 3
dataGrdLst.RowCount = 2
'列名を指定
dataGrdLst.Columns(0).HeaderText = "番号"
dataGrdLst.Columns(1).HeaderText = "名前"
dataGrdLst.Columns(2).HeaderText = "住所"
'1行目 セルのデータを設定
dataGrdLst.Rows(0).Cells(0).Value = 1001
dataGrdLst.Rows(0).Cells(1).Value = "山田 太郎"
dataGrdLst.Rows(0).Cells(2).Value = "東京 "
'2行 セルのデータを設定
dataGrdLst.Rows(1).Cells(0).Value = 2002
dataGrdLst.Rows(1).Cells(1).Value = "テスト 次郎"
dataGrdLst.Rows(1).Cells(2).Value = "大阪 "
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '列数3 行数2 dataGrdLst.ColumnCount = 3 dataGrdLst.RowCount = 2 '列名を指定 dataGrdLst.Columns(0).HeaderText = "番号" dataGrdLst.Columns(1).HeaderText = "名前" dataGrdLst.Columns(2).HeaderText = "住所" '1行目 セルのデータを設定 dataGrdLst.Rows(0).Cells(0).Value = 1001 dataGrdLst.Rows(0).Cells(1).Value = "山田 太郎" dataGrdLst.Rows(0).Cells(2).Value = "東京 " '2行 セルのデータを設定 dataGrdLst.Rows(1).Cells(0).Value = 2002 dataGrdLst.Rows(1).Cells(1).Value = "テスト 次郎" dataGrdLst.Rows(1).Cells(2).Value = "大阪 " End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    '列数3 行数2
    dataGrdLst.ColumnCount = 3
    dataGrdLst.RowCount = 2

    '列名を指定
    dataGrdLst.Columns(0).HeaderText = "番号"
    dataGrdLst.Columns(1).HeaderText = "名前"
    dataGrdLst.Columns(2).HeaderText = "住所"

    '1行目 セルのデータを設定
    dataGrdLst.Rows(0).Cells(0).Value = 1001
    dataGrdLst.Rows(0).Cells(1).Value = "山田 太郎"
    dataGrdLst.Rows(0).Cells(2).Value = "東京 "

    '2行 セルのデータを設定
    dataGrdLst.Rows(1).Cells(0).Value = 2002
    dataGrdLst.Rows(1).Cells(1).Value = "テスト 次郎"
    dataGrdLst.Rows(1).Cells(2).Value = "大阪 "


End Sub

2.選択しているセルの値の取得

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Private Sub cmdShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdShow.Click
'セルの値表示用の変数
Dim strLst As String
'選択セルの行番号
Dim rowIndex As Integer
'選択セルの列番号
Dim colIndex As Integer
'選択しているセルの値
strLst = dataGrdLst.SelectedCells(0).Value & vbCrLf
'選択しているセルの行番号
rowIndex = dataGrdLst.SelectedCells(0).RowIndex + 1 & vbCrLf
'選択しているセルの列番号
colIndex = dataGrdLst.SelectedCells(0).ColumnIndex + 1 & vbCrLf
'メッセージボックスで表示
MessageBox.Show("選択セルの値: " & strLst, "選択結果", MessageBoxButtons.OK, MessageBoxIcon.Information)
MessageBox.Show("行番号: " & rowIndex.ToString(), "選択結果", MessageBoxButtons.OK, MessageBoxIcon.Information)
MessageBox.Show("列番号: " & colIndex.ToString(), "選択結果", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Private Sub cmdShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdShow.Click 'セルの値表示用の変数 Dim strLst As String '選択セルの行番号 Dim rowIndex As Integer '選択セルの列番号 Dim colIndex As Integer '選択しているセルの値 strLst = dataGrdLst.SelectedCells(0).Value & vbCrLf '選択しているセルの行番号 rowIndex = dataGrdLst.SelectedCells(0).RowIndex + 1 & vbCrLf '選択しているセルの列番号 colIndex = dataGrdLst.SelectedCells(0).ColumnIndex + 1 & vbCrLf 'メッセージボックスで表示 MessageBox.Show("選択セルの値: " & strLst, "選択結果", MessageBoxButtons.OK, MessageBoxIcon.Information) MessageBox.Show("行番号: " & rowIndex.ToString(), "選択結果", MessageBoxButtons.OK, MessageBoxIcon.Information) MessageBox.Show("列番号: " & colIndex.ToString(), "選択結果", MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub
Private Sub cmdShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdShow.Click
    'セルの値表示用の変数
    Dim strLst As String
    '選択セルの行番号
    Dim rowIndex As Integer
    '選択セルの列番号
    Dim colIndex As Integer

    '選択しているセルの値
    strLst = dataGrdLst.SelectedCells(0).Value & vbCrLf
    '選択しているセルの行番号
    rowIndex = dataGrdLst.SelectedCells(0).RowIndex + 1 & vbCrLf
    '選択しているセルの列番号
    colIndex = dataGrdLst.SelectedCells(0).ColumnIndex + 1 & vbCrLf

    'メッセージボックスで表示
    MessageBox.Show("選択セルの値: " & strLst, "選択結果", MessageBoxButtons.OK, MessageBoxIcon.Information)
    MessageBox.Show("行番号: " & rowIndex.ToString(), "選択結果", MessageBoxButtons.OK, MessageBoxIcon.Information)
    MessageBox.Show("列番号: " & colIndex.ToString(), "選択結果", MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub

実行結果例
選択セルの値:山田 太郎
行番号:1
列番号:2

VB.net

Posted by arkgame