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.
Printable View
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.
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?
Yes... it is a datagridview... ( excuse me )
and i'd like to have my customized textbox.
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.
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
How do you set properties of the standard column types? It is exactly the same for your custom column.
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 ?
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
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.
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 ?
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.
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 ???
You tell me whether it was easy to use after you've used it.
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
.NET Reflector decompiles .NET assemblies and translates them into very close approximations of the original source code. As I said in post #11: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.Quote:
download a copy of .NET Reflector and use it to see how it's done in the standard columns in the Framework
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
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.
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
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.
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
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.
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..
have you understood my problem ?
can you help me ?
Thank You
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.
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 ... :(:(
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.
Unfortunately in this solution i must do so :-(
Can u help me ?