Help with Creating a Custom DataGrid in Vb.net

Shahab a 261 Reputation points
2024-09-19T12:01:49.47+00:00

Hello everyone, I am writing a program using Visual Basic .NET.

I would like to have a custom DataGrid that meets the following conditions:

In the Edit Columns - Bound Column Properties section, I want to have several properties as follows:

IsNumeric, which is a Boolean value. If it's set to True, that cell should only accept numeric values.

IsDecimal, which is a Boolean value. If it's set to True, that cell should only accept decimal values.

HasButton, which is a Boolean value. If it's set to True, the cell should have a button on the left side, in addition to being a textbox. This button should have a Click event.

Regarding the third point, it is important to me that when the user clicks on the target cell, the button should appear, and when the focus leaves the textbox, the button should be hidden.

I would appreciate your help.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,737 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 32,376 Reputation points Microsoft Vendor
    2024-09-20T02:36:29.24+00:00

    Hi @Shahab a ,

    To implement a custom DataGridView in VB.NET that supports the features you described (i.e., numeric and decimal validation, and a button appearing when the cell is focused), you will need to create a custom DataGridViewColumn and DataGridViewCell. Here's how you can approach the solution:

    1. Numeric and Decimal Validation:
      • You can create custom column properties like IsNumeric and IsDecimal in a derived DataGridViewTextBoxColumn.
      • Override the EditingControlShowing event to enforce numeric and decimal input.
    2. Button Appearance on Focus:
      • You will need a combination of a DataGridViewTextBoxCell and a button. The button will appear dynamically when the cell is focused, and it will disappear when the focus is lost.
      • Use a DataGridView custom column and cell to handle the dynamic button display logic.

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.