You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell ISE中如何将DataGridView点击的邮箱单元格值填入TextBox

点击DataGridView邮箱单元格自动填充TextBox的实现方法

完全可以实现这个需求,只需要给DataGridView绑定CellClick事件,在点击单元格时把对应邮箱值同步到TextBox里。

完整代码示例

# 初始化DataGridView
$dataGrid = New-Object System.Windows.Forms.DataGridView
$dataGrid.Width = 503
$dataGrid.Height = 250
$dataGrid.location = new-object system.drawing.point(120,380)
$dataGrid.DataSource = $DataTable
$dataGrid.ReadOnly = $true
$dataGrid.RowHeadersVisible = $false
$dataGrid.AllowUserToAddRows = $false
$dataGrid.AutoSizeColumnsMode = 'Fill'

# 初始化TextBox
$emailMaj = New-Object Windows.Forms.TextBox
$emailMaj.Location = New-Object Drawing.Point 200,300
$emailMaj.Size = New-Object Drawing.Point 130,30

# 绑定CellClick事件,实现点击单元格填充邮箱
$dataGrid.add_CellClick({
    param($sender, $e)
    # 这里假设你的邮箱列名为"email",可根据实际列名修改
    if ($sender.Columns[$e.ColumnIndex].Name -eq "email") {
        # 获取点击单元格的值并赋值给TextBox
        $emailMaj.Text = $sender.Rows[$e.RowIndex].Cells[$e.ColumnIndex].Value.ToString()
    }
})

关键说明

  • 事件绑定:通过add_CellClick方法给DataGridView添加点击事件处理器
  • 列判断:可以通过列名(如示例中的"email")或者列索引确认点击的是邮箱列,根据你的DataTable结构调整即可
  • 值赋值:直接从点击的单元格中取出值,转换为字符串后赋值给TextBox的Text属性

界面参考

DataGridView与TextBox界面截图

内容的提问来源于stack exchange,提问作者Cinox

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 01:37:10