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

WinForm C#:如何实现DataGridView批量填充对应TextBox无需逐行选择

实现方法

核心思路

直接遍历DataGridView的前6行(如果行数足够),将每行对应列的数据依次赋值给对应的TextBox组,无需依赖选中事件,直接从Rows集合中读取数据即可。

具体代码示例

假设你的TextBox组按命名规则区分(比如txtNombre1、txtApellido1对应第一行,txtNombre2、txtApellido2对应第二行,以此类推),可以这样编写按钮点击事件:

private void btnCargarDatosCasamiento_Click(object sender, EventArgs e)
{
    // 取实际行数和6的较小值,避免越界
    int targetRows = Math.Min(dataGridView1.Rows.Count, 6);
    
    for (int i = 0; i < targetRows; i++)
    {
        DataGridViewRow row = dataGridView1.Rows[i];
        // 跳过DataGridView自动生成的空行
        if (row.IsNewRow) continue;
        
        // 通过控件名称查找对应TextBox
        TextBox txtNombre = (TextBox)this.Controls.Find($"txtNombre{i+1}", true)[0];
        TextBox txtApellido = (TextBox)this.Controls.Find($"txtApellido{i+1}", true)[0];
        
        // 读取单元格数据,处理null值避免异常
        txtNombre.Text = row.Cells["Nombre"].Value?.ToString() ?? string.Empty;
        txtApellido.Text = row.Cells["Apellido"].Value?.ToString() ?? string.Empty;
        
        // 如需填充其他字段,继续添加对应TextBox的赋值逻辑
    }
    
    // 若数据不足6行,清空剩余TextBox
    if (dataGridView1.Rows.Count < 6)
    {
        for (int i = dataGridView1.Rows.Count; i < 6; i++)
        {
            TextBox txtNombre = (TextBox)this.Controls.Find($"txtNombre{i+1}", true)[0];
            TextBox txtApellido = (TextBox)this.Controls.Find($"txtApellido{i+1}", true)[0];
            txtNombre.Text = string.Empty;
            txtApellido.Text = string.Empty;
        }
        MessageBox.Show("数据不足6行,已填充现有数据并清空剩余输入框");
    }
}

简化方案:使用控件数组

如果不想通过名称查找控件,可以提前将TextBox组存入数组,代码更简洁:

// 在窗体加载时初始化控件数组
private TextBox[] _nombreTextBoxes;
private TextBox[] _apellidoTextBoxes;

private void Form1_Load(object sender, EventArgs e)
{
    _nombreTextBoxes = new[] { txtNombre1, txtNombre2, txtNombre3, txtNombre4, txtNombre5, txtNombre6 };
    _apellidoTextBoxes = new[] { txtApellido1, txtApellido2, txtApellido3, txtApellido4, txtApellido5, txtApellido6 };
}

private void btnCargarDatosCasamiento_Click(object sender, EventArgs e)
{
    int targetRows = Math.Min(dataGridView1.Rows.Count, 6);
    
    for (int i = 0; i < targetRows; i++)
    {
        DataGridViewRow row = dataGridView1.Rows[i];
        if (row.IsNewRow) continue;
        
        _nombreTextBoxes[i].Text = row.Cells["Nombre"].Value?.ToString() ?? string.Empty;
        _apellidoTextBoxes[i].Text = row.Cells["Apellido"].Value?.ToString() ?? string.Empty;
    }
    
    // 清空剩余TextBox
    for (int i = targetRows; i < 6; i++)
    {
        _nombreTextBoxes[i].Text = string.Empty;
        _apellidoTextBoxes[i].Text = string.Empty;
    }
}

关键注意事项

  • 必须判断row.IsNewRow,避免读取DataGridView自动生成的空行
  • 使用?.ToString() ?? string.Empty处理单元格值为null的情况,防止空引用异常
  • 如果通过列索引而非列名访问数据,将row.Cells["Nombre"]替换为row.Cells[0]这类索引即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 02:23:29