How to Remove Space From the GridView Cell in C#

This article describes how to remove &nbsp from the cells in a Grid in C#.

This article describes how to remove "&nbsp" from the cells in a Grid in C#. There is a common problem that we come across when we create an empty field in a grid. It is filled with "&nbsp" since the value for that field that we get from the grid is "&nbsp". The procedure to remove this from the grid is provided below.

  1. First create a Web based project in Visual Studio and make a simple Grid. I have attached a screenshot that show the code of the Grid. I made a simple Grid that has one row and four columns in it.

    GridView
     
  2. In the screenshot below I have bound the data to the grid and left the last column of the grid empty.

    grid as empty
     
  3. When you run the preceding code the output of the code is as shown in the screenshot below.

    output
     
  4. When you click on the submit button you will see the "&nbsp" in the empty cell of the grid. On the submit I took the values from the preceding Grid and then bound it into a new data table. The ouput is shown in the screenshot below:

    HtmlDecode

     
  5. The solution for this problem is to add Server.HtmlDecode() at the beginning of the field when we are obtaining its value in a string. The screenshot below shows the code where Server.HtmlDecode() is written before the grid cell.

    HtmlDecode Code
     
  6. Now run the code again and when we click on the submit button; there will be no "&nbsp" in the cell. Please see the screenshot below.

    run the code

I hope this article was useful to you.