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

如何将sfTextBoxExt中的值添加到Syncfusion SfDataGrid中?

实现SfTextBoxExt数据添加到SfDataGrid的方法

核心思路

你已经使用DataTable作为SfDataGrid的数据源,只需获取两个SfTextBoxExt的输入内容,向该DataTable中添加新行即可,SfDataGrid会自动同步更新数据。

具体代码示例

假设你通过按钮触发添加操作,在按钮的点击事件中编写如下代码:

private void btnAdd_Click(object sender, EventArgs e)
{
    // 获取两个文本框的输入值
    string variantInput = sfTextBoxVariant.Text.Trim();
    string stokInput = sfTextBoxStok.Text.Trim();

    // 可选:空值校验,避免添加无效空行
    if (string.IsNullOrEmpty(variantInput) || string.IsNullOrEmpty(stokInput))
    {
        MessageBox.Show("请填写完整的Variant和Stok信息");
        return;
    }

    // 获取绑定到SfDataGrid的DataTable实例
    DataTable dataTable = A_2_2_GridVariant.DataSource as DataTable;
    if (dataTable == null) return;

    // 创建新行并赋值
    DataRow newRow = dataTable.NewRow();
    newRow["Variant"] = variantInput;
    newRow["Stok"] = stokInput;
    
    // 将新行添加到DataTable
    dataTable.Rows.Add(newRow);

    // 可选:清空文本框,方便后续输入
    sfTextBoxVariant.Clear();
    sfTextBoxStok.Clear();
}

关键说明

  • 替换代码中的sfTextBoxVariant和sfTextBoxStok为你实际使用的两个SfTextBoxExt控件名称。
  • DataTable自带数据变更通知机制,添加行后SfDataGrid会自动刷新显示新数据,无需额外调用刷新方法。
  • 空值校验逻辑可根据业务需求调整,比如允许某字段为空则移除对应判断。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 08:24:14