How to convert the year entered by user into formula(y+1)-(y-1) etc

There are two formats the user can enter into the gridview
first format: 2012-2010,2012-2007 etc.,
I want to convert it as (y+1)-(y-1).(y+1)-(y-4) y will be the current year.
second format 2009,2008,2007,2006etc., I want to convert it as (y-2),(y-3),(y-4)and (y-5) etc

Please let me know.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kaufmed 👽Commented:
Please redefine your expected format. Please clarify which are hyphens (if any) and which are actual subtraction operations.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KavyaVSAuthor Commented:
I want to convert 2012-2010 as (y+1)-(y-1).after(y+1) - is hyphen.
(y-1) here it is y minus 1.

kaufmed 👽Commented:
Try this.

Format 1
string strFormat1 = "2012-2010,2012-2007";
string[] strArrFormat1 = strFormat1.Split(',');

for (int i = 0; i < strArrFormat1.Length; i++)
    string[] parts = strArrFormat1[i].Split('-');

    int y1 = Convert.ToInt32(parts[0]);
    int y2 = Convert.ToInt32(parts[1]);

    strArrFormat1[i] = string.Concat((++y1).ToString(), "-", (++y2).ToString());

string result1 = string.Join(",", strArrFormat1);


Open in new window

Format 2
string strFormat2 = "2009,2008,2007,2006";
string[] strArrFormat2 = strFormat2.Split(',');

for (int i = 0; i < strArrFormat2.Length; i++)
    int y = Convert.ToInt32(strArrFormat2[i]);

    strArrFormat2[i] = (++y).ToString();

string result2 = string.Join(",", strArrFormat2);


Open in new window

I think, to be more flexible, you may want to try "DateTime.Now.Year".

// previous code ...

Int32 nYear = DateTime.Now.Year;

// dowhachalike! :-)

Int32[] yearRange = {nYear+1, nYear-1, nYear+1, nYear-2, ...};
Int32[] yearList = {nYear-2, nYear-3, ...};

// write your pairs
Console.Write(String.Format("{0}-{1}, {2}-{3}", yearRange[0], yearRange[1], yearRange[2], yearRange[3]));

// write your list
foreach(Int32 nYear in yearList)

Open in new window

Obviously, this is over simplified, and the organziation leaves much to be desired.  The only point here is to show you how to get your current year as a variable using the DateTime.Now.Year property.
Looks like I missed the part where the year is entered by the user, but the concept is the same.

In the first form, if the user is always expected to enter the year in the form (y+1) - (y-1) (such as 2012-2010), then take the data from the first grid and use "String.Split().  The line will look something like this, assuming your DataGridView object is called dgvIt:

String[] listTokens = dgvIt[nColumn][nRow].toString().Split("-");
Int32 nYear = Int32.Parse(listTokens[0]) - 1;

Open in new window

The above is an oversimplification, but you now have the "y" you are looking for and can validate other entries as you desire.

In the second list, if you always know that the first value is (y-2), you can do something very similar

Int32 nYear = Int32.Parse(dgvIt[nColumn][nRow].toString());

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.