0
votes

I have to ensure that each cell in an excel column contains values of the same data type.

In this sheet the first row contains the column header, therefore the data starts in row 2.

The First column should contain only integer values and the second column should contain strings. How to make sure that all values in the column have the same type or how to use tryparse in this validation?

This is the code which render the data in excel

for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++)
{                
   var P = new VPODSettlement();
   P .POs = Convert.ToInt32(settlement.Cells[rowIterator,1].Value);
   P .Am_title = settlement.Cells[rowIterator, 2].Value.toString();
   Ps .Add(P);                    
}
1
You should be able to just the value type of the field. You will want to avoid calling .ToString as you will loose the type. And you can use the worksheet.Cells property which only contains cells that have actual content in the sheet. See if this helps: stackoverflow.com/questions/31537981/…. And this one: stackoverflow.com/questions/37495707/…Ernie S
thank you for your answer. i used int.tryparse() for this validationHarish S A

1 Answers

2
votes

There is worksheet.DataValidations class which offers various data validation methods. Applying Excel Datavalidation over a column will not only ensure correct data to be written, will also prevent the excel file from being wrongfully edited by the user or another program.