locked
Delete row if the first 4 datarows are empty RRS feed

  • Question

  • User-73514677 posted

    Hi.

    I have a dataTable , in which I want to dynamically delete the row from the dataTable if the datarow[0] to datarow[3] are empty.

    tried code:

    foreach (DataRow row in myDataSet.Tables[0].Rows)
     {
     if ((string.IsNullOrEmpty(row[0].ToString())) && (string.IsNullOrEmpty(row[1].ToString())) && (string.IsNullOrEmpty(row[2].ToString())) && (string.IsNullOrEmpty(row[3].ToString())))
      {
     // delete the Row
    
        }
     }
    
    myDataSet.Tables[0].AcceptChanges();

    how to delete the row ?

    How to achieve this in C# ?

    Thanks

    Monday, March 30, 2020 2:24 PM

Answers

  • User753101303 posted

    Hi,

    If looking at the documentation for the DataRow class you'll find https://docs.microsoft.com/en-us/dotnet/api/system.data.datarow.delete?view=netframework-4.8

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 30, 2020 5:16 PM
  • User288213138 posted

    Hi venkatzeus,

    venkatzeus

    I have a dataTable , in which I want to dynamically delete the row from the dataTable if the datarow[0] to datarow[3] are empty.

    You can use a for loop to iterate over the datarows, Check if they are empty, and if so, delete them.

    Here a demo for you as a reference.

    DataTable dt = new DataTable()
    for (int i = 0; i < dt.Rows.Count; i++)
             {
                 DataRow dr = dt.Rows[i];
                 if (string.IsNullOrEmpty(dt.Rows[i][0].ToString()) == true && string.IsNullOrEmpty(dt.Rows[i][1].ToString()) && string.IsNullOrEmpty(dt.Rows[i][2].ToString()))
                       {
                           dr.Delete();
                       }
             }

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 31, 2020 3:18 AM

All replies

  • User753101303 posted

    Hi,

    If looking at the documentation for the DataRow class you'll find https://docs.microsoft.com/en-us/dotnet/api/system.data.datarow.delete?view=netframework-4.8

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 30, 2020 5:16 PM
  • User288213138 posted

    Hi venkatzeus,

    venkatzeus

    I have a dataTable , in which I want to dynamically delete the row from the dataTable if the datarow[0] to datarow[3] are empty.

    You can use a for loop to iterate over the datarows, Check if they are empty, and if so, delete them.

    Here a demo for you as a reference.

    DataTable dt = new DataTable()
    for (int i = 0; i < dt.Rows.Count; i++)
             {
                 DataRow dr = dt.Rows[i];
                 if (string.IsNullOrEmpty(dt.Rows[i][0].ToString()) == true && string.IsNullOrEmpty(dt.Rows[i][1].ToString()) && string.IsNullOrEmpty(dt.Rows[i][2].ToString()))
                       {
                           dr.Delete();
                       }
             }

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 31, 2020 3:18 AM