【问题标题】:how to add column dynamically in datagridview using c#如何使用c#在datagridview中动态添加列
【发布时间】:2010-03-04 07:03:07
【问题描述】:

如何使用c#在datagridview中动态添加列

【问题讨论】:

    标签: c# datagridview


    【解决方案1】:

    例如:

            DataGridViewColumn col = new DataGridViewTextBoxColumn();
            col.HeaderText = "Hi there";
            int colIndex = grid.Columns.Add(col);
    

    【讨论】:

    • 我得到 'DataGridViewColumn' 是一种类型,不能用作表达式。
    【解决方案2】:

    如果它像任何其他 .Net 网格控件:

    YourDataGridView.Columns.Add(New DataGridViewColumn());
    

    【讨论】:

    • 如果 DataGridViewColumn 的 CellType 属性为空,Columns.Add 可能会引发异常。我认为最好在添加之前设置 DataGridViewColumn 的 CellTemplate(CellType 是 CellTemplate 的运行时类型),或者使用从 DataGridViewColumn 继承的类,其中 CellTemplate 已经适当设置。
    • @Tim Goodman - 我同意,我的例子只是基本上展示了它是如何完成的。实际的实现会涉及更多。
    【解决方案3】:

    我的意思是在特定列上添加所有值,那么它可能会对您有所帮助。

    试试这个提示:

    for (int i=0; i< datagriview1.coulums.count -1; i++)
    {
      int total + = total + datagridview1.columns[i]
        .cell[index for columns you want to add].formatedvalue.toString();  
    
    }
    
    textbox1.text=total;
    

    试试那个代码。它可以帮助你。

    谢谢, 妮可

    【讨论】:

      【解决方案4】:

      我还测试了这段代码,你的意思是在特定列上添加所有值,那么它可能会对你有所帮助。 试试这个提示:

      gridView1.Columns.Clear();
      gridView1.Columns.Add(new GridColumn() {Caption = @"Caption", FieldName = "FieldName", Name = "FieldName", Visible = true});
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-31
        • 1970-01-01
        • 2021-08-03
        • 2021-04-22
        • 2011-08-29
        相关资源
        最近更新 更多