Delete Empty Rows or Null Rows in Datatable in C#

How to Delete Empty Rows or Null Rows in Datatable

Example:  In this application we can see how to Delete Empty Rows or Null Rows in Datatable.

Mostly this issue comes from while uploading the excel(.xls,.xlsx) file data into your sql table through datatable in windows application or web application. In rare cases,we can see while getting data from excel to datatable it will copies the empty rows also.

Below Code.
C#
public DataTable DeleteEmptyRowsFromDataTable(DataTable dt)
 {
   for (int i = dt.Rows.Count - 1; i >= 0; i--)
   {
     if (dt.Rows[i][1] == DBNull.Value)
     dt.Rows[i].Delete();
   }
     dt.AcceptChanges();
     return dt;           
 }

Comments

Popular posts from this blog

How To Setup Angular 10 Environment On Windows

Create Custom Form Control for ng-select in Angular

Angular CLI Commands - Cheat Sheet

Difference of High-Level Design (HLD) and Low-Level Design (LLD)

Configure the API gateway in Microservices Architecture with example in .NET Core

Domain Driven Design (DDD) in .NET Core

Recommended Visual Studio Code Extensions for Angular Development

Send API POST Request in MS SQL Server

Tightly Coupled and Loosely Coupled in .NET Core with example

Export to Excel in Angular 6 | 7 | 8 | 9 | 10