locked
Object Reference Not Set To An Instance Of An Object When Setting DataGridView.DataSource = MyList RRS feed

  • Question

  • I have a constructor that attempts to set a data source of a grid to a list but it throws an exception.

    private CustomerSelect(List<Customer> customers)
            {
                customerList = customers;
                dgvCustomerList.DataSource = customers;
            }
    
    When I check customers, it does contain 2 items that I added for testing and all the values for each customer object is correct. What am I missing?

    Wednesday, January 4, 2012 6:25 AM

Answers

  • Works now, forgot to add InitializeComponent() into the constructor... It's way too late for me... Thanks for everyone's help offering though.
    • Marked as answer by Phenomin Wednesday, January 4, 2012 8:06 AM
    Wednesday, January 4, 2012 8:06 AM

All replies

  • Hai Phenomin..

    Firts you check this one.. May be the error comes for customerList = customers..

    private CustomerSelect(List<Customer> customers)
            {
                //customerList = customers;
                dgvCustomerList.DataSource = customers;
            }
    
    


    Check thios link for all ready solved the same errors.

    http://social.msdn.microsoft.com/Forums/eu/csharplanguage/thread/766c7b46-6431-4ec1-8e74-50f718c6606b


    If you find an answer helpful, click Vote as helpful button. If you find an answer to your question, Click mark it as the answer.
    Wednesday, January 4, 2012 6:37 AM
  • What is the type of customerList?

    Lets consider customerList_DataType is the datatype for customerList,  Where is the below statement in your code?

    customerList_DataType customerList = new customerList_DataType();

     

     


    • Edited by D.Sreedhar Wednesday, January 4, 2012 6:39 AM
    Wednesday, January 4, 2012 6:38 AM
  • customerList is the same, List<Customer>. It's a variable set at class level so I can determine the Customer object to return when a row is double-clicked in the DataGridView back to the calling form. The code shown above is from a modal window and since ShowDialog() cannot return an object, I have this public static method which is called from another form.

    public partial class CustomerSelect : Form
        {
            private List<Customer> customerList;
            private static int index;
    
            public CustomerSelect()
            {
                InitializeComponent();
            }
    
            private void CustomerSelect_Load(object sender, EventArgs e)
            {
                dgvCustomerList.AutoGenerateColumns = false;
            }
    
            private CustomerSelect(List<Customer> customers)
            {
                //customerList = customers;
                dgvCustomerList.DataSource = customers;
            }
    
            private void SelectCustomer(object sender, DataGridViewCellMouseEventArgs e)
            {
                index = e.RowIndex;
                this.DialogResult = DialogResult.OK;
                Close();
            }
    
            public static Customer GetCustomer(List<Customer> customers)
            {            
    
                var customerSelect = new CustomerSelect(customers);
    
                if (customerSelect.ShowDialog() == DialogResult.OK)
                {
                    var customer = customers[index];
                    return customer;
                }
                else
                    return null;            
            }
        }



    • Edited by Phenomin Wednesday, January 4, 2012 6:49 AM
    Wednesday, January 4, 2012 6:47 AM
  • Hi Phenomin

             Check as follows ,

    Replace this

    dgvCustomerList.DataSource = customers;

    with

    if (dgvCustomerList != null) {
     dgvCustomerList.DataSource = customers;

    } else {
     Interaction.MsgBox("dgvCustomerList is Nothing");

    }

     

     


    If a post answers your question, please click Mark As Answer on that post and Mark as Helpful. Happy Coding...
    • Proposed as answer by Tiya01 Wednesday, January 4, 2012 7:10 AM
    Wednesday, January 4, 2012 6:47 AM
  • Well, apparently it's nothing. I moved that line out completely and put it into the form load method as dgvCustomerList.DataSource = customerList which should do the same thing and a blank form with no title and no controls came up. As I closed it, then the exception is thrown.

    I have also changed my form code to this...

     

    public partial class CustomerSelect : Form
        {
            private List<Customer> customerList;
            private Customer customer;
    
            public Customer Customer
            {
                get
                {
                    return customer;
                }
                set
                {
                    customer = value;
                }
            }
    
            public CustomerSelect()
            {
                InitializeComponent();
            }
    
            private void CustomerSelect_Load(object sender, EventArgs e)
            {
                dgvCustomerList.AutoGenerateColumns = false;
                dgvCustomerList.DataSource = customerList;
            }
    
            public CustomerSelect(List<Customer> customers)
            {
                customerList = customers;
            }
    
            private void SelectCustomer(object sender, DataGridViewCellMouseEventArgs e)
            {
                customer = customerList[e.RowIndex];
                DialogResult = DialogResult.OK;
                Close();
            }
    
            //public static Customer GetCustomer(List<Customer> customers)
            //{            
    
            //    var customerSelect = new CustomerSelect(customers);
    
            //    if (customerSelect.ShowDialog() == DialogResult.OK)
            //    {
            //        var customer = customers[index];
            //        return customer;
            //    }
            //    else
            //        return null;            
            //}
        }
    
    <br/>
    

    <br/>
    
    // Calling form code below...
    
    private void btnSearch_Click(object sender, EventArgs e)
    
            {
                try
                {
                    var customer = Customer.Create();
                    var customerList = Customer.Create(txtSearch.Text);
                    if (customerList.Count > 1)
                    {
                        var customerSelect = new CustomerSelect(customerList);
                        if (customerSelect.ShowDialog() == DialogResult.OK)
                            customer = customerSelect.Customer;
                    }
                    else
                        customer = customerList[0];
                    LoadFields(customer);
                    btnAdd.Enabled = false;
                    btnDelete.Enabled = true;
                    btnEdit.Enabled = true;
                    btnEdit.Text = "&Edit";
                    LockControls(true);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
    


    Which seems like the better way to do it... But when I call it from the other form it does the exact same thing, comes up with a blank form?

     

     

    Wednesday, January 4, 2012 7:44 AM
  • Works now, forgot to add InitializeComponent() into the constructor... It's way too late for me... Thanks for everyone's help offering though.
    • Marked as answer by Phenomin Wednesday, January 4, 2012 8:06 AM
    Wednesday, January 4, 2012 8:06 AM
  • Ok, as I see it,

    var customerSelect = new CustomerSelect(customerList);

    You are creating CustomerSelect type object and you put it's type as var. Since you know the type, you can just put,

    CustomerSelect customerSelect = new CustomerSelect(customerList);

    Try this and see.

    Happy Coding.

    Regards,

    Jaliya

     

     

     


    Jaliya Udagedara(MCPD) http://www.jaliyaudagedara.blogspot.com/
    Wednesday, January 4, 2012 8:36 AM