DataGridViewを使用して選択された行のデータを取得する方法は何ですか?

DataGridViewで選択された行のデータを取得するには、次のコードを使用できます:

// 获取DataGridView中选中的第一行
DataGridViewRow selectedRow = dataGridView1.SelectedRows[0];

// 获取选中行中的每个单元格的数据
string cell1Value = selectedRow.Cells[0].Value.ToString(); // 第一列的数据
string cell2Value = selectedRow.Cells[1].Value.ToString(); // 第二列的数据
// 以此类推

// 或者直接获取整行的数据
List<string> rowData = new List<string>();
foreach (DataGridViewCell cell in selectedRow.Cells)
{
    rowData.Add(cell.Value.ToString());
}

// 输出获取到的数据
foreach (string data in rowData)
{
    Console.WriteLine(data);
}

以上のコードでは、まず選択された最初の行を取得し、Cells属性を使用して各セルのデータを取得するか、行全体のデータを直接反復処理することができます。最後に、取得したデータを処理するために必要に応じて操作することができます。

コメントを残す 0

Your email address will not be published. Required fields are marked *


广告
広告は10秒後に閉じます。
bannerAds