1

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’&gt;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?

4

2 回答 2

1

如果您的代码有效且经验丰富,我不会更改任何现有代码。对于其他功能,您可能需要考虑将其包装在自定义数据绑定 WebControl 中。这样你就可以使用数据源等。

于 2009-05-05T15:31:02.540 回答
0

我想说你应该调查 GridView/ListView 看看他们能做什么,但最终,如果你和你的客户对你自己的代码感到满意并且它对你有用,那么你需要它做的一切,那么就没有必要改变只是因为那里有其他东西。

于 2009-05-05T15:30:29.453 回答