Search code examples
c#datatabledatasetrow

Remove row from datatable if column is not numeric


I have a datatable with a few columns. The first column is our employee id. Unfortunately not all of the rows are numeric and we want to remove those who aren't numeric. For instance, we have 1 row which has "@#$" and I want to remove rows like these. I currently have the following code.

        var len = dt.Rows.Count;
        for(int y = 0; y < len; y++)
        {
            var mwd = dt.Columns[0].ToString();
            bool valid = int.TryParse(mwd, out int n);
            if (valid)
            {
                log.LogInformation("mwd is numeric");
            }
            else
            {
                log.LogInformation("mwd is not numeric");
                dt.Rows[y].Delete();
            }
        }

However, this doesn't remove the row. What am I doing wrong? Thanks in advance.

EDIT: Surrounding code

DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        string[] columns = { "Mwd", "Naam", "Kostenplaats Externe id (Klant)", "Kostenplaats Loonlijstcode (Activiteit)", "Kostenplaats Naam (Activiteit)", "Datum", "Uren ruw", "Ber. Uren", "Verlof volledig pad" };
        foreach (string column in columns)
        {
            dt.Columns.Add(column);
        }
        using (StreamReader reader = new StreamReader(req.Body))
        {
            while (reader.EndOfStream == false)
            {
                string[] rows = reader.ReadLine().Split(',');
                DataRow dr = dt.NewRow();
                for (int i = 0; i < columns.Length; i++)
                {
                    var temp = rows[i].Trim('"');
                    dr[i] = temp.Trim('\'');
                }
                dt.Rows.Add(dr);
            }
        }

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            foreach (DataColumn column in dt.Columns)
            {
                var mwd = dt.Rows[i][column].ToString();
                int n;
                bool valid = int.TryParse(mwd, out n);
                if (valid)
                {
                    log.LogInformation("mwd is numeric");
                }
                else
                {
                    log.LogInformation("mwd is not numeric");
                    dt.Rows[i].Delete();
                    i--;
                    break;
                }
            }
        }
        dt.AcceptChanges();

        log.LogInformation(dt.ToString());


        for (int x = 0; dt.Rows.Count > x; x++)
        {
            string sql = "INSERT INTO dbo.kronos (Mwd, Naam, KostenplaatsExterneIdKlant, KostenplaatsLoonlijstcodeActiviteit, KostenplaatsNaamActiviteit, Datum, UrenRuw, BerUren, VerlofVolledigPad)" +
                " VALUES ('" + dt.Rows[x]["Mwd"].ToString() + "', '" + dt.Rows[x]["Naam"].ToString() + "', '"
                + dt.Rows[x]["Kostenplaats Externe id (Klant)"].ToString() + "', '" + dt.Rows[x]["Kostenplaats Loonlijstcode (Activiteit)"].ToString() + "', '"
                + dt.Rows[x]["Kostenplaats Naam (Activiteit)"].ToString() + "', '" + dt.Rows[x]["Datum"].ToString() + "', '"
                + dt.Rows[x]["Uren ruw"].ToString() + "', '" + dt.Rows[x]["Ber. Uren"].ToString() + "', '" + dt.Rows[x]["Verlof volledig pad"].ToString() + "')";

            var str = Environment.GetEnvironmentVariable("ConnectionString");

            using (SqlConnection connection = new SqlConnection(str))
            {
                SqlCommand command = new SqlCommand(sql, connection);
                connection.Open();
                command.ExecuteNonQuery();
            }
        }

        return result;

Solution

  • try this code

    for (int i = 0; i< dt.Rows.Count;i++)
    {
       foreach (DataColumn column in dt.Columns)
       {
           var mwd = dt.Rows[i][column].ToString();
           int n;
           bool valid = int.TryParse(mwd, out n);
           if (valid)
           {
              log.LogInformation("mwd is numeric");
           }
           else
           {
             log.LogInformation("mwd is not numeric");
             dt.Rows[i].Delete();
             i--;
             break;
           }
        }
     }
    dt.AcceptChanges();
    

    If you know the name of the column or its index, use the following code

    for (int i = 0; i < dt.Rows.Count; i++)
    {
        var mwd = dt.Rows[i]["Name"].ToString();
        //or---------------------------------
        var mwd = dt.Rows[i][index].ToString();
        int n;
        bool valid = int.TryParse(mwd, out n);
        if (valid)
        {
           log.LogInformation("mwd is numeric");
        }
        else
        {
           log.LogInformation("mwd is not numeric");
           dt.Rows[i].Delete();
           i--;
        }
    }
    dt.AcceptChanges();