How to enable DataGridView sorting when user clicks on the column header?

前端 未结 15 1002
误落风尘
误落风尘 2020-12-02 15:28

I have a datagridview on my form and I populate it with this:

dataGridView1.DataSource = students.Select(s => new { ID = s.StudentId, RUDE = s.RUDE, Nombr         


        
15条回答
  •  无人及你
    2020-12-02 15:48

    If you get an error message like

    An unhandled exception of type 'System.NullReferenceException' occurred in System.Windows.Forms.dll

    if you work with SortableBindingList, your code probably uses some loops over DataGridView rows and also try to access the empty last row! (BindingSource = null)

    If you don't need to allow the user to add new rows directly in the DataGridView this line of code easily solve the issue:

    InitializeComponent();
    m_dataGridView.AllowUserToAddRows = false; // after components initialized
    ...
    

提交回复
热议问题