简体   繁体   English

Datagridview还是其他?

[英]Datagridview or something else?

I'm writing a winforms application which stores its data as plain text files and presents it to the user as a multi-column list. 我正在编写一个winforms应用程序,该应用程序将其数据存储为纯文本文件,并将其作为多列列表呈现给用户。 I'd like the user to be able to sort and filter the list, and also to re-order and hide/unhide columns. 我希望用户能够对列表进行排序和过滤,还可以对列进行重新排序和隐藏/取消隐藏。

I thought a DataGridView would be a good fit since it has a lot of that functionality built in, but I'm going to need some cell types (a date picker for instance) that are not available out of the box with a DataGridView. 我认为DataGridView会很合适,因为它内置了许多功能,但是我将需要一些DataGridView不能立即使用的单元格类型(例如日期选择器)。 I know you can host controls inside a DGV and have read a Technet article on it, but it seems fairly complex and I'm newish to C#, Winforms, and OOP. 我知道您可以在DGV中托管控件并阅读了Technet上的文章,但是它看起来相当复杂,并且对C#,Winforms和OOP不熟悉。 A DGV is also not the prettiest control, and even though I know how to change its properties to make it look somewhat nicer, it never gets to where I really like it. DGV也不是最漂亮的控件,即使我知道如何更改其属性以使其看起来更好,它也从未到达我真正喜欢的地方。 Appearance isn't a dealbreaker if it's the way to go, but it's a "nice ot have." 如果可以的话,外观并不是决定性的事情,但这是一个“不错的选择”。

So my question is: should I be struggling through getting the DGV to do what I want, even if it takes me longer and is more frustrating to do, or should I be rolling my own custom control(s)? 所以我的问题是:我应该为使DGV做我想做的事情而苦苦挣扎,即使我花了更长的时间并且做起来更令人沮丧,还是我应该滚动自己的自定义控件? I've created a couple of user controls in the past and am fairly comfortable with that. 过去,我已经创建了几个用户控件,对此我相当满意。

Brian is right in the comment above. Brian在上面的评论中是对的。 If you want customization, WPF is the way to go. 如果要自定义,则可以使用WPF。 However, coming from a WinForms background and starting fresh with WPF will be a steep learning curve. 但是,来自WinForms的背景并从WPF重新开始将是一个艰难的学习过程。

Writing you own DataGridView -like control from scratch I don't view as a viable option. 从头开始编写自己的类似DataGridView的控件,我认为这不是一个可行的选择。 Reflect the code for the DataGridView and you will see why, there are thousands of lines of code for this component. 反映出DataGridView的代码,您将明白为什么,此组件有成千上万行代码。 If you mean that you will override the DataGridView class then, cool, that is a good idea. 如果您的意思是您将覆盖DataGridView类,那么很酷,那是个好主意。 If it is cell based controls like the data picker you want you may be better overriding/sub-classing the DataGridViewCell instead... 如果它是基于单元格的控件(例如您想要的数据选择器),则最好改写/子类化DataGridViewCell而不是...

You can customize the appearance of the DataGridView to make it look good out-of-the-Box, but don't underestimate the amount of time it will take to sub-class/inherit from DataGridViewCell to make something like a DataPicker , it won't be that enjoyable, but of course possible... 您可以自定义DataGridView的外观以使其看起来开箱即用,但不要低估从DataGridViewCell继承/继承以制作类似DataPicker类的东西所需的时间。不会那么令人愉快,但当然有可能...

You can get the filtering you require, by just binding the grid to a DataSource like a DataTable and filtering that. 您只需将网格绑定到DataTable类的DataSource并将其过滤,即可获得所需的过滤。 This will automatically filter the displayed results. 这将自动过滤显示的结果。

I would think about using an existing library for this, as you will be reinventing the wheel to a large extent. 我会考虑为此使用现有的库,因为您将在很大程度上重新发明轮子。 Of course most controls are commercial and not free; 当然,大多数控件都是商业性的,而不是免费的。 but there must be some that are... 但一定有一些...

