0

I have to implement the below in single custom cell in DatagridView Winform

Please click on link for output image

01
John Doe
Employee Name

Requirements:

  • A cell with three fields. The first field is the row counter, the second database value, and the third is a display value.
  • The third field will be visible; only when the user selects a particular cell.

Implementation :

After referring to the Microsoft documents and StackOverflow thread

  • I have created a user control with three labels in it.
  • I am overriding the paint method to paint the user control on the cell.

Issues:

  • Can the following be implemented without overriding the paint().
  • Is there a memory efficient Any better way to implement the same.

Code References :

Microsoft docs: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.idatagridvieweditingcontrol?redirectedfrom=MSDN&view=netcore-3.1

Stackoverflow code thread: Display user control in DatagridViewCell

  • The StackOverflow link you posted is correct. You don't need to handle the Paint event, unless it's needed for something else. You need to create a custom DataGridViewCell, which is used as the CellTemplate of your DataGridViewColumn. The EditingControl can be your UserControl (or anything else). What you do with your UserControl (what it's bound to and how you use the data entered), is entirely up to you. Also: [How to: Host Controls in Windows Forms DataGridView Cells](https://docs.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-host-controls-in-windows-forms-datagridview-cells) – Jimi Sep 07 '20 at 14:20
  • https://stackoverflow.com/q/16774966/5045688 – Alexander Petrov Sep 07 '20 at 15:32
  • @Jimi My usercontrol consist of 3 text boxes with some data, I just want it to display it on the cell when the program starts. No editing is required as per the datepicker example in microsoft docs. The stackoverflow solution meet the requirement but it have used paint override function to display the controls content on cell. Just was thinking if i could implement the same thing without implementing the editing control. – Hacker_Sam Sep 07 '20 at 17:30
  • 1
    So, if I understand it correctly, you just want to show an un-bound UserControl inside a Cell, to present some values in a *graphical* way. First thing: is it really necessary to show a UserControl there? Can't you use a multi-line Cell instead (as @Alexander Petrov proposed, actually)? Your UC is not even an EditingControl, apparently. If you want this really hard, then follow what's in that link or see a [related answer TaW posted](https://stackoverflow.com/a/29006361/7444103), it may be what you want. I suggest setting the EditingControl anyway, but I understand that's more complex (long). – Jimi Sep 07 '20 at 17:56
  • @Jimi : Thanks for clearly understanding the issue/requirement. Pls refer the link https://i.stack.imgur.com/ifELO.png. What you said is correct i just need to display and no editing is required. I would have implemented it using multi-line cell but as you can see the image each line have different font size and property and also the third line need to be visible when user select the row or the cell, so i decided to move with the usercontrol approch. Any suggestions on that would help ! Thanks – Hacker_Sam Sep 08 '20 at 06:23

0 Answers0