Results 1 to 8 of 8

Thread: [RESOLVED] add control to datagridview

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2007
    Posts
    694

    Resolved [RESOLVED] add control to datagridview

    Hi,
    There is a control called nbText (which formats numbers) which inherits from TextBox.
    How can I place this nbText control inside a datagridview (dgv) control in windows?

    The following does not seem to work:
    Code:
    nbText nbValue = new nbText();
    nbValue.Name = "nbValue";
    nbValue.HeaderText = "nbValue"; //HeaderText is not available...
    nbValue.Width = 300;
    dgv.Columns.Add(nbValue);

    How can I add this nbText control to the dgv?

    Thanks

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: add control to datagridview

    The following article at MSDN explains how to host controls in a DataGridView.

    http://msdn.microsoft.com/en-us/libr...as5c80.aspx#Y0

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2007
    Posts
    694

    Re: add control to datagridview

    I still do not see why I am unable to use what I have done. Any thoughts please? Thanks

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: add control to datagridview

    Quote Originally Posted by arkiboys View Post
    I still do not see why I am unable to use what I have done. Any thoughts please? Thanks
    Usually one will show code rather than have those who want to help guessing which is what you are doing here and also with the same question on MSDN Social forums under Windows Forn Data Controls and Binding forum which it appears they are pointing you in the same direction I have. All I am trying to get across is you will get better assistance when providing more details and if possible code.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: add control to datagridview

    In a DataGridView, each column contains a template for cells in that column. When a row is added, the column uses the template to generate a cell to be placed in that column of the row. The cell knows the type of control used to edit its contents. When the user starts to edit a cell, an instance of that type is created, or an existing instance is used if one exists, and it is embedded in the cell.

    The DataGridView works in a particular way and you need to work with that. First, you need to create a class to act as the editing control. You need to inherit your existing control, in this case nbText (what a terrible name), and implement the IDataGridViewEditingControl interface. You tehn need to define a cell type, inheriting DataGridViewCell or some more specific cell type if it provides some of the functionality you want, e.g. DataGridViewTextBoxCell, and specify your editing control type for that cell. You then need to define a column type that uses your cell type as its cell template. You then add an instance of that column type to a grid and the rest happens automatically. The link provided by KI demonstrates how to implement those steps.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2007
    Posts
    694

    Re: add control to datagridview

    I see.
    Is there a way to place a keypress event on only one column of the datagridview?
    Thanks

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: add control to datagridview

    Many DGV events give you a row index and/or a column index. You can decide if and what to do based on those.

    You might also like to check this out for another example of a custom column. It doesn't use a custom editing control, but you already know how to do that from KI's link.

    http://www.vbforums.com/showthread.php?t=554744
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2007
    Posts
    694

    Re: add control to datagridview

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width