Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Developers,


I want to remove some rows from my datatable if some of the columns are blank.
i tried below code,

C#
int rowcount = datatabletest.Rows.Count;

             for (int i = 0; i < rowcount; i++)
             {
                 if (datatabletest.Rows[i]["Mcolumn1"].ToString().Trim() == "" || dtExport.Rows[i]["column2"].ToString().Trim() == "")
                 {
                    datatabletest.Rows.Remove(datatabletest.Rows[i]);

                 }
             }


but the problem here is if the first row meets the condition the that row will be deleted from the table.this will reduce one row count table.but the code will check for the last row since we have written it inside the loop.But code can not find in that position since one row is already deleted.Its throwing No row on that place exception.

For example:
my table contains 3 rows.
First row has null values so it will be deleted by code.But the code will check for the row at position 3 since for loop will loop 3 times as row count is 3.
but the first row is deleted so now table has only 2 rows.So when code check for row 3 position it couldn't find and throwing error.


I tried with for each loop also but doesn't work.Got below error
System.InvalidOperationException: Collection was modified; enumeration operation might not execute.

so please share your logic. please let me know if question is not clear.
Posted
Updated 17-Nov-14 17:39pm
v2

An easy solution may be to just adjust your row count and index on any removal.

Try this:

C#
int rowcount = datatabletest.Rows.Count;

             for (int i = 0; i < rowcount; i++)
             {
                 if (datatabletest.Rows[i]["Mcolumn1"].ToString().Trim() == "" || dtExport.Rows[i]["column2"].ToString().Trim() == "")
                 {
                    datatabletest.Rows.Remove(datatabletest.Rows[i]);
                    rowcount--;
                    i--;
                 }
             }
 
Share this answer
 
Comments
Am Gayathri 18-Nov-14 2:37am    
Great ! It works !
Thanks...:)
Hi,
Review below code, where you can get result using LINQ query on a DataTable[^].
C#
DataTable datatabletest = new DataTable();
DataTable dtNewTableTest = new DataTable();

var vDataTable = from vRow in datatabletest.AsEnumerable()
             where vRow.Field<string>("Column1") != "" || vRow.Field<string>("Column2") != ""
             select vRow;

dtNewTableTest = vDataTable.CopyToDataTable();

AsEnumerable() returns IEnumerable<DataRow>. If you need to convert IEnumerable<DataRow> to a DataTable, use the CopyToDataTable() extension.
 
Share this answer
 
v2
Comments
Am Gayathri 18-Nov-14 1:30am    
IF i use linq here, if there is no items which meets condition ("Column1") != "" || vRow.Field<string>("Column2") != "" then i will get an error in below code
dtNewTableTest = vDataTable.CopyToDataTable();
since the table is empty.
Am Gayathri 18-Nov-14 2:46am    
Thanks
Hi,
You can remove the columns from the selection query itself. That will be better I think. Please try it.
 
Share this answer
 
Comments
Am Gayathri 18-Nov-14 2:46am    
Thanks
Jineesh TR 18-Nov-14 6:36am    
You are welcome friend..

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