Click here to Skip to main content
14,924,621 members
Please Sign up or sign in to vote.
1.40/5 (2 votes)
See more:
Hi,

I am reading the data from excel sheet and using OLEDB command to fill the adapter with excel data.

I am using the following code to check whether 3rd column message names are starting with GW or not.

It is working fine. But here I am using Rows in the code for checking columns in excel


foreach (DataRow row in lstDS[0].Tables[0].Rows)<br />
           {<br />
<br />
               string fieldFromDB = row[2].ToString();<br />
               if (fieldFromDB.StartsWith("GW"))


I am confused, Is the dataset stores the values of excel in reverse order(like interchanging of columns and rows)

Now I need to do something like, if the message name in 3rd column starts with GW, then I need to copy the corresponding message ID in 2nd column of the same row

Can anyone please explain me how this can be done.


Thanks
John
Posted
Comments
   
You don't show anything filling in any data...
—SA

1 solution

I added some comments to your code and added few lines. Perhaps it will help. If I correctly understand your intention you want to examine every row of the table and copy contents of the third column to the second column if it begins with "GW" string, correct?


foreach (DataRow row in lstDS[0].Tables[0].Rows)
{

string fieldFromDB = row[2].ToString();

//The following line is checking if content of the third column starts with "GW"
if (fieldFromDB.StartsWith("GW"))
{
   //If above condition is 'true', assign value of third column to the second column 
   row[1]=row[2];
}
}

And to answer another part of your question. DataSet doesn’t switch columns with rows.

If your Excel sheet looks like blow you will end up with DataTable containing 3 columns and 2 rows.

Column#1     Column#2  Column #3
abc              def             gh
axx              cc              GWa
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900