简体   繁体   中英

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. 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. 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. 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. 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)? I've created a couple of user controls in the past and am fairly comfortable with that.

Brian is right in the comment above. If you want customization, WPF is the way to go. However, coming from a WinForms background and starting fresh with WPF will be a steep learning curve.

Writing you own DataGridView -like control from scratch I don't view as a viable option. Reflect the code for the DataGridView and you will see why, there are thousands of lines of code for this component. If you mean that you will override the DataGridView class then, cool, that is a good idea. If it is cell based controls like the data picker you want you may be better overriding/sub-classing the DataGridViewCell instead...

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...

You can get the filtering you require, by just binding the grid to a DataSource like a DataTable and filtering that. 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. Their WinForms products are very good, and in particular, their win grid control sounds like it would meet your needs. 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). Their controls are somewhat expensive, but it's there if it suits your needs.

A good alternative for DataGridView is SourceGrid :

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. 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. 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. 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.

Good luck.

Extending the functionality of a DataGridView isn't easy but can be done as I've done it before 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.

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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