I can’t seem to make the following work. Add a DataGridView control with 2 columns, contactID and firstName.
Refer to the firstName column: var col = this.datagridView1.Columns[“firstName”]; gives me a null reference.
Refer to the column by the index: var col = this.datagridView1.Columns; works just fine.
Hopefully it’s something obvious that i’m missing here. Any help appreciated!
You have to set the column names explicitly. I suspect that you have AutoGenerateColumns = true and you just set the datasource. This procedure does not give column names. And this is why you get the null exception. But of course this may be a wrong assumption.
I tried to reproduce but failed. (see simple sample attached).
Are you sure that the name is set to the columns ? Sometimes the designer plays tricks on you.
Any chance to provide us with a test case ?
Thanks in advance.
Please login first to submit.