|
-
Sep 2nd, 2010, 05:23 PM
#1
Thread Starter
New Member
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.
-
Sep 2nd, 2010, 08:06 PM
#2
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?
-
Sep 3rd, 2010, 02:14 AM
#3
Thread Starter
New Member
Re: Dataview with textbox on each row
Yes... it is a datagridview... ( excuse me )
and i'd like to have my customized textbox.
-
Sep 3rd, 2010, 02:50 AM
#4
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.
-
Sep 6th, 2010, 03:45 AM
#5
Thread Starter
New Member
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
-
Sep 6th, 2010, 04:32 AM
#6
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.
-
Sep 6th, 2010, 04:42 AM
#7
Thread Starter
New Member
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 ?
-
Sep 6th, 2010, 10:58 AM
#8
Thread Starter
New Member
Re: Dataview with textbox on each row
I try to do this :
vb Code:
Dim New_Column As New DataGridView_My_Text_Column(Conn_SQL)
My_grid.Columns.Add(New_Column)
Public Class DataGridView_my_text_column
Inherits DataGridViewColumn
Public Sub New(ByVal conn_sql As SqlClient.SqlConnection)
MyBase.New(New my_TextboxCell(conn_sql))
End Sub
Public Overrides Property CellTemplate() As System.Windows.Forms.DataGridViewCell
Get
Return MyBase.CellTemplate
End Get
Set(ByVal value As System.Windows.Forms.DataGridViewCell)
If value IsNot Nothing AndAlso _
Not value.GetType.IsAssignableFrom(GetType(my_TextboxCell)) _
Then
Throw New InvalidCastException("Must be TextboxCell")
End If
MyBase.CellTemplate = value
End Set
End Property
End Class
Public Class my_TextboxCell
Inherits DataGridViewTextBoxCell
Public Sub New(ByVal conn_sql As SqlClient.SqlConnection)
Me.connection = conn_sql
End Sub
Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle)
MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
dataGridViewCellStyle)
Dim ctl As my_TextBoxEditingControl = _
CType(DataGridView.EditingControl, my_TextBoxEditingControl)
ctl.My_SqlConnection = Me.connection
If IsDBNull(Me.Value) Then
ctl.Text = ""
Else
ctl.Text = CType(Me.Value, String)
End If
End Sub
Public Property connection() As SqlClient.SqlConnection
Get
Return Nothing
End Get
Set(ByVal value As SqlClient.SqlConnection)
Me.Value = value
End Set
End Property
Public Overrides ReadOnly Property EditType() As Type
Get
Return GetType(my_TextBoxEditingControl)
End Get
End Property
Public Overrides ReadOnly Property ValueType() As Type
Get
Return GetType(String)
End Get
End Property
End Class
Public Class my_TextBoxEditingControl
Inherits My_Text
Implements IDataGridViewEditingControl
Private dataGridViewControl As DataGridView
Private valueIsChanged As Boolean = False
Private rowIndexNum As Integer
Public Sub New()
End Sub
Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle) _
Implements System.Windows.Forms.IDataGridViewEditingControl.ApplyCellStyleToEditingControl
Me.Font = dataGridViewCellStyle.Font
End Sub
Public Property EditingControlDataGridView() As System.Windows.Forms.DataGridView Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlDataGridView
Get
Return dataGridViewControl
End Get
Set(ByVal value As System.Windows.Forms.DataGridView)
dataGridViewControl = value
End Set
End Property
Public Property EditingControlFormattedValue() As Object _
Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlFormattedValue
Get
Return Me.Text
End Get
Set(ByVal value As Object)
If TypeOf value Is String Then
Me.Text = value.ToString()
End If
End Set
End Property
Public Property EditingControlRowIndex() As Integer Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlRowIndex
Get
Return rowIndexNum
End Get
Set(ByVal value As Integer)
rowIndexNum = value
End Set
End Property
Public Property EditingControlValueChanged() As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlValueChanged
Get
Return valueIsChanged
End Get
Set(ByVal value As Boolean)
valueIsChanged = value
End Set
End Property
Public Function EditingControlWantsInputKey(ByVal keyData As System.Windows.Forms.Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlWantsInputKey
End Function
Public ReadOnly Property EditingPanelCursor() As System.Windows.Forms.Cursor Implements System.Windows.Forms.IDataGridViewEditingControl.EditingPanelCursor
Get
End Get
End Property
Public Function GetEditingControlFormattedValue(ByVal context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object Implements System.Windows.Forms.IDataGridViewEditingControl.GetEditingControlFormattedValue
Return Me.Text
End Function
Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements System.Windows.Forms.IDataGridViewEditingControl.PrepareEditingControlForEdit
End Sub
Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.RepositionEditingControlOnValueChange
Get
Return False
End Get
End Property
Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
valueIsChanged = True
Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
MyBase.OnTextChanged(e)
End Sub
End Class
Public Class DataGridView_my_TextBoxCell
Inherits System.Windows.Forms.DataGridViewTextBoxCell
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
-
Sep 6th, 2010, 07:05 PM
#9
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.
-
Sep 6th, 2010, 07:30 PM
#10
Thread Starter
New Member
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 ?
-
Sep 6th, 2010, 07:39 PM
#11
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.
-
Sep 7th, 2010, 09:11 AM
#12
Thread Starter
New Member
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 ???
-
Sep 7th, 2010, 07:39 PM
#13
Re: Dataview with textbox on each row
You tell me whether it was easy to use after you've used it.
-
Sep 8th, 2010, 08:28 AM
#14
Thread Starter
New Member
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
-
Sep 9th, 2010, 12:19 AM
#15
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.
-
Sep 10th, 2010, 01:11 PM
#16
Thread Starter
New Member
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
-
Sep 10th, 2010, 07:32 PM
#17
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.
-
Sep 11th, 2010, 04:23 PM
#18
Thread Starter
New Member
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
-
Sep 11th, 2010, 07:54 PM
#19
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.
-
Sep 12th, 2010, 05:39 PM
#20
Thread Starter
New Member
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
-
Sep 12th, 2010, 06:02 PM
#21
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.
-
Sep 12th, 2010, 06:31 PM
#22
Thread Starter
New Member
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..
-
Sep 17th, 2010, 05:58 PM
#23
Thread Starter
New Member
Re: Dataview with textbox on each row
have you understood my problem ?
can you help me ?
Thank You
-
Sep 17th, 2010, 10:15 PM
#24
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.
-
Sep 20th, 2010, 04:06 AM
#25
Thread Starter
New Member
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 ... 
-
Sep 20th, 2010, 04:11 AM
#26
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.
-
Sep 20th, 2010, 05:28 AM
#27
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|