I hope this helps. 我希望这有帮助。

I have been using Infragistics for a few years now. 我已经使用Infragistics几年了。 Their WinForms products are very good, and in particular, their win grid control sounds like it would meet your needs. 他们的WinForms产品非常出色,尤其是他们的赢网格控制听起来很满足您的需求。 You can even use their grid columns in DataGridView if you don't want to use their grid (their grid takes a little bit of time to get used to). 如果您不想使用他们的网格,甚至可以在DataGridView中使用他们的网格列(他们的网格需要一点时间来习惯)。 Their controls are somewhat expensive, but it's there if it suits your needs. 他们的控件有些昂贵,但是如果您需要它,它就在那儿。

A good alternative for DataGridView is SourceGrid : DataGridView一个很好的替代方法是SourceGrid

https://sourcegrid.codeplex.com/ https://sourcegrid.codeplex.com/

I tried a test with a calendar form I use for our system: 我尝试使用用于系统的日历表单进行测试:

Private Sub dgvTaskLog_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvTaskLog.CellContentClick
    Dim frm As New frmCalendar
    frm.ShowDialog()
    If IsDate(frm.outSelectedDate) Then
        dgvTaskLog(e.ColumnIndex, e.RowIndex).Value = frm.outSelectedDate
    End If
End Sub

When the user clicks on the cell the custom calendar form opens. 当用户单击单元格时,将打开自定义日历表单。 You would most ikel ned to do some checks on what column to just display the calendar (or other "control") for that column. 您将最需要对要显示该列的日历(或其他“控件”)的列进行一些检查。 Not sure how this works for tabbing around cells, you may need another event to pull it off. 不确定如何在单元格之间切换是如何工作的,您可能需要另一个事件才能将其关闭。

The custom form hosts the VB.Net calendar control and adds local business logic. 自定义窗体承载VB.Net日历控件并添加本地业务逻辑。 If a valid date is returned it is stuffed into the cell. 如果返回有效日期,则将其填充到单元格中。 If the grid is bound you would need to update the datasource instead. 如果网格是绑定的,则需要更新数据源。

Any way - just another possible option. 无论如何-只是另一个可能的选择。

Personally, I would stick with the DataGridView in WinForms. 就个人而言,我会坚持使用WinForms中的DataGridView。 Instead of making a date-picker cell type, I would instead consider just launching a new Form with a date-picker on it to handle the actual time/date entry into the cells. 我不考虑创建日期选择器单元格类型,而是考虑启动一个带有日期选择器的新窗体,以处理进入单元格的实际时间/日期。 This would give you the customization flexibility you need while not adding complexity to the DGV. 这将为您提供所需的自定义灵活性,同时又不会增加DGV的复杂性。 The DGV can be left mostly or completely as-is with just out-of-the-box functionality, and then you can build your custom functionality around it as suggested with the date-picker on your own custom form, for example. DGV可以直接使用大多数功能,也可以完全保留原样,然后,可以根据自己的自定义表单上的日期选择器的建议,围绕它构建自定义功能。

Good luck. 祝好运。

Extending the functionality of a DataGridView isn't easy but can be done as I've done it before Report Manager . 扩展DataGridView的功能并不容易,但是可以像在Report Manager之前所做的那样完成。 You can also create custom field controls. 您还可以创建自定义字段控件。 Perhaps it might be easiest to create a template field with your gridview and use the ajax calendar extender. 也许最简单的方法是使用gridview创建一个模板字段并使用ajax日历扩展程序。

<asp:GridView ID="myGridView" runat="server">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="myTextBox" runat="server" />
                <ajaxToolkit:CalendarExtender ID="calDate" runat="server" TargetControlID="myTextBox" Format="MM/dd/yyyy" SelectedDate='01/01/2016'></ajaxToolkit:CalendarExtender>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM