Image copyright Reuters Image caption Gavin Grimm filed a lawsuit after his high school prevented him from using the men's bathroom

A US judge has ruled that federal law protects a transgender student's right to use the bathroom corresponding to his gender identity.

In the latest legal twist to a long-running case, a Virginia court rejected Gloucester County school board's bid to dismiss a lawsuit filed by Gavin Grimm, a student who has since graduated.

Mr Grimm sued after his school barred him from using the men's bathroom.

He said he felt an "incredible sense of relief" after the ruling.

"After fighting this policy since I was 15 years old, I finally have a court decision saying that what the Gloucester County School Board did to me was wrong and it was against the law," he said.

Mr Grimm's case has been the most prominent in the debate over which bathroom transgender people should be permitted to use, a debate that has come to the forefront of LGBT rights over the past few years.

This decision does not completely end his case, but the judge on Tuesday ordered the school board to arrange a settlement conference within 30 days.

"The district court's ruling vindicates what Gavin has been saying from the beginning," said Joshua Block, an attorney for the American Civil Liberties Union.

US district judge Arenda Wright Allen's ruling said the school's argument was "resoundingly unpersuasive", and she refused to throw out Mr Grimm's claim as the school had requested.

Mr Grimm sued Gloucester High School in July 2015, saying its policy of making him use a separate unisex bathroom violated the following:

Title IX of the Education Amendments of 1972, which bars discrimination in education on the basis of sex

Fourteenth Amendment to the US Constitution, which rules no state can deny "equal protection of its laws" to any of its people.

The school had initially allowed him to use the men's bathroom after he explained he had transitioned to male.

But several adults complained about the the move, and the school's principal said he would from then on have to use newly installed single-person bathrooms.

Image copyright Getty Images Image caption Many places in the US now have gender-neutral bathrooms

The lawsuit made its way up to the US Supreme Court after a series of cases in Virginia.

The country's highest court agreed to take the suit after an appeals court ruled in favour of Mr Grimm following a directive from then-President Barack Obama, saying federal law protects transgender bathroom rights.

But the US Supreme Court reversed its decision after President Donald Trump rescinded his predecessor's guidelines.