0

Recently added Toolstrip to my C# Application , and there is error when click on menu item that call the function but when call the function from Windowsform Button it works perfect. following is my function:

 private void ShowAddDocument(object sender, EventArgs e)
    {

            foreach (var rr in this.gridEX1.GetDataRows())
            {

                if (rr.Cells["Select"].Value != null)
                {
                    if (rr.Cells["DocumentNumber"].Value.ToString() != "")
                    {

                        rr.BeginEdit();
                        rr.Cells["Select"].Value = null;
                        rr.EndEdit();
                    }
                    else
                    {
                        this.gridEX1.Tag = "SELECTED";


                    }
                }

            }

and I use following line to connect function and Toolstrip MenuItem Click event :

this.userControl11.IssueDocMenuItem.Click += new System.EventHandler(ShowAddDocument);

but when click on Toolstrip Menu Item it raise an error : "Object Reference not set to instance of an object". but strange part is when I use Windowsform Button and call the function by following code:

    private void button1_Click(object sender, EventArgs e)
    {
        ShowAddDocument(sender, e);
    }

It my code works without any error!!!.

franchesco totti
  • 582
  • 1
  • 7
  • 28
  • And which line is it given for? I can only make assumptions based on your code. It's either `gridEX` that's null, `GetDataRows` that returns null making `rr` null or one of the cells retrieve with indexes on `rr` that's null ex. `rr["Select"]` and `rr["DocumentNumber"]` Also see: http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – Bauss Jan 01 '17 at 06:38
  • no my grid is not null and when click on Button1 it works without any error – franchesco totti Jan 01 '17 at 06:41
  • Yeah, because a runtime that has been developed for many years suddenly throws exceptions for null values randomly in such simple code? Not to sound rude or anything, but you should *ALWAYS* assume that your own code is incorrect. Did you try to inspect *ALL* values with a debugger and not just your grid, also what values are returned in your method / row indexes. – Bauss Jan 01 '17 at 06:44
  • I already found behavior named ActAsSelector in my gridex Select Column and set is to True then my problem resolved.happy now. – franchesco totti Jan 01 '17 at 06:50

1 Answers1

0

I already found behavior named ActAsSelector in my gridex Select Column and set is to True then my problem resolved.happy now.

franchesco totti
  • 582
  • 1
  • 7
  • 28