how to Customize autoformatting of C# Code in visual Studio 2022

43 Views Asked by At

I want to format C# code in visual studio as image shows:enter image description here

but once auto formatted run on save I get below code enter image description here

I want auto save keep working but keep such alignment as is

  • tried to format code as below
       private void _Insert_Poeple_Info_To_GridView()
       {
           dgvAllPeople.DataSource
                 = _PeopleData.DefaultView.
                                     ToTable(false , "person_ID"    ,
                                                     "National_ID"  , "First_Name" ,
                                                     "Second_Name"  , "Third_Name" , "Last_Name"   ,
                                                     "Email"        , "Phone"      , "Address"     ,
                                                     "Home_Country" , "Gender"     , "DateOfBirth" ,
                                                     "Image_Path"
                                                              );
           lbRecordeCounts.Text = _PeopleData.Rows.Count.ToString();
       }
  • expect and want autoformat to let this aliment as is when format the code

  • get code this way

dgvAllPeople.DataSource
      = _PeopleData.DefaultView.
                          ToTable(false , "person_ID" ,
                                          "National_ID" , "First_Name" ,
                                          "Second_Name" , "Third_Name" , "Last_Name" ,
                                          "Email" , "Phone" , "Address" ,
1

There are 1 best solutions below

1
Alexander Burov On BEST ANSWER

It seems you have enabled option "Run Code Cleanup profile on Save". You can find it by navigating to the menu Tools > Options, expanding Text Editor and selecting Code Cleanup. On the right side you can toggle corresponding check box: enter image description here

That should disable autoformat on save, however, there are few other triggers for auto formatter to run, you can check them out at Tools > Options > Text Editor > C# > Code Style > Formatting. You'll have to either disable those triggers or manually cancel autoformatting: you always can cancel autoformatting by pressing Ctrl+Z (e.g. it will format the whole block when you close a curly bracket).

P. S.

Personally, I would not recommend using such formatting:

  1. It forces you to either disable or constantly revert autoformatting (and it's quite easy to miss some);
  2. Once someone else will work on the code, there's no guarantee they will keep formatting;
  3. While it is hard to maintain, it doesn't provide you a lot of value (I would maybe agree if you would format some matrix this way, but here's a plain list of column names).

I would rather define an array separately (depending on frequency of use it could be just in method, readonly field or even static readonly):

private void _Insert_Poeple_Info_To_GridView()
{
    string[] columnNames =
    [
        "person_ID",
        "National_ID",
        "First_Name",
        "Second_Name",
        "Third_Name",
        "Last_Name",
        "Email",
        "Phone",
        "Address",
        "Home_Country",
        "Gender",
        "DateOfBirth",
        "Image_Path"
    ];
    
    dgvAllPeople.DataSource = _PeopleData.DefaultView.ToTable(false, columnNames);
    lbRecordeCounts.Text = _PeopleData.Rows.Count.ToString();
}

That kind of formatting is extremely easy to maintain and autoformatting will help to do so, not work against you.