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

Infragistics UltraCombo已获取数据但无法加载填充问题

解决Infragistics UltraCombo加载数据库数据后为空的问题

问题根源

你的代码里有几个关键问题导致UltraCombo显示为空:

  • 控件未加入窗体控件集合:构造函数里仅实例化了currentLayoutControl,但未将其添加到窗体的Controls集合中——控件根本没渲染到窗体上,自然看不到数据。哪怕你说试过添加,也要确认代码里确实执行了这一步。
  • 数据库连接操作冗余:LoadUserGridLayouts里手动打开/关闭连接,但SqlDataAdapter会自动处理连接的开闭,手动操作反而可能引发连接状态异常,影响数据填充。
  • 数据绑定后未触发刷新:UltraCombo设置数据源后,有时需要显式刷新才能渲染出数据。

修正方案

1. 确保控件被添加到窗体

在窗体构造函数里实例化控件后,必须将其加入Controls集合,同时设置位置和大小避免被其他控件覆盖:

this.currentLayoutControl = new CurrentLayoutControl();
this.Controls.Add(currentLayoutControl);
// 根据你的窗体布局调整位置和大小
currentLayoutControl.Location = new Point(20, 20);
currentLayoutControl.Size = new Size(200, 25);

2. 修复数据库操作代码

用using语句自动释放数据库资源,移除冗余的连接开闭操作,同时删除SQL里的USE My_Database(连接字符串里已指定数据库,重复写会引发异常):

public DataTable LoadUserGridLayouts(string connString)
{
    DataTable dataTable = new DataTable();
    string sql = "SELECT User_Grid_Layout_Id, User_Grid_Layout FROM app.User_Grid_Layouts";

    using (SqlConnection conn = new SqlConnection(connString))
    using (SqlCommand command = new SqlCommand(sql, conn))
    using (SqlDataAdapter adapter = new SqlDataAdapter(command))
    {
        adapter.Fill(dataTable);
    }

    return dataTable;
}

3. 完善数据绑定逻辑

设置数据源后,显式调用Refresh方法确保控件渲染数据:

public void PopulateLayoutCombo()
{
    DataTable layouts = currentLayoutControl.LoadUserGridLayouts(suiteConnectionString);
    currentLayoutControl.DataSource = layouts;
    currentLayoutControl.ValueMember = "User_Grid_Layout_Id";
    currentLayoutControl.DisplayMember = "User_Grid_Layout";
    // 强制刷新控件
    currentLayoutControl.Refresh();
}

完整修正代码

public partial class MainForm : Form
{
    public CurrentLayoutControl currentLayoutControl;

    public MainForm()
    {
        InitializeComponent();
        this.currentLayoutControl = new CurrentLayoutControl();
        // 添加到窗体控件集合
        this.Controls.Add(currentLayoutControl);
        // 设置控件位置和大小
        currentLayoutControl.Location = new Point(20, 20);
        currentLayoutControl.Size = new Size(200, 25);
        PopulateLayoutCombo();
    }

    public void PopulateLayoutCombo()
    {
        DataTable layouts = currentLayoutControl.LoadUserGridLayouts(suiteConnectionString);
        currentLayoutControl.DataSource = layouts;
        currentLayoutControl.ValueMember = "User_Grid_Layout_Id";
        currentLayoutControl.DisplayMember = "User_Grid_Layout";
        currentLayoutControl.Refresh();
    }
}

public class CurrentLayoutControl : UltraCombo
{
    public DataTable LoadUserGridLayouts(string connString)
    {
        DataTable dataTable = new DataTable();
        string sql = "SELECT User_Grid_Layout_Id, User_Grid_Layout FROM app.User_Grid_Layouts";

        using (SqlConnection conn = new SqlConnection(connString))
        using (SqlCommand command = new SqlCommand(sql, conn))
        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
        {
            adapter.Fill(dataTable);
        }

        return dataTable;
    }
}

额外检查项

  • 确认suiteConnectionString里指定了正确的数据库(Initial Catalog=My_Database),且连接账号有读取app.User_Grid_Layouts表的权限。
  • 检查User_Grid_Layout字段是否有实际值——如果这个字段全为空,下拉列表会显示空白项。
  • 确认Infragistics 2021.2控件引用正确,与.NET 4.7.0无版本冲突。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 13:35:28