字段初始值设定项不能引用非静态字段,方法或属性

Ort*_*und 0 c# delegates static-methods winforms

我的应用程序是从XLSX电子表格中获取大量数据(400k +记录),在表单上的DataGridView中显示每个工作表中的数据,还允许您将选定工作表的数据导出到mySQL中.

导出将发生在它自己的线程上(请注意,这里有很多不完整的):

    private AddItemCallBack AddItemDelegate = new AddItemCallBack(AddItemMethod);
    private delegate void AddItemCallBack(int Total);

    private void lnkExport_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        sName = ddlTables.SelectedValue.ToString();
        pb = progressBar1;

        var t = new Thread(() => ExportData(sName));
        t.Start();
    }

    private void ExportData(string SheetName = "")
    {
        string sql = "select * from " + String.Format(tablename, SheetName);
        OleDbCommand cmd = new OleDbCommand(sql, conn);

        DataSet ds = new DataSet();
        OleDbDataAdapter da = new OleDbDataAdapter();

        da.SelectCommand = cmd;
        da.Fill(ds);

        connStr = "Data Source=localhost; Initial Catalog=test; User ID=root; Password=Ly@12157114";
        MySqlConnection con = new MySqlConnection(connStr);
        totalRecords = ds.Tables[0].Rows.Count;
        currentRecords = 0;

        foreach (DataRow row in ds.Tables[0].Rows)
        {
            sql = "insert into planet(clubid, clubname, acctno, title, firstname, lastname, cell, email, derp, accttype) " +
            "values(@id, @name, @acct, @title, @fname, @lname, @cell, @email, @derp, @type)";

            MySqlCommand command = new MySqlCommand(sql, con);

            command.Parameters.AddWithValue("@id", row[0]);
            command.Parameters.AddWithValue("@name", row[1]);
            command.Parameters.AddWithValue("@acct", row[2]);
            command.Parameters.AddWithValue("@title", row[3]);
            command.Parameters.AddWithValue("@fname", row[4]);
            command.Parameters.AddWithValue("@lname", row[5]);
            command.Parameters.AddWithValue("@cell", row[6]);
            command.Parameters.AddWithValue("@email", row[7]);
            command.Parameters.AddWithValue("@derp", row[8]);
            command.Parameters.AddWithValue("@type", row[9]);

            try
            {
                con.Open();
                command.ExecuteNonQuery();
                con.Close();
                currentRecords = currentRecords + 1;

                this.Invoke(this.AddItemDelegate, new object[] { totalRecords });
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

    }

    private static void AddItemMethod(int Total)
    {
        progressBar1.Maximum = Total;

        if (progressBar1.Value < progressBar1.Maximum)
        {
            progressBar1.Value = progressBar1.Value + 1;
        }
        else
        {
            progressBar1.Value = 0;
        }            
    }
Run Code Online (Sandbox Code Playgroud)

VS抱怨说

错误1非静态字段,方法或属性'ImportExcel.Form1.progressBar1'需要对象引用G:\ ImportExcel\ImportExcel\Form1.cs 139 13 ImportExcel

对于AddItemMethod void中的progressBar1控件引用的每个实例.

我可以从void中删除静态类型定义,但是我在定义CallBack的行上得到了相同的错误,说AddItemMethod不是静态的.

真的不知道该怎么办.有什么建议?

Yiğ*_*ner 6

使AddItemMethod非静态,并使用构造函数初始化回调.

private AddItemCallBack AddItemDelegate;
private delegate void AddItemCallBack(int Total); 

private void AddItemMethod(int Total)
{
    progressBar1.Maximum = Total; 
    if (progressBar1.Value < progressBar1.Maximum)
    {
        progressBar1.Value = progressBar1.Value + 1;
    } 
    else 
    { 
        progressBar1.Value = 0; 
    }
}

public Form1()
{
    InitializeComponent();
    AddItemDelegate = new AddItemCallBack(AddItemMethod);
}
Run Code Online (Sandbox Code Playgroud)