DataGridView可见性

时间:2018-01-01 15:16:31

标签: c# winforms datagridview

我想从未创建DatagridView的方法访问DatagridView。我知道一个简单的补救方法是将DatagridView设置为类变量,但是必须有一种方法可以将DataGridView从方法传递给方法。

在我的方法btnManipulateGrid_Click()中,我收到了

的编译错误
  

当前上下文中不存在名称datagridviewonetwo

我认为这是由于"范围"对于网格,因为我在方法btnDynamicallyAddStuf_Click()中创建了网格,所以网格只对所述方法可见。

所以我的问题是,除了将网格声明为公共类变量之外,我如何将网格传递给同一类中的其他方法?

private void btnDynamicallyAddStuf_Click(object sender, EventArgs e)
{
    Panel pnladdedthroughcode = new Panel();
    DataGridView datagridviewonetwo = new DataGridView();
    Button xprtToExcel = new Button();
    datagridviewonetwo.AllowUserToAddRows = false;
    datagridviewonetwo.AllowUserToDeleteRows = false;
    datagridviewonetwo.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
    datagridviewonetwo.Dock = System.Windows.Forms.DockStyle.Fill;
    datagridviewonetwo.Location = new System.Drawing.Point(0, 0);
    datagridviewonetwo.Name = "datagridviewonetwo";
    datagridviewonetwo.ReadOnly = true;
    datagridviewonetwo.Size = new System.Drawing.Size(665, 362);
    datagridviewonetwo.TabIndex = 3;
    Controls.Add(pnladdedthroughcode);
    pnladdedthroughcode.ResumeLayout(false);
    pnladdedthroughcode.PerformLayout();
    pnladdedthroughcode.Controls.Add(datagridviewonetwo);
    pnladdedthroughcode.Location = new System.Drawing.Point(16, 49);
    pnladdedthroughcode.Name = "pnladdedthroughcode";
    pnladdedthroughcode.Size = new System.Drawing.Size(665, 362);
    pnladdedthroughcode.TabIndex = 4;
    datagridviewonetwo.DataSource = DataTableCoded;
    datagridviewonetwo.AutoResizeColumns();
    btnManipulateGrid.Location = new System.Drawing.Point(546, 28);
    btnManipulateGrid.Name = "btnManipulateGrid";
    btnManipulateGrid.Size = new System.Drawing.Size(122,21);
    btnManipulateGrid.TabIndex = 7;
    btnManipulateGrid.Text = "Manipulate Data";
    btnManipulateGrid.UseVisualStyleBackColor = true;
    btnManipulateGrid.Click += new System.EventHandler(btnManipulateGrid_Click);
    Controls.Add(btnManipulateGrid);
}

private void btnManipulateGrid_Click(object sender, EventArgs e)
{
    //this line gives me the error
    var csvString = datagridviewonetwo.ForEachCell(wrapper => EscapeSpecialChars(wrapper, '&', '/')).ChangeRows(RemoveExtraData).ToCsvString();
}

2 个答案:

答案 0 :(得分:1)

您已手动将网格添加到手动创建的面板,然后将面板添加到窗体控件集合。

如果你想避免使用'类级别变量'(我没有看到这个决定背后的原因)你应该按照相同的路径恢复网格

Panel pnl = this.Controls.OfType<Panel>();
if(pnl != null)
{
   DataGridView grid = pnl.Controls.OfType<DataGridView>();
   .... do stuff with the grid
}

当然,仅当您在表单控件集合中手动添加了面板时,此方法才有效。如果不是这种情况,您应该使用更精确的方法通过Name属性

查找控件
Panel pnl = this.Controls.OfType<Panel>()
                .FirstOrDefault(z => z.Name == "pnladdedthroughcode");
if(pnl != null)
   .....

最后注意,类级变量不需要设置为 public 。如果您计划仅在该表单方法中使用该网格,那么您只需将类级别变量声明为私有,并删除从控件层次结构中检索网格的所有代码。

public MyForm: Form
{
    // This is visible only inside your form class....
    private DataGridView datagridviewonetwo = null;
}

答案 1 :(得分:0)

这是因为datagridviewonetwo是一个局部变量。你需要找到控件。尝试使用asp.net(使用System.Web.UI)

DataGridView myCtl = FindControl("datagridviewonetwo") as DataGridView ;
var csvString = myCtl.ForEachCell(wrapper => EscapeSpecialChars(wrapper, '&', '/')).ChangeRows(RemoveExtraData).ToCsvString();

如果是windows窗体,请尝试

Control[] dgv= this.Controls.Find("datagridviewonetwo", true);

dgv [0]不应该为null,它是一个datagridview。