【问题标题】:Adding controls to DataGridView which is bound to a DataTable将控件添加到绑定到 DataTable 的 DataGridView
【发布时间】:2014-01-14 09:28:39
【问题描述】:

我正在开发一个WinForm 应用程序。在我的应用程序上,有一个 DataGridView 控件绑定到一个 DataTable 对象。现在我想在它的一个特定列中显示用户ProgressBar,但我只想使用DataTable 对象来做到这一点。我试过了,但似乎没有任何效果

            dataGridView1.DataSource = dataTable;

            DataColumn dc = new DataColumn("string", typeof(string));
            DataColumn dc2 = new DataColumn("Progress", typeof(ProgressBar));
            dataTable.Columns.Add(dc);
            dataTable.Columns.Add(dc2);

            DataRow dr = dataTable.NewRow();
            dr[0] = 1;
            ProgressBar p = new ProgressBar();
            p.Value = 35;
            dr[1] = p;
            dataTable.Rows.Add(dr);

也许我做错了。有没有其他方法可以做到这一点??

【问题讨论】:

    标签: c# winforms datagridview datatable


    【解决方案1】:

    试试下面的代码:

    DataTable _table = (dataGridView1.DataSource as DataTable);
    
    DataColumn dc = new DataColumn("string", typeof(string));
    DataColumn dc2 = new DataColumn("Progress", typeof(ProgressBar));
    _table.Columns.Add(dc);
    _table.Columns.Add(dc2);
    
    DataRow dr = _table.NewRow();
    dr["string"] = 1;
    ProgressBar p = new ProgressBar();
    p.Value = 35;
    dr["Progress"] = p;
    
    _table.Rows.Add(dr);
    

    在使用ColumnIndex 的状态下使用ColumnName 在DataRow 中插入值。

    请检查this link 在 DGV 单元中创建ProgressBar

    【讨论】:

    • 纳达。没有帮助:(。
    • 没有任何错误。只是空网格。有一件事要提,而不是DataTable _table = (dataGridView1.DataSource as DataTable); 我使用了dataGridView1.DataSource = dataTable; 因为它返回错误空对象。
    • @GaneshS 好的,您需要检查已编辑答案中的链接才能在 DGV 中添加 ProgressBar
    猜你喜欢
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 2013-06-20
    相关资源
    最近更新 更多