Archive

Archive for the ‘C# Programming Tips’ Category

Check The String is a valid Excel CellName or not using c#

April 3, 2011 Leave a comment

If you want to check the validity of name of cells you enterd
This function checks given string is a range addres or not
A1:A13- valid
A1:B12 valid
A1:A-not valid
A1:aa not valid
1A:b1 –not valid.

public bool checkcellRangeValidity(string inputrange)
{ bool valid = true;
string[] parts = inputrange.Split(':');
if (parts.Length == 2)
{
if (!((validcell(parts[0])) && (validcell(parts[1]))))
valid = false;
}
else //only one part or more parts on both sides of ':'
{
valid = false;
}
return valid;
}

Give string is a valid cell or not
A1: valid
A-not valid
1 not valid
1A not valid

public bool validcell(string inputcell)
{
bool valid = true;

int count = 0;
int digitscount = 0;
int lettercount = 0;

foreach (char c in inputcell)
{
if (char.IsDigit(c))
digitscount++;

if (char.IsLetter(c))
lettercount++;

if (!char.IsLetterOrDigit(c))
{
valid = false;
break;
}

if (count == 0) //means the very first char should be letter
{
count++;
if (!char.IsLetter(c))
{
valid = false;
break;
}

}
}

if (valid)
{ //chk only if the conditions are true till
if (!((digitscount >= 1) && (lettercount >= 1)))
{
// the cell is lagging either a no or a letter

valid = false;
}
}

return valid;
}

Categories: C# Programming Tips Tags:

Get Order & Name Of a DataTable Column

March 16, 2011 1 comment

DataTable myDataTable=new DataTable();
//To get the name of a column .If u dont know the column name use column no
string currentColumnName = myDataTable.Columns["Col1"].ColumnName;
//To get the Rank Or Column No
// then pick the column no of this column and save it as the value
int rank = myDataTable.Columns[currentColumnName].Ordinal;

Categories: C# Programming Tips

Find Percentile Of N Numbers using c#

March 16, 2011 Leave a comment

In Excel we had a formula to find the percentile of n numbers for a given percentile value…I  want to implement the same in some place of my code  and when i am looking for an algorithm  i cant able to find any satisfactory solution and i decided to write an algorithm of my own.

The basic definition of Percentile and examples to find percentile can be found here
Link For Percentile

Note:Accuracy of this algorithm (Deviation of 0.0001 from the result of excel sheet percentile function)
Download Code From here and copy paste to your editor
find percentile using c#

Categories: C# Programming Tips

Copy One DataRow From A DataTable to Another DataTable

January 4, 2011 Leave a comment

every C# programmer deals with populating, selecting, and even deleting from a DataTable. A moderately advanced operation is transferring an entire single row from an original DataTable and inserting it into a new DataTable

DataTable originalTable=new DataTable();

/*Add datas to this data table*/

DataTable newTable = originalTable.Clone(); //make new table with same structre of OriginalTable

DataRow newRow = newTable.NewRow();

newRow.ItemArray = originalTable.Rows[1].ItemArray; //Copy Row at index 1..If we need to copy morethan 1 row loop this section

newTable.Rows.Add(newRow); //add this row to the new data table

Categories: C# Programming Tips