简体   繁体   English

公开dataGridView1

[英]making dataGridView1 public

Im trying to make my dataGridView public so I can access it from another class. 我正在尝试将我的dataGridView公开,以便可以从另一个类访问它。 I attempted to do so by doing this 我试图这样做

public DataGridView dgv = new DataGridView();

To test this change out, I tried populating it on Form1 with information using 为了测试此更改,我尝试使用以下信息在Form1上填充该更改:

dgv.DataSource = prevEx.ToList();

It would previously get filled with information but now it doesnt get populated at all, and I'm left staring at an empty DGV . 它以前会充满信息,但现在根本不填充信息,而我只盯着一个空的DGV I know for a fact prevEx has data in it because prior to making it public, the DGV would get populated. 我知道prevEx里面有数据,因为在将其公开之前, DGV会被填充。

I know I'm initializing the new DGV prior to the step in which it is supposed to populate the field (its up top with all my other public variables), so thats not the problem so as of now I'm at a loss. 我知道我要在应该填充该字段的步骤之前初始化新的DGV(使用其他所有公共变量填充它的顶部),所以这不是问题,因为现在我很茫然。 Any help would be awesome. 任何帮助都是极好的。

To clear up some confusion here is more code. 为了消除混乱,这里有更多代码。

public partial class Form1 : Form
    {
    public static Microsoft.Office.Interop.Excel._Application excelApp = new Microsoft.Office.Interop.Excel.Application() { DisplayAlerts = false, Visible = false };
    System.Diagnostics.Process proc = System.Diagnostics.Process.GetCurrentProcess();

    public List<Attorney> listOfAttys = Helpers.getAttorneys();
    public static DataGridView dgv = new DataGridView();

    public static string fileName = null;
    public static string chatterCase = null;
    public static string userName = null;
    public static string attorneyNum = null;
    public static int batchID;
    public static string ID;
    public static DateTime dateHold;
    public static string holdDesc = null;
    public static int startingRow;
    public static string holdAccount = null;

    public void btnLoad_Click(object sender, EventArgs e)
    {
        txtbxFilename.Text = null;
        txtbxChatterCase.Text = null;
        txtbxUserName.Text = null;
        txtbxRowNum.Text = null;

        System.IO.Stream myStream = null;
        OpenFileDialog openFileDialog1 = new OpenFileDialog();

        openFileDialog1.InitialDirectory = "C:\\";
        openFileDialog1.Filter = "Excel files |*.xls;*.xlsx";
        openFileDialog1.FilterIndex = 2;
        openFileDialog1.RestoreDirectory = true;

        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            try
            {
                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    fileName = txtbxFilename.Text = openFileDialog1.FileName;

                    myStream.Close();
                    myStream.Dispose();
                }
            }
            catch (Exception ex)
            {
                excelApp.Quit();
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }
        }

        var prevEx = LegalTransactionList.previewExcel();
        dgv.DataSource = prevEx.ToList();

    }

Declare with a static tag too: 还要声明一个静态标签:

public static DataGridView dgv = new DataGridView();

This will make it retain data between classes (globally). 这将使其在类之间(全局)保留数据。

In your other class call it ass such: 在您的其他课程中,将其称为ass:

MyClassName.dgv.DataSource = prevEx.ToList();

Make it with a static tag: 用一个静态标签:

  public static DataGridView dgv = new DataGridView();

Assuming this control belongs to Class Foo, to access it another class simply use 假设此控件属于Class Foo,则只需使用以下命令即可访问另一个控件

  Foo.dgv.DataSource = prevEx.ToList();

If class Foo has been instantiated in the class that needs to call it ie 如果类Foo已在需要调用的类中实例化,即

   Foo bar = new Foo();

Then you would not need the static tag and it would be accessed as such: 然后,您将不需要静态标记,并且可以这样访问它:

bar.dgv.DataSource = prevEx.ToList();

However my recomendation would be to make a public static list, and set the data source inside the same class as dgv, instead of making the dgv public and static. 但是,我的建议是创建一个公共静态列表,并将数据源设置为与dgv相同的类,而不是将dgv设为公共和静态。

And one last suggestion, try setting the dataSource to null first. 最后一个建议是,尝试首先将dataSource设置为null。

  dgv.DataSource = null;
  dgv.DataSource = prevEx.ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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