This paper studies the problem regarding the optimal power dispatch of photovoltaic (PV) distributed generators (DGs) in Direct Current (DC) grid-connected and standalone networks. The mathematical model employed considers the reduction of operating costs, energy losses, and CO2 emissions as objective functions, and it integrates all technical and operating constraints implied by DC grids in a scenario of variable PV generation and power demand. As a solution methodology, a master–slave strategy was proposed, whose master stage employs Antlion Optimizer (ALO) for identifying the values of power to be dispatched by each PV-DG installed in the grid, whereas the slave stage uses a matrix hourly power flow method based on successive approximations to evaluate the objective functions and constraints associated with each solution proposed within the iterative process of the ALO. Two test scenarios were considered: a grid-connected network that considers the operating characteristics of the city of Medellín, Antioquia, and a standalone network that uses data from the municipality of Capurganá, Chocó, both of them located in Colombia. As comparison methods, five continuous optimization methods were used which were proposed in the specialized literature to solve optimal power flow problems in DC grids: the crow search algorithm, the particle swarm optimization algorithm, the multiverse optimization algorithm, the salp swarm algorithm, and the vortex search algorithm. The effectiveness of the proposed method was evaluated in terms of the solution, its repeatability, and its processing times, and it obtained the best results with respect to the comparison methods for both grid types. The simulation results obtained for both test systems evidenced that the proposed methodology obtained the best results with regard to the solution, with short processing times for all of the objective functions analyzed.