I want to format C# code in visual studio as image shows:
but once auto formatted run on save I get below code
I want auto save keep working but keep such alignment as is
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" ,
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:
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).
Personally, I would not recommend using such formatting:
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.