Click here to Skip to main content
15,849,182 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

In the following code I am writing all the 2nd row of dataset into excel file.

C#
int i = 1;
foreach (DataRow row in lstDS[0].Tables[0].Rows)
{
   xlWorkSheet1.Cells[i, 1] = row[2];
   i++;
}


But I need to filter them in a way that only strings starting with GW should be written into excel sheet????


Thanks
John
Posted
Updated 29-Nov-13 7:48am
v2

C#
foreach (DataRow row in lstDS[0].Tables[0].Rows)
{
    string fieldFromDB=row[2].toString();
    if (fieldFromDB.StartsWith("GW"))
    {
        xlWorkSheet1.Cells[i, 1] = row[2];
        i++;
   }
}
 
Share this answer
 
Comments
Member 10408451 29-Nov-13 10:42am    
Hi,

Thank you for your reply. This is working fine.

I need to extract the column of the data table if the string of the row matches with GW

How this can be done???
Debopam Pal 29-Nov-13 11:23am    
See my answer below for this question...
Adam Zgagacz 29-Nov-13 11:17am    
I'm not sure if I fully understand your question. Do you mean column name or caption?
You can access column object and properties within your loop with code like:
row.Table.Columns[2].ColumnName;
row.Table.Columns[2].Caption;
Well, I would start with this Google search:
Google Search for String.StartsWith[^]

This will tell you how to use the "StartsWith" command on a string.

You can then use this with the contents of row[2] and see if they meet your criteria and in turn add it to the cell. then loop.

Hope that helps :-)
 
Share this answer
 
Hi there,
You can simply use text spliter method;
C#
string[] st = System.Text.RegularExpressions.Regex.Split(row, "GW");


Good Luck,
z3ngew
 
Share this answer
 
v2

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