I want to sort a DataGridView by a column when the form loading, but I got an exception.
private void frm_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'dataSetExclusion.Exclude' table. You can move, or remove it, as needed.
this.excludeTableAdapter.Fill(this.dataSetExclusion.Exclude);
this.dgv.Sort(this.dgv.Columns["ID"], ListSortDirection.Ascending);
}
The column’s headtext in the DataGridView dgv is “ID”. Its DataPropertyName is “ExcludeID”. I tried both “ID” and “ExcludeID” for the column name, but still got the exception.
Value cannot be null.Parameter name: dataGridViewColumn
Try to check the name property of that column like shown below