I agree with everybody about the low importance of grades, but offer one suggestion. Maintain good enough grades to not be kicked out of your program. At my institution, two C's on your transcript will get you the boot.
First, let me say that I don't know exactly how bad your grades are since I do not know the rules in your country.
My impression is that you care too much about grades. A single grade (or even three) is often not only related to how you do in the subject, but also to many other things -- how strict the prof grades, in what kind of mental state the prof is, ...
It is true that research matters much more than coursework in graduate studies (if you get a paper in Science then no one will care if you got a B- in some random course). I half-jokingly tell my students that they should get the minimal grades needed to keep their scholarship.
That said, bad grades in your own field of study are not a positive indication ...
My advisor says the grades and courses are a formality and I should focus on my research as I will learn more by doing than through any coursework.
Your advisor is right. Listen to him. Class in an engineering Ph. D. program mean very little. Research is more important by an order of magnitude.