无法隐式转换类型system.windows.forms.datagridtextboxcolumn到system.windows.forms.datagridviewcolumn M'alerter

问题描述 投票:1回答:1

我真的陷入了创建具有2列的动态datagridview的这一点,我使用了datgridtextboxcolumns,但我一直收到此错误

无法隐式转换类型system.windows.forms.datagridtextboxcolumn到system.windows.forms.datagridviewcolumn“for”dgviewcol1“和”dgviewcol2

当我尝试使用AddRange添加新列时。我也有错误说在datagridviewtextboxcolumn中不存在Visible属性,这是false。

任何人都知道我怎么能摆脱这个错误?非常感激!

private DataGridView dgview;
    private DataGridTextBoxColumn dgviewcol1;
    private DataGridTextBoxColumn dgviewcol2;
    void Search()
    {
        dgview = new DataGridView();
        dgviewcol1 = new DataGridTextBoxColumn();
        dgviewcol2 = new DataGridTextBoxColumn();
        this.dgview.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dgview.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {this.dgviewcol1, this.dgviewcol2}); // "cannot implicitly convert type system.windows.forms.datagridtextboxcolumn to system.windows.forms.datagridviewcolumn"
        dataGridView2.Visible = false;
        this.dgviewcol1.Visible = false; // Visible property doesn't exist in datagridviewtextboxcolumn
        this.dgviewcol2.Visible = false;
        this.Controls.Add(dgview);
        this.dgview.ReadOnly = true;
        dgview.BringToFront();

    }
c# asp.net windows-forms-designer
1个回答
1
投票

您正在尝试将DataGridTextBoxColumn添加到DataGridView而不是添加DataGridViewTextBoxColumn,因此编译器会尝试将其转换为引发异常的DataGridViewColumn。

private DataGridView dgview;
private DataGridViewTextBoxColumn dgviewcol1;
private DataGridViewTextBoxColumn dgviewcol2;
void Search()
{
    dgview = new DataGridView();
    dgviewcol1 = new DataGridViewTextBoxColumn();
    dgviewcol2 = new DataGridViewTextBoxColumn();
    this.dgview.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
    this.dgview.Columns.AddRange(new System.Windows.Forms.DataGridViewTextBoxColumn[] {this.dgviewcol1, this.dgviewcol2}); // "cannot implicitly convert type system.windows.forms.datagridtextboxcolumn to system.windows.forms.datagridviewcolumn"
    dataGridView2.Visible = false;
    this.dgviewcol1.Visible = false; // Visible property doesn't exist in datagridviewtextboxcolumn
    this.dgviewcol2.Visible = false;
    this.Controls.Add(dgview);
    this.dgview.ReadOnly = true;
    dgview.BringToFront();

}

希望这可以解决您的问题

© www.soinside.com 2019 - 2024. All rights reserved.