开发者

c# winform Invoke throws NullReferenceException

i'm working on an winform(mdi) pro. And I need to update a dataGridView control when i get new data from another thread. and when new data comes and i'm dragging the dataGridview scroll, it throw a nullreference exception in dataGridView.Invoke. i have searched for few days and drove google crazy,but didn't help. the code like this:

  
    public void ReceiveNewData(object sender, UpateEventArgs ex)
    {
        if (this.dataGridView.InvokeRequired)
        {
       开发者_开发问答     dataGridView.Invoke(new UpateEventHandler(ReceiveNewData), new object[] { this, ex });
        }
        else
            this.BindNewData();
    }

    private void BindNewData()
     {

        if (dataGridView!= null && (QuoteMember.listOneClickQuoteItem != null || QuoteMember.listMarketingQuoteItem != null))
        {
            DataTable dataSource = PublicFunction.ToDataTable(QuoteMember.listOneClickQuoteItem);
            if (dataSource != null)
                    dataSource.Merge(PublicFunction.ToDataTable(QuoteMember.listMarketingQuoteItem), true);
                else
                    dataSource = PublicFunction.ToDataTable(QuoteMember.listMarketingQuoteItem);
            dataGridView.DataSource = dataSource;
        }
    }

public PublicFunction
{
        public static DataTable ToDataTable(List dataSource)
        {
            if(dataSource != null)
                return ToDataTable((dataSource.ToArray()), 1);
            return null;
        }

        public static DataTable ToDataTable(List dataSource) 
        {
            if (dataSource != null)
                return ToDataTable((dataSource.ToArray()), 2); 
            return null; 
        }
        private static DataTable ToDataTable(QuoteItemBase[] m, int type)
        {
            DataTable dsTemp = null;

            if (type == 1)
            {
                dsTemp = new DataTable("OneClickQuote");
            }
            else if (type == 2)
            {
                dsTemp = new DataTable("MarketingQuote");
            }
            else
                dsTemp  = new DataTable("temptable");

            dsTemp.Columns.Add("Date");
            dsTemp.Columns.Add("Time");
            dsTemp.Columns.Add("NO");
            dsTemp.Columns.Add("Name");


            if (m == null)
                return dsTemp;

            foreach (var item in m)
            {
                DataRow drTemp = dsTemp.NewRow();
                drTemp["Date"] = item.date;
                drTemp["Time"]  = item.time;
                drTemp["NO"] = item.no;
                drTemp["Name"] = item.name;
                dsTemp.Rows.Add(drTemp);

            }

            return dsTemp;
      }
}






PS: if new data comes and i'm not dragging scroll bar, it works fine.

any ideas? thank you !


I found that when you invoke a control and set bindings (or clear them) and an object is set to null this can throw a null reference exception, this is reflected through invoke giving an error, this error however is somewhere else in your code:

quick example:

public class test : Form
{
  public test()
  {
    Thread t = new Thread(start);
    t.Start();
  }
  public void start()
  {
    LoadCompleteEvent();
  }
  public void LoadComplete() //fired by LoadCompleteEvent();
  {
    if(this.InvokeIsRequired)
    {
      //do invoke
      //and return
    }

    comboBoxEditBrand.Properties.Items.Clear();
    comboBoxEditBrand.Properties.Items.AddRange(ListOfStuff.ToArray());
  }
  public void comboBoxEditBrand_SelectedItemChanged(object sender, eventargs e) // fired as control is changed
  {
    //error here!!
    if(comboBoxEditBrand.SelectedItem == SomeBrandItem) //<- this is where the error is thrown!! check for null first!
    {
      //do something
    }
  }
}

it's something like this.. this code will probably not throw the error because A) it's from the top of my head and B) it's made up. BUT this is kind of what bugged me for half a morning as to WHY this error was thrown.

just place

if(comboBoxEditBrand.SelectedItem == null)
  return;

where it says //error here!! and it should work again.


Make sure you switch to the Gui thread before you invoke

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