Reputation: 20806
I have a custom control inherited from DataGridView
that I'd like to be able to just copy the .cs file for, and drop it in to any project. There's one bit of code that I can't manage to move out of Main.cs
, and into DataGridView.cs
though:
private void Main_Load(object sender, EventArgs e)
{
dgv.Sort(dgv.Columns[0], ListSortDirection.Ascending);
dgv.Columns[0].HeaderCell.SortGlyphDirection = SortOrder.Ascending;
}
This is what I've tried:
class MyDataGridView : DataGridView
{
protected override void InitLayout()
{
base.InitLayout();
Sort(Columns[0], ListSortDirection.Ascending);
Columns[0].HeaderCell.SortGlyphDirection = SortOrder.Ascending;
}
// Lots of methods snipped
}
The program immediately throws an InvalidOperationException
on the Sort()
:
DataGridView control must be bound to an IBindingList object to be sorted.
How can I move this code into DataGridView.cs
?
Upvotes: 3
Views: 10971
Reputation: 20806
This is how I did it:
class MyDataGridView : DataGridView
{
public MyDataGridView()
{
base.DataBindingComplete += Sort;
}
public void Sort(object sender, EventArgs e)
{
Sort(Columns[0], ListSortDirection.Ascending);
Columns[0].HeaderCell.SortGlyphDirection = SortOrder.Ascending;
}
}
Upvotes: 1