datagrid| Display
Here we talk about how to do data column content and display operation method. This approach is similar to implementation: There is data table test (ID int not NULL PRIMARY key, name varchar (), sex bit), then there is no way to use SQL statements directly, such as: Select ID, name, sex =c ASE Sex (When True Then ' man ' when false Then ' woman ' else sex end) to build to show "male", "female", and actually store true and false? Of course, if we use datagridboolcolumn, we can achieve a similar effect by setting some of its properties (Truevalue,falsevalue), but for those not bit columns? The answer is yes. We use the inheritance DataGridTextBoxColumn class, and then rewrite the Getcolumnvalueatrow method to achieve the effect. Getcolumnvalueatrow method, take the data from the data source, and then judge, return the value we want to display in the grid. (Code see the detailed code later)
In this way, we can successfully display the data we want in the grid, but there is another problem, that is, if we want to modify the data in the grid, can we be submitted to the database? If only through the above operation, only to achieve the purpose of the display, but also to rewrite the Edit,commit,abort method to achieve the click of the cell after the modified content, and then submit, and finally updated to the database.
Effect Figure 1 Below is the complete code of the program, here is just a comment, I hope you can make corresponding improvement according to the need 。
'************************************************************************************
' Program name: Cansetvaluedatagridtextbox
' Function Description: Inherits from DataGridTextBoxColumn class's column style, mainly realizes display value and the actual value display and the update
' Parameter description: None
' Return value: Cansetvaluedatagridtextbox
' Writer: Min
' Date time: 2005-06-16 a.m.
' Legacy problem: Clicking the column header sort will change the display value, is this a bug? Remains to be resolved
'************************************************************************************
Public Class Cansetvaluedatagridtextbox
Inherits System.Windows.Forms.DataGridTextBoxColumn
#Region "code generated by the Windows forms Designer"
Public Sub New ()
MyBase.New ()
' This call is required by the Windows Forms Designer.
InitializeComponent ()
' Add any initialization after the InitializeComponent () call
End Sub
' UserControl overrides dispose to clean up the list of components.
Protected Overloads Overrides Sub Dispose (ByVal disposing as Boolean)
If disposing Then
If not (components are nothing) Then
Components. Dispose ()
End If
End If
Mybase.dispose (disposing)
End Sub
' Required by the Windows Forms Designer
Private Components as System.ComponentModel.IContainer
' NOTE: The following procedure is required by the Windows Forms Designer
' You can use the Windows Forms Designer to modify this procedure.
' Do not modify it using the Code Editor.
<system.diagnostics.debuggerstepthrough () > Private Sub InitializeComponent ()
components = New System.ComponentModel.Container ()
End Sub
#End Region
Private rnum as Integer column ordinal
Private rname as String = "" ' Column name
Type of Private M_type as type column
Public Sub New (ByVal source as DataTable, ByVal rnum as Integer, ByVal displaytrue As String, ByVal TrueValue As String, _
ByVal Displayfalse As String, ByVal Falsevalue as String)
Rnum = Rnum
M_displaytrue = Displaytrue
M_valuetrue = TrueValue
M_displayfalse = Displayfalse
M_valuefalse = Falsevalue
M_type = source. Columns (Rnum). DataType
End Sub
Public Sub New (ByVal source as DataTable, ByVal rname As String, ByVal displaytrue as String, ByVal TrueValue As String, _
ByVal Displayfalse As String, ByVal Falsevalue as String)
Rname = Rname
M_displaytrue = Displaytrue
M_valuetrue = TrueValue
M_displayfalse = Displayfalse
M_valuefalse = Falsevalue
M_type = source. Columns (Rname). GetType
End Sub
Private m_displaytrue as String ' the truth displayed
False value displayed by Private M_displayfalse as String '
Private m_valuefalse as String ' stored false value
The true value of the Private m_valuetrue as String ' Store
'-------------the following content to manipulate the display of data---------------
' Rewrite the procedure to display the data in the appropriate form (★)
Protected Overrides Function Getcolumnvalueatrow (ByVal source as System.Windows.Forms.CurrencyManager, ByVal rownum as Integer) as Object
' Get the data from the data source for the specified row (note: The type used here is Object)
Dim result as Object = Mybase.getcolumnvalueatrow (source, rownum)
' Display of settings
If result.tostring = M_valuetrue Then
Return me.m_displaytrue
ElseIf result.tostring = M_valuefalse Then
Return Me.m_displayfalse
ElseIf result is DBNull.Value Then
If Me.nulltext is nothing Then
Return DBNull.Value
Else
Return Me.nulltext
End If
Else
Throw New Exception ("There are characters in this column that do not have a specified display!") ")
End If
End Function
'---------------The following actions update------------------
Private isediting as Boolean = False ' is in the modified state
Private oldvalue as Object ' original value
' Click on the cell, Ready to edit (★)
Protected Overloads Overrides Sub Edit (ByVal source as System.Windows.Forms.CurrencyManager, ByVal rownum as Integer, Byva L bounds as System.Drawing.Rectangle, ByVal [readOnly] as Boolean, ByVal Instanttext as String, ByVal cellisvisible as Boo Lean
Console.WriteLine ("Edit") '---------
' Directly triggers the parent-class event to achieve the effect of clicking on the selected text. (If you don't trigger, move the textbox to the corresponding border)
Mybase.edit (source, rownum, bounds, [readOnly], Instanttext, cellisvisible)
' Set Modify code
isediting = True
' Record original value
OldValue = Me.getcolumnvalueatrow (source, rownum)
End Sub
' Submit changes to the input (★)
Protected Overrides Function Commit (ByVal dataSource as System.Windows.Forms.CurrencyManager, ByVal rownum as Integer) as Boolean
Console.WriteLine ("In Commit") '---------
' If the modification code is true it means that the modification is complete
If not isediting Then return True
' Get the contents of a cell
Dim CurrentValue as Object = Me.TextBox.Text
Console.WriteLine (currentvalue) '---------
Try
CurrentValue = Setsuitablevalue (CurrentValue)
Setcolumnvalueatrow (DataSource, rownum, CurrentValue)
Catch ex as Exception
Return False
End Try
isediting = False
Invalidate ()
Console.WriteLine ("Out Commit") '---------
Return True
End Function
' Abandonment of the processing
Protected Overrides Sub Abort (ByVal rownum as Integer)
Console.WriteLine ("Abort")
isediting = False
Me.TextBox.Text = OldValue
Me.invalidate ()
End Sub
' Display type conversion function
Private Function Getchangetype (ByVal s as String) as Object
Select case M_type. Tostring
Case "System.integer"
Console.WriteLine ("Integer")
return Convert.ToInt32 (s)
Case "System.Boolean"
Console.WriteLine ("Boolean")
return Convert.toboolean (s)
Case "System.String"
return s
Case "System.Decimal"
return Convert.todecimal (s)
End Select
End Function
Private Function Setsuitablevalue (ByVal s as String) as Object
If s = Me.nulltext Then
Return DBNull.Value
ElseIf s = m_displaytrue Then
Return Getchangetype (M_valuetrue)
ElseIf s = M_displayfalse Then
Return Getchangetype (M_valuefalse)
Else
Throw New Exception ("Enter illegal, check the value you are ready to commit after entering!") ")
End If
End Function
End Class