views:

487

answers:

2

A few years ago, I decided to create my own DataGrid as I didn’t like the standard one provided by Microsoft. It’s a very simple function which takes a DataTable as an input parameter and which returns a string (the html code to display a table on a webpage).

It’s very flexible (there are some optional parameters to do the paging, sorting and to format each column the way I want) and fast (only the records which are used are retrieved from the database). The function itself is very short (about 20ish lines of code). I’ve been using it for at least 4 years now.

Assuming you have a PlaceHolder on your webpage, this is how you would call the custom function:

MyPlaceHolder.Controls.Add(new LiteralControl(CreateCustomGrid(MyDataTable)))

CreateCustomGrid(MyDataTable))) would return something like this (if MyDataTable has 2 columns and 2 rows):

<table class="MyClass"  rules="all">
  <tr>
      <th>Column1</th>
      <th>Column2</th>
  </tr>
   <tr>
      <td align="center">Value1</td>
      <td align="center">Value2</td>
  </tr>
    <tr>
      <td align="center">Value3</td>
      <td align="center"><a href=’MyLink’>Value4</a></td>
  </tr>
  </table>

Internally the function knows how to format each column (this function is only being used on one website) but it’s also possible to change it for each individual column by using optional parameters. Same for the paging and sorting. All in all it’s very flexible and very easy to use.

Now things have changed and the DataGrid has been replaced by the GridView and/or ListView. I’ve looked at them but I don’t see anything that they do that my function doesn’t so I would be tempted to carry on using my function but I might be overlooking something. At the same time, it looks a bit odd to carry on using a custom function to generate an html table. What’s your views on this?

A: 

I'd say that you should investigate the GridView/ListView to see what they can do but, ultimately, if you and your customers are happy with your own code and it works for you, does everything you need it to then there's no need to change just because there is something else out there.

Fermin
+1  A: 

If your code works and is seasoned, I wouldn't change any existing code. For additional functionality, you might want to consider wrapping it in a custom data-bound WebControl. That way you can use datasources, etc.

Keltex
+1 My thoughts exactly. Well stated!
Adam McKee