Results 1 to 27 of 27

Thread: Dataview with textbox on each row

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    15

    Dataview with textbox on each row

    I have a solution in vs2005 vb.net.

    I have a 'personalized' dataview with different datacolumns.

    I'd like to add a 'personalized' textbox in one of these columns.

    How can i add it ?

    Thank you and excuse for my english.

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

    Re: Dataview with textbox on each row

    That doesn't actually make sense. A TextBox is a control, i.e. a UI element, and a DataView has nothing to do with UIs. A DataView is a way to access data from a DataTable using sorting and filtering. It is not visual in any way.

    Do you actually mean a DataGridView, which is a UI element and is intended for the visual display and editing of data?
    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    15

    Re: Dataview with textbox on each row

    Yes... it is a datagridview... ( excuse me )

    and i'd like to have my customized textbox.

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

    Re: Dataview with textbox on each row

    Check out this thread for an example of creating custom column and cell classes.

    http://www.vbforums.com/showthread.php?t=554744

    If you need to use a control other than the standard TextBox then you'll also need to inherit that control and implement the IDataGridViewEditingControl interface.
    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

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    15

    Re: Dataview with textbox on each row

    Thank you very much.
    I saw the code and i used it.
    I created my : DataGridView_my_text_column
    with my : my_TextboxCell
    and in the InitializeEditingControl used "my_TextBoxEditingControl"

    "my_TextBoxEditingControl" is not a textbox may a my_text
    ( is similar by i have lots of proprierties like my_sqlconnection , my_sqltable.. etc etc )

    I need to pass parameters to my_TextBoxEditingControl because i'd like to use it in all of my form and sometimes i need have my_table="customers" other my_table="orders" ( f.e. )

    The answer is : is better ( or can i ) bass the parameters when i dim DataGridView_my_text_column or after e added it to my grid ?

    Thank you
    Luca

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

    Re: Dataview with textbox on each row

    How do you set properties of the standard column types? It is exactly the same for your custom column.
    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

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    15

    Re: Dataview with textbox on each row

    In the other standard column I set just :

    New_column.HeaderText
    .Resizable
    .Width
    .Name
    .DataPropertyName
    .ReadOnly

    I do it after dim and before add column to grid ... and i haven't problem...
    In the other column i'd set .my_sqlconnection .my_table that are in "my_TextBoxEditingControl".
    how can i do ?

  8. #8

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    15

    Re: Dataview with textbox on each row

    I try to do this :

    vb Code:
    1. Dim New_Column As New DataGridView_My_Text_Column(Conn_SQL)
    2.         My_grid.Columns.Add(New_Column)
    3.  
    4.  
    5.     Public Class DataGridView_my_text_column
    6.         Inherits DataGridViewColumn
    7.  
    8.         Public Sub New(ByVal conn_sql As SqlClient.SqlConnection)
    9.             MyBase.New(New my_TextboxCell(conn_sql))
    10.         End Sub
    11.  
    12.         Public Overrides Property CellTemplate() As System.Windows.Forms.DataGridViewCell
    13.             Get
    14.                 Return MyBase.CellTemplate
    15.             End Get
    16.             Set(ByVal value As System.Windows.Forms.DataGridViewCell)
    17.  
    18.                 If value IsNot Nothing AndAlso _
    19.                     Not value.GetType.IsAssignableFrom(GetType(my_TextboxCell)) _
    20.                     Then
    21.                     Throw New InvalidCastException("Must be TextboxCell")
    22.                 End If
    23.                 MyBase.CellTemplate = value
    24.             End Set
    25.         End Property
    26.  
    27.     End Class
    28.  
    29.     Public Class my_TextboxCell
    30.         Inherits DataGridViewTextBoxCell
    31.  
    32.         Public Sub New(ByVal conn_sql As SqlClient.SqlConnection)
    33.             Me.connection = conn_sql
    34.         End Sub
    35.         Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
    36.         ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle)
    37.  
    38.             MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
    39.                                             dataGridViewCellStyle)
    40.  
    41.             Dim ctl As my_TextBoxEditingControl = _
    42.                 CType(DataGridView.EditingControl, my_TextBoxEditingControl)
    43.  
    44.             ctl.My_SqlConnection = Me.connection
    45.             If IsDBNull(Me.Value) Then
    46.                 ctl.Text = ""
    47.             Else
    48.                 ctl.Text = CType(Me.Value, String)
    49.             End If
    50.  
    51.         End Sub
    52.  
    53.         Public Property connection() As SqlClient.SqlConnection
    54.             Get
    55.                 Return Nothing
    56.             End Get
    57.             Set(ByVal value As SqlClient.SqlConnection)
    58.                 Me.Value = value
    59.             End Set
    60.         End Property
    61.  
    62.         Public Overrides ReadOnly Property EditType() As Type
    63.             Get
    64.                 Return GetType(my_TextBoxEditingControl)
    65.             End Get
    66.         End Property
    67.  
    68.         Public Overrides ReadOnly Property ValueType() As Type
    69.             Get
    70.                 Return GetType(String)
    71.             End Get
    72.         End Property
    73.  
    74.     End Class
    75.     Public Class my_TextBoxEditingControl
    76.         Inherits My_Text
    77.         Implements IDataGridViewEditingControl
    78.  
    79.         Private dataGridViewControl As DataGridView
    80.         Private valueIsChanged As Boolean = False
    81.         Private rowIndexNum As Integer
    82.        
    83.         Public Sub New()
    84.         End Sub
    85.         Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle) _
    86.             Implements System.Windows.Forms.IDataGridViewEditingControl.ApplyCellStyleToEditingControl
    87.             Me.Font = dataGridViewCellStyle.Font
    88.         End Sub
    89.  
    90.         Public Property EditingControlDataGridView() As System.Windows.Forms.DataGridView Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlDataGridView
    91.             Get
    92.                 Return dataGridViewControl
    93.             End Get
    94.             Set(ByVal value As System.Windows.Forms.DataGridView)
    95.                 dataGridViewControl = value
    96.             End Set
    97.         End Property
    98.  
    99.         Public Property EditingControlFormattedValue() As Object _
    100.             Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlFormattedValue
    101.             Get
    102.                 Return Me.Text
    103.             End Get
    104.             Set(ByVal value As Object)
    105.                 If TypeOf value Is String Then
    106.                     Me.Text = value.ToString()
    107.                 End If
    108.             End Set
    109.         End Property
    110.         Public Property EditingControlRowIndex() As Integer Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlRowIndex
    111.             Get
    112.                 Return rowIndexNum
    113.             End Get
    114.             Set(ByVal value As Integer)
    115.                 rowIndexNum = value
    116.             End Set
    117.         End Property
    118.  
    119.         Public Property EditingControlValueChanged() As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlValueChanged
    120.             Get
    121.                 Return valueIsChanged
    122.             End Get
    123.             Set(ByVal value As Boolean)
    124.                 valueIsChanged = value
    125.             End Set
    126.         End Property
    127.  
    128.         Public Function EditingControlWantsInputKey(ByVal keyData As System.Windows.Forms.Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlWantsInputKey
    129.  
    130.         End Function
    131.  
    132.         Public ReadOnly Property EditingPanelCursor() As System.Windows.Forms.Cursor Implements System.Windows.Forms.IDataGridViewEditingControl.EditingPanelCursor
    133.             Get
    134.             End Get
    135.         End Property
    136.  
    137.         Public Function GetEditingControlFormattedValue(ByVal context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object Implements System.Windows.Forms.IDataGridViewEditingControl.GetEditingControlFormattedValue
    138.             Return Me.Text
    139.         End Function
    140.  
    141.         Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements System.Windows.Forms.IDataGridViewEditingControl.PrepareEditingControlForEdit
    142.  
    143.         End Sub
    144.  
    145.         Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.RepositionEditingControlOnValueChange
    146.             Get
    147.                 Return False
    148.             End Get
    149.         End Property
    150.         Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
    151.             valueIsChanged = True
    152.             Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
    153.             MyBase.OnTextChanged(e)
    154.         End Sub
    155.  
    156.  
    157.     End Class
    158.     Public Class DataGridView_my_TextBoxCell
    159.         Inherits System.Windows.Forms.DataGridViewTextBoxCell
    160.     End Class

    I try to pass the Connection 'from' DataGridView_My_Text_Column to CTL ( my_TextboxCell )
    but when i run the solution in the line : My_grid.Columns.Add(New_Column)
    i have an Error : No parameterless constructor defined for this object.

    Can you help me ?

    Thank You Luca

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

    Re: Dataview with textbox on each row

    As the error message suggests, your column class needs a parameterless constructor. When you add a column to a grid in the designer, you aren't able to pass any arguments to the constructor. You must set the properties after the column has been added. As such, you must provide a constructor with no parameters.
    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

  10. #10

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    15

    Re: Dataview with textbox on each row

    ok...
    so i'll do :

    Dim New_Column As New DataGridView_My_Text_Column()
    My_grid.Columns.Add()

    but now... how can i set any properties in my CTL ?

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

    Re: Dataview with textbox on each row

    What's the idea here? You set the column properties, the column sets the cell properties and the cell sets the editing control properties, right? So, when a cell is added to the column, the column gets its current property values and passes them to the new cell. Whenever a property value changes on the column, it passes that value to all cells.

    I suggest that you download a copy of .NET Reflector and use it to see how it's done in the standard columns in the Framework, then emulate that in your own classes.
    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

  12. #12

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    15

    Re: Dataview with textbox on each row

    Some questions :
    .NET Reflector is enought for me ? I saw that is freeware and the Pro version isn't....
    .Net Replector disassemble and analyze .NET components.... is it easy to use ?
    and with it ... do u think thatn i'll resolve my problem ???

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

    Re: Dataview with textbox on each row

    You tell me whether it was easy to use after you've used it.
    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

  14. #14

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    15

    Re: Dataview with textbox on each row

    I downloaded and installed .net reflector 6.5
    and now ??
    i saw that i can navigate in the 'control' but i don't understand how can it help me...
    Excuse me ..
    Luca

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

    Re: Dataview with textbox on each row

    .NET Reflector decompiles .NET assemblies and translates them into very close approximations of the original source code. As I said in post #11:
    download a copy of .NET Reflector and use it to see how it's done in the standard columns in the Framework
    You can view the source code for the DataGridViewTextBoxColumn, etc, and see how they work, then write your code to work in the same way.
    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

  16. #16

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    15

    Re: Dataview with textbox on each row

    Hi..
    i try to view code about DataGridViewTextBoxColumn ...cellTemplate ...DataGridViewCell...
    .net reflector is really incredible... but i don't understand what i fave to copy .. and what to change ..
    For example .. DataGridViewCell is 2800 line ... which class / proprierties i have to change ?

    Thank you
    Luca

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

    Re: Dataview with textbox on each row

    First, what is it that you're trying to accomplish here? You're trying to understand how to pass property values from the column to the cell and from the cell to the editing control, right? You need to understand the principle so that you can implement the principle in your own classes. That's why I said that you should look at the code for the existing classes, e.g. DataGridViewTextBoxColumn, DataGridViewTextBoxCell and DataGridViewTextBoxEditingControl, to see how it's done there. Once you understand the principle from those examples, then you can implement the principle yourself in your own classes.
    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

  18. #18

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    15

    Re: Dataview with textbox on each row

    It's rigth... and i'm trying to do it..
    i try to pass my sqlConnection as parameter in the METHOD NEW ...
    If i pass it only in DataGridView_my_text_column this don't return error...
    If i pass it also in the

    Public Class my_TextboxCell
    Inherits DataGridViewTextBoxCell

    Public Sub New(ByVal conn_sql As SqlClient.SqlConnection)

    when i run the solution i have the error..

    TY Luca

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

    Re: Dataview with textbox on each row

    We can't help you fix an error if we don't know what error is. That said, it is very bad design for each cell in a grid column to have its own connection to a database. Whatever it is that you're trying to do, I don;t think it's a good idea.
    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

  20. #20

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    15

    Re: Dataview with textbox on each row

    I try to explain you my problem and the solution that i thought.
    I need to use my_textbox in a datacolumn.
    why ?
    Imagea custumer's form.. i have the oreder grid .. and in the first column the article code.
    I'd like to use my_texbox because i have proprieties :
    MY_SQLconnection
    My_Table
    My_code
    My_description
    ...
    ...
    So i can with a double click non y_textbox open a new form with data that i need.
    So... i'd like to create a datacolumn passing some parameters ... and pass then to my_textbox.. or in the My_textbox New .. i'd like to set some of them like the value in the datacolumn.
    Have you understood ?
    Isn't my solution 'correct' ?? how do you execute it ?
    Thank you
    Luca

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

    Re: Dataview with textbox on each row

    That's very much the wrong way to go about it. A DataGridView is a control, i.e. a UI element. It's job is to present data to the user and to receive user input. Adding data access code to a UI element is a very bad idea. Almost all enterprise applications are built in multiple layers, and the data access code and the presentation code don't even know that each other exist, as they are completely separated by the business logic code.

    In your case, the grid will raise events when various things happen, including double-clicks. Your form should handle those events and then initiate the appropriate action, quite possibly in a completely separate part of the application.

    For a simple example of what you're trying to do, follow the CodeBank link in my signature and check out my thread on updating a grid row in a dialogue.
    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

  22. #22

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    15

    Re: Dataview with textbox on each row

    are you speaking about : "Update Grid Row in Dialogue" ?
    My project is using already another form to update my data.
    The problem is tha i'd like to do that directly in the grid...
    my client 'd like to insert data quickly..

  23. #23

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    15

    Re: Dataview with textbox on each row

    have you understood my problem ?
    can you help me ?
    Thank You

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

    Re: Dataview with textbox on each row

    You're creating a problem where there isn't one. What you're trying to do is already very simple. Embedding data access code into the cells of a grid is a very bad idea so I strongly recommend that you don't do it. It's simple enough to populate a DataTable and bind it to a DataGridView. If the user double-clicks a row in the grid then it's simple enough to handle that event in the form and then perform the appropriate data access outside the grid. That is the correct way to do it and that is the way you should do it. Putting your data access code into the grid itself is not going to make anything faster.
    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

  25. #25

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    15

    Re: Dataview with textbox on each row

    Ok it'd be the perfect way... but i need to use "my_textbox" to 'control' the data and i didn't think that it was so difficult.

    If it is no possible ...

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

    Re: Dataview with textbox on each row

    Creating a TextBox that contains data access code is just bad design and you absolutely should not do it. I strongly suggest that you rethink your design.
    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

  27. #27

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    15

    Re: Dataview with textbox on each row

    Unfortunately in this solution i must do so :-(
    Can u help me ?

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