2

I'm using this code:

DataTable table = new DataTable();
table.Columns.Add("Title");
dataGridView1.DataSource = table;

to populate my dataGridView but I get a null reference exception when I set the datasource.

6
  • 1
    Where are you initializing dataGridView1? Commented Jan 30, 2016 at 19:32
  • 1
    @ComanderKai77 Where does dataGridView1 comes from ? Commented Jan 30, 2016 at 19:37
  • In your code only if dataGridView1 is null, such Exception will be thrown. Check that or give us more code.Notice that Exception always contains number of the line which thrown exception. Commented Jan 30, 2016 at 19:48
  • 2
    Possible duplicate of What is a NullReferenceException and how do I fix it? Commented Jan 30, 2016 at 19:57
  • The DGV is created by the Forms Commented Jan 30, 2016 at 20:57

1 Answer 1

4

Your dataGridView1 is most likely a NULL reference. To verify, set a breakpoint on the line and when you hit it, hover your mouse over dataGridView1.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.