convert a repeating code to method
- by Mr_Green
In my project, I am adding ComboBox, Text, Link label to my DataGridView dgvMain.I have created different methods for different cell templates as shown below: (The code below is working)
gridLnklbl(string headerName)
DataGridViewLinkColumn col = new DataGridViewLinkColumn();
col.HeaderText = headerName; //
col.Name = "col" + headerName; // same code repeating to all the methods
dgvMain.Columns.Add(col); //
gridCmb(string headerName)
DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
col.HeaderText = headerName;
col.Name = "col" + headerName;
dgvMain.Columns.Add(col);
gridText(string headerName)
DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
col.HeaderText = headerName;
col.Name = "col" + headerName;
dgvMain.Columns.Add(col);
As you can see, except the declaration of objects, the code for every method is repeating. Just curious to know, can the repeating code be converted to single method? I dont know how to do that.. Its not about 3 codes of line, I have written many more lines which can be make common to those methods.