convert a repeating code to method
Posted
by
Mr_Green
on Stack Overflow
See other posts from Stack Overflow
or by Mr_Green
Published on 2012-11-20T04:57:15Z
Indexed on
2012/11/20
4:59 UTC
Read the original article
Hit count: 122
c#
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.
© Stack Overflow or respective owner