在Windows窗体中显示和操作数据,我应该使用哪种控件?

时间:2013-12-26 22:30:58

标签: c# winforms

我想使用C#.NET在Windows窗体中显示数据。这很好,有很多方法可以做到这一点。数据以平面文本文件形式出现。某些数据应显示为跨控件(注释行)的单个元素,而其他数据应显示在包含许多列的表中。但是,我希望整个列表(包括注释)显示在一个列表中。

我的问题是,此时我不确定应该/可以使用什么控件来显示这些不规则数据,或者我应采取什么方法。我应该学习和计划使用什么部分c#表单控件以填补我的目标。我目前的候选人在ListView和DataGridView之间摇摆不定。可以使用哪些工具来满足此需求,其中一些表行将是单个列,而其他表将是多个列(如果有的话)。

让我们说我的数据如下:

red       | green    | purple   | blue
comment that is one single piece of data
Foo----------------- | Bar---------------
red-------| green--- | purple-- | blue
one       | green    | purple   | blue
Foo----------------- | Bar---------------
one       | two      | three    | four
comment that is one single piece of data
comment that is one single piece of data
red       | green    | purple   | blue
one       | green    | purple   | blue
Foo----------------- | Bar---------------
one       | two      | three    | four

1 个答案:

答案 0 :(得分:2)

查看数据结构会非常有帮助。世界上什么是“统一评论列表”? “不规则数据”是什么意思?

无论如何,根据我的经验,Winforms中处理表格数据的最佳工具是DataGridView。你可以有组合框细胞,图像细胞。您可以轻松地对它们进行格式化和着色,并在必要时进行验证和禁用。


修改:更多信息

你需要的基本上是一些可以在HTML网格中轻松获得的colspan。你不能在DataGridView中轻松地做到这一点,引用Ms DataGridView程序管理器

The DataGridView does not have any support for merging cells. You can custom paint
content across cells, but apart from that you would have to do a lot of work and
custom coding to produce merged editable cells.

但您可以购买第三方组件(Telerik GridView或类似组件)或搜索此类自定义实施:

DataGridVewTextBoxCell with Span Behaviour

也许这也很有用:

Multi Column Combo Cell for the .NET 2.0 DataGridView Control

http://social.msdn.microsoft.com/Forums/windows/en-US/fea3dcff-5447-450a-8f7c-bb1c4f63371d/merging-cells-in-datagridview?forum=winformsdatacontrols


编辑3:好的,如果您只是需要显示数据,为什么不尝试在winform中添加webBrowser组件,编写HTML文本然后显示它?您可以像任何网页一样添加样式,字体,颜色等。

样品

private void showHtml()
{
    var htmlstring = @"<html><table border='1'>
      <tr>
        <td>Red</td>
        <td>Green</td>
        <td>Purple</td>
        <td>Blue</td>
      </tr>
        <tr>
        <td colspan='4'>Comment that is one single piece of data</td>
      </tr>
       <tr>
        <td colspan='2'>Foo</td>
         <td colspan='2'>Bar</td>
      </tr>
      <tr>
        <td>Red</td>
        <td>Green</td>
        <td>Purple</td>
        <td>Blue</td>
      </tr>
      <tr>
        <td>Red</td>
        <td>Green</td>
        <td>Purple</td>
        <td>Blue</td>
      </tr>
          <tr>
        <td colspan='4'>Comment that is one single piece of data</td>
      </tr>
      <tr>
        <td>One</td>
        <td>Two</td>
        <td>Three</td>
        <td>Four</td>
      </tr>
    </table>
    </html>";
    webBrowser1.DocumentText = htmlstring;
}

http://oi40.tinypic.com/2uygkjt.jpg