1

So i'm importing an excel doc into my project

DataSet result = excelReader.AsDataSet();

 if (result != null)
            {
                foreach (System.Data.DataTable t in result.Tables)
                {
                    if (t != null && t is System.Data.DataTable)
                    {
                        System.Data.DataTable table = t as System.Data.DataTable;

                        Items Lastitem = new Items();

                        foreach (System.Data.DataRow r in t.Rows)
                        {
                            if (r != null && r is System.Data.DataRow)
                            {
                                //new ItemType
                                if (r.ItemArray[0] != null)
                                {

Then I run checks to check which column is empty when I get the empty column i then want to get the empty row. I have tried:

if (checkIfColumnisEmpty(r.ItemArray[0]) && !checkIfColumnisEmpty(r.ItemArray[1]))
                                        {
                                            throw new ImportBOQException("Error importing document: First column is empty at row " + r);

r being the datarow, the telesense allows me rowError, rowState etc... but not row Number....any ideas please? }

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
John
  • 3,965
  • 21
  • 77
  • 163

1 Answers1

1

You could use a For Loop instead of Foreach when iterating through your rows as follows :

for (int i = 0; i < t.Rows.Count; i++)    
{

    // Do Your Null Checking and throw the exception with iteration number + 1

    if (checkIfColumnisEmpty(t.Rows[i].ItemArray[0]) && !checkIfColumnisEmpty(t.Rows[i].ItemArray[1]))
       {
          throw new ImportBOQException("Error importing document: First column is empty at row " + (i + 1));
       }
}
Abdusalam Ben Haj
  • 5,343
  • 5
  • 31
  • 45
  • That's really understandable, but it would much much more comfortable if there was a RowNumber property. – Th3B0Y Aug 29 '17 at 13:36
  • @Th3B0Y actually there is some kind of property, check the accepted answer of this question : https://stackoverflow.com/questions/4502920/how-to-get-the-row-number-from-a-datatable – Abdusalam Ben Haj Sep 11 '17 at 21:15