There is such a sorting procedure:

private void сортироватьЧастныеТаблицыToolStripMenuItem_Click(object sender, EventArgs e) { dataGridView2.Sort(dataGridView2.Columns["dataGridViewTextBoxColumn8"], ListSortDirection.Descending); } 

How can it be converted to a subroutine and how can it be correctly called later?

    1 answer 1

    It all depends on how you want to use it further. If you use it further to sort different dataGridView by different dataGridViewTextBoxColumn, then you can put it into a separate method and call it by passing the necessary parameters to it.

     void SortDataViewByColumn(GridView dataGridView, string nameColumn) { dataGridView.Sort(dataGridView.Columns[nameColumn], ListSortDirection.Descending); } 

    Then for your example:

     private void сортироватьЧастныеТаблицыToolStripMenuItem_Click(object sender, EventArgs e) { SortDataViewByColumn(dataGridView2, "dataGridViewTextBoxColumn8"); } 
    • Swears that he can not find the type or field names GridView . I tried to connect using System.Windows.Controls; , but the comment directive does not work with comment is not needed - Den4ik
    • I wrote figuratively GridView. You need to replace with your type of object, which has the name dataGridView2. - strevg 1:09 pm
    • Just do not know what object you are using. - strevg
    • Everything, I understood, thank you very much - Den4ik