????? ????? ???? ???? DataGridTextBoxColumn ????? Windows ??? ?????? ????? ????

?????? ????????? ?????? ?????????
???? ???????: 318581 - ??? ???????? ???? ????? ????? ??? ???????.
??? ????? ??? ???????. ????? "??? ??" ??? ??? ??????? ??? ????.
????? ???? | ?? ????

?? ??? ??????

??????

???? ??? ??????? ???? ????? ??? ????? ????? ???? ?????? DataGridTextBoxColumn ????? Windows ???? ????? ????? ???? ???????? ????? ???????? ?? DataGrid ????? Windows.

???????? ???? ???? ??? ??? ????? "CR" ??? ??????? ??????? ?????? ????? ????? ???????? ??????? "CR" ??? ?????. ????? ????? ??? ??????? ??? ?? ??? ?????? ??? ?? ??? ????????? ??????? ??????? ?? ??? ????????. ???? ???????? ????? ????? GetColumnValueAtRow. ????? ???????? ? ????? ????? ????????.

???? ???????

  1. ??? ????? Microsoft Visual Studio .NET ?? ?? ?????? ????? Visual Basic Windows ????? ????.
  2. ????? ???? ???? DataGrid ??? ???????.
  3. ???? ?? ??? ????? ???????? ???????? ??????? ?? ???? ????????? ???????? ???????. ????? ????? ????? CRTextBoxColumn ???? ???? ??? DataGridTextBoxColumn. ??? ???? ????? ????? Form.Load ????? ????? ?????? ? ????? DataGrid ???????? ??? CRTextBoxColumn:
    Private ds As New DataSet()
    
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
      '
      ' Generate sample data
      '
      ' Two columns:
      '    Decimal - uses CRTextBoxColumn for column style.
      '    Integer - uses standard DataGridTextBoxColumn for column style.
      '
      Dim dt As New DataTable()
      dt.TableName = "TestTable"
      dt.Columns.Add("Amount", GetType(Decimal))
      dt.Columns.Add("ColB", GetType(Integer))
      ds.Tables.Add(dt)
      dt.Rows.Add(New Object() {2, 4})
      dt.Rows.Add(New Object() {-3.45, 5})
      dt.Rows.Add(New Object() {4.25, 6})
      dt.Rows.Add(New Object() {-1.33, 7})
    
      '
      ' Create DataGridTableStyle and DataGridColumnStyle objects
      ' and add them the the DataGrid.
      '
      Dim ts As New DataGridTableStyle(), cs As DataGridColumnStyle
    
      ' Add the custom column style.
      cs = New CRTextBoxColumn()
      cs.Width = 120
      cs.MappingName = "Amount"        ' Map to decimal column.
      cs.HeaderText = "Charge/Payment"
      ts.GridColumnStyles.Add(cs)
    
      ' Add the standard column style.
      cs = New DataGridTextBoxColumn()
      cs.Width = 100
      cs.MappingName = "ColB"          ' Map to integer column.
      cs.HeaderText = "Integer Col"
      ts.GridColumnStyles.Add(cs)
    
      ts.MappingName = "TestTable"     ' Map table style to TestTable.
      DataGrid1.TableStyles.Add(ts)
    
      DataGrid1.DataSource = ds
      DataGrid1.DataMember = "TestTable"
    
    End Sub
    
    
    Class CRTextBoxColumn
      Inherits DataGridTextBoxColumn
    
      Protected Overrides Function GetColumnValueAtRow(ByVal cm As CurrencyManager, ByVal RowNum As Integer) As Object
        '
        ' Get data from the underlying record and format for display.
        '
        Dim oVal As Object = MyBase.GetColumnValueAtRow(cm, RowNum)
        If oVal.GetType Is GetType(DBNull) Then
          Return ""                         ' String to display for DBNull.
        Else
          ' CDec on next statement will throw an exception if this
          ' column style is bound to a column containing non-numeric data.
          Dim Temp As Decimal = CDec(oVal)
          If Temp >= 0 Then
            Return Temp.ToString("0.00")             ' positive number
          Else
            Return (-Temp).ToString("0.00") & "CR"   ' negative number
          End If
        End If
      End Function
    
      Protected Overrides Function Commit(ByVal cm As CurrencyManager, ByVal RowNum As Integer) As Boolean
        '
        ' Parse the data and write to underlying record.
        '
        Me.HideEditBox()   ' return focus to the DataGrid control
        Dim box As DataGridTextBox = CType(Me.TextBox, DataGridTextBox), Value As Decimal
        ' Do not write data if not editing.
        If box.IsInEditOrNavigateMode Then Return True
        If TextBox.Text = "" Then   ' in this example, "" maps to DBNull
          SetColumnValueAtRow(cm, RowNum, DBNull.Value)
        Else
          ' Parse the data.
          Try
            If TextBox.Text.ToUpper.EndsWith("CR") Then
              Value = -Decimal.Parse(TextBox.Text.Substring(0, TextBox.Text.Length - 2))
            Else
              Value = Decimal.Parse(TextBox.Text)
            End If
          Catch
            Return False    ' Exit on error and display old "good" value.
          End Try
          SetColumnValueAtRow(cm, RowNum, Value)   ' Write new value.
        End If
        Me.EndEdit()   ' Let the DataGrid know that processing is completed.
        Return True    ' success
      End Function
    
    End Class
    					
  4. ?? ?????? ???????. DataGrid ????? ??? ?????? ?????? ????:
    Charge/Payment   Integer Col
    ----------------------------
    2.00                       4
    3.45CR                     5
    4.25                       6
    1.33CR                     7
    						
  5. ????? ????????. ????? ??? ?????? ??????. ??? ??? ?????? ???? ????? ?? ?????? ????? (??? ???? ???????-5.43) ? ???? ???? ????? negate (-). ????? ?? ???? ??? ????? "CR" (??? ???? ??????? 5.43CR). ????? ????? ????? ????? ???????? ??????? "CR" ?????? ?? DataGrid.

pitfalls

???? ??? ????? ??? ??????? ??????? ???? ?? ??????? ??? ????? ????? ???????? ???????? ??????? ?? ??? ???????.
  • ??? ????? ??? ??? ???? ??????? ?? ??? ??????? ??? ???? ?? ????? ??? ?????? ?????. ??? ??? ????? ?????? ??????? ??? ??? ????????.
  • ?? ???? ??? ???? ?? ????? ??? ?????? ????? ??? ?????? ?? ?? ????? ????????? ????? ???????? ?? ?????????. ??? ?? ??? ????? ?? ???? ????? ?? ????? ??? ????????. ?????? ???? ????? ??? ????? ?? ???????. ?? ??? ????? ????? ??????? ???????? ?????? ?? ????? ?????? ??????? ?????? ???? ?? ????? ?? ????????.
  • ?? ???? ?????? ?? ???? ????? ??????? ?????. ?? ???? ??? ????? ??????? ?????? ?????? 0 (????).
  • ?? ???? ???????? ??? ??? ?? ??????? MappingName ??????? DataGridTableStyle ? DataGridColumnStyle ????? ??????? ??????? ?????? ???.
  • ??? ???? ???? ?? ???? ????? ??????? HeaderText.

???????

???? ???????: 318581 - ????? ??? ??????: 28/???? ??????/1435 - ??????: 3.2
????? ???
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft ADO.NET 1.1
  • Microsoft .NET Framework Software Development Kit 1.0
  • Microsoft .NET Framework Software Development Kit 1.0 Service Pack 2
????? ??????: 
kbnosurvey kbarchive kbmt kbhowtomaster KB318581 KbMtar
????? ????
???: ??? ????? ??? ?????? ???????? ?????? ????? ???? ????? ?????????? ????? ?? ????????? ?????? ????. ???? ???? ?????????? ???? ?? ???????? ???????? ?????? ????????? ????? ????????? ???????? ????? ???????? ?????? ?? ?????? ??? ?? ???????? ???????? ?? ????? ??????? ?????? ??? ??????? ?????? ??. ?????? ?? ???? ??? ??????? ???????? ????? ?? ???? ????? ?????? ??? ????? ??? ????? ??????? ?? ????? ?? ?????? ??? ??? ??????? ??????? ?? ????? ????? ????? ????? ?????. ?? ????? ???? ?????????? ??????? ??? ????? ?? ??????? ?? ????? ?????? ?? ??? ????? ?? ????? ??????? ?? ???????? ?? ??? ???????. ???? ???? ?????????? ???????? ??? ????? ?????? ??????? ??????
???? ??? ????? ??????? ?????? ??????????318581

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com