简体   繁体   中英

DataGridView Visibility

I am wanting to access a DatagridView from a method that the DatagridView was not created in. I know an easy remedy would be to set the DatagridView as a class variable, but there must be a way of passing the DataGridView from method to method.

In my method btnManipulateGrid_Click() I get a compile error of

the name datagridviewonetwo does not exist in the current context

I think this is due to the "scope" of the grid, since I create the grid in the method btnDynamicallyAddStuf_Click() then the grid is only visible to said method.

So my question is, other than declaring the grid as a public class variable, how can I pass the grid to additional methods in the same class?

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();
}

You have added manually the grid to a manually created panel and then you have added the panel to the forms control collection.

If you want to avoid the use of a 'class level variable' (I don't see the reason behind this decision) you should recover the grid following the same path

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

Of course this works only if you have just the panel added manually in your forms control collection. If this is not the case you should use a more precise approach in finding the control through the Name property

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

Final note, a class level variable doesn't need to be set public . If you plan to use that grid only inside that form methods, then you simply declare the class level variable as private and get rid of all that code that retrieves the grid from your control hierarchy.

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

it is because datagridviewonetwo is a local variable. You need to find the control. Try if it is asp.net (using System.Web.UI )

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

If it is windows form, try

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

dgv[0] should not be null and it is a datagridview.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM