Results 1 to 4 of 4

Thread: DataGrid's Cell with DataGridTextBox's Change event

  1. #1

    Thread Starter
    Addicted Member parminder's Avatar
    Join Date
    Apr 2003
    Location
    India
    Posts
    168

    DataGrid's Cell with DataGridTextBox's Change event

    Hi to all,

    Here is a problem with my DataGrid.

    Problem 1:
    How to prevent the user to add a new row in DataGrid and allow him to modify existing rows ?

    Problem 2:
    I want to update value of one DataGrid Cell based on other which is going edited by user.

    I create a DataSet (without datasource) with 5 columns : ICode, Name,Qty,Rate and Amount.

    All of these columns are defined with my custom DataTableStyle using my custom

    DataGridTextBox (FormattableTextBoxColumn class) column. This class can raise event when

    FormatableTextBox column value changed (DataGrid.TableStyle(0).C .TextBox_Changed event)

    every time.

    Now I want to update qty, I want Amount should update automatically. It is not happening.


    The Form1 class's code and FormattableTextBoxColumn class's code is like :
    (The entire project code is also as an attachment in TestGrid.Zip file)

    Form1 Class

    VB Code:
    1. Public Class Form1
    2.     Inherits System.Windows.Forms.Form
    3.  
    4. ' Here was Form Generated Code
    5.  
    6.     Dim myDataSet As New DataSet()
    7.     Dim myDataTable As New DataTable("Table1")
    8.     Private OldRow As Integer
    9.     Private OldCol As Integer
    10.  
    11.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    12.  
    13. Handles MyBase.Load
    14.         Dim tableStyle As New DataGridTableStyle()
    15.         tableStyle.MappingName = "Table1"
    16.  
    17.         Dim column As New FormattableTextBoxColumn()
    18.         column.MappingName = "ICode"
    19.         column.HeaderText = "ICode"
    20.         column.Width = 60
    21.         tableStyle.GridColumnStyles.Add(column)
    22.  
    23.         column = New FormattableTextBoxColumn()
    24.         column.MappingName = "Name"
    25.         column.HeaderText = "Name"
    26.         column.Width = 140
    27.         tableStyle.GridColumnStyles.Add(column)
    28.  
    29.         column = New FormattableTextBoxColumn()
    30.         column.MappingName = "Qty"
    31.         column.HeaderText = "Qty"
    32.         column.Width = 40
    33.         AddHandler column.CellTextBoxChanged, AddressOf QtyTextBoxChanged
    34.         tableStyle.GridColumnStyles.Add(column)
    35.  
    36.         column = New FormattableTextBoxColumn()
    37.         column.MappingName = "Rate"
    38.         column.HeaderText = "Rate"
    39.         column.Width = 40
    40.         tableStyle.GridColumnStyles.Add(column)
    41.  
    42.         column = New FormattableTextBoxColumn()
    43.         column.MappingName = "Amount"
    44.         column.HeaderText = "Amount"
    45.         column.Width = 80
    46.         tableStyle.GridColumnStyles.Add(column)
    47.         Me.DataGrid1.TableStyles.Add(tableStyle)
    48.  
    49.     End Sub
    50.  
    51.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    52.  
    53. Handles Button1.Click
    54.         Dim datColumn As New DataColumn("ICode", System.Type.GetType("System.Int16"))
    55.         myDataTable.Columns.Add(datColumn)
    56.         datColumn = New DataColumn("Name", System.Type.GetType("System.String"))
    57.         myDataTable.Columns.Add(datColumn)
    58.         datColumn = New DataColumn("Qty", System.Type.GetType("System.Int16"))
    59.         myDataTable.Columns.Add(datColumn)
    60.         datColumn = New DataColumn("Rate", System.Type.GetType("System.Decimal"))
    61.         myDataTable.Columns.Add(datColumn)
    62.         datColumn = New DataColumn("Amount", System.Type.GetType("System.Decimal"))
    63.         myDataTable.Columns.Add(datColumn)
    64.  
    65.         myDataSet.Tables.Add(myDataTable)
    66.  
    67.         AddMenuItem(101, "Coffee", 4, 25)
    68.         AddMenuItem(102, "Tamato Soup", 2, 20)
    69.         AddMenuItem(103, "Pizza", 2, 80)
    70.         AddMenuItem(104, "Ice Cream", 8, 35)
    71.         AddMenuItem(105, "Cold Drink", 2, 15)
    72.  
    73.         Me.DataGrid1.SetDataBinding(myDataSet, "Table1")
    74.     End Sub
    75.  
    76.     Private Sub AddMenuItem(ByVal pICode As Int16, ByVal pIName As String, ByVal pQty As
    77.  
    78. Int16, ByVal pRate As Decimal)
    79.         Dim tt As DataTable
    80.         tt = myDataTable
    81.         Dim dr As DataRow = tt.NewRow
    82.         dr(0) = pICode
    83.         dr(1) = pIName
    84.         dr(2) = pQty
    85.         dr(3) = pRate
    86.         dr(4) = pQty * pRate
    87.         myDataTable.Rows.Add(dr)
    88.     End Sub
    89.  
    90.     Private Sub QtyTextBoxChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    91.         If Not (OldRow = Me.DataGrid1.CurrentCell.RowNumber And OldCol =
    92.  
    93. Me.DataGrid1.CurrentCell.ColumnNumber) Then
    94.             OldRow = Me.DataGrid1.CurrentCell.RowNumber
    95.             OldCol = Me.DataGrid1.CurrentCell.ColumnNumber
    96.             Exit Sub
    97.         Else
    98.             With Me.DataGrid1
    99.                 .Item(.CurrentRowIndex, 4) = Val(.Item(.CurrentRowIndex, 2)) *
    100.  
    101. Val(.Item(.CurrentRowIndex, 3))
    102.             End With
    103.             MsgBox("Event Fired")
    104.         End If
    105.     End Sub
    106.  
    107. End Class


    FormattableTextBoxColumn class:
    VB Code:
    1. Public Class FormattableTextBoxColumn
    2.     Inherits DataGridTextBoxColumn
    3.  
    4.     Public Event CellTextBoxChanged As System.EventHandler
    5.  
    6.     Public Sub New()
    7.         MyBase.New()
    8.         AddHandler Me.TextBox.TextChanged, New System.EventHandler(AddressOf
    9.  
    10. TxtBoxChanged)
    11.     End Sub
    12.  
    13.     Private Sub TxtBoxChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    14.         RaiseEvent CellTextBoxChanged(Me, e)
    15.     End Sub
    16. End Class



    The entire project is attached.

    Thanks in advance
    Attached Files Attached Files

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474

    Re: DataGrid's Cell with DataGridTextBox's Change event

    Originally posted by parminder
    Problem 1:
    How to prevent the user to add a new row in DataGrid and allow him to modify existing rows ?
    Bind it to a dataview that its allownew is set to false.

    And for the second problem. I wouldnt catch the textbox change event of datagrid, instead i will add this line to the code and let alone that textchange event.
    VB Code:
    1. datColumn = New DataColumn("Amount", System.Type.GetType("System.Decimal"))
    2. [b]datColumn.Expression = "Rate * Qty"[/b]
    3. myDataTable.Columns.Add(datColumn)
    Last edited by Lunatic3; Sep 2nd, 2003 at 12:44 PM.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  3. #3
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    And the reason your code does not work is that while you are changing the text in the datagrid text, the underlying datasource is not updated yet and its value is still the same untill you move away from that column. So using your code you should move to another column, then back to this column and now if you change it. the 'Amount' column will be updated by the previous 'Qty'.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  4. #4

    Thread Starter
    Addicted Member parminder's Avatar
    Join Date
    Apr 2003
    Location
    India
    Posts
    168
    Thanks Lutanic3,

    your alternative solution is good. I also found the other alternative solution. I changed the code of QtyTextBoxChanged procedure as follows (but still your code is good)


    VB Code:
    1. Private Sub QtyTextBoxChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    2.         Dim ftb As FormattableTextBoxColumn
    3.         ftb = sender
    4.         If Not (OldRow = Me.DataGrid1.CurrentCell.RowNumber And OldCol = Me.DataGrid1.CurrentCell.ColumnNumber) Then
    5.             OldRow = Me.DataGrid1.CurrentCell.RowNumber
    6.             OldCol = Me.DataGrid1.CurrentCell.ColumnNumber
    7.             Exit Sub
    8.         Else
    9.             With Me.DataGrid1
    10.                 If Len(Trim(ftb.TextBox.Text)) = 0 Then
    11.                     Exit Sub
    12.                 End If
    13.                 Me.DataGrid1.EndEdit(Me.DataGrid1.TableStyles(0).GridColumnStyles("Qty"), Me.DataGrid1.CurrentRowIndex, False)
    14.                 .Item(.CurrentRowIndex, 4) = Val(.Item(.CurrentRowIndex, 2)) * Val(.Item(.CurrentRowIndex, 3))
    15.                 Me.DataGrid1.BeginEdit(Me.DataGrid1.TableStyles(0).GridColumnStyles("Qty"), Me.DataGrid1.CurrentRowIndex)
    16.                 ftb.TextBox.SelectionStart = Len(Trim(ftb.TextBox.Text))
    17.                 ftb.TextBox.SelectionLength = 0
    18.             End With
    19.         End If
    20.     End Sub

    I will try your solution for problem 1.

    Here is another problem .

    When I design KeyPress or KeyDown events of FormattedTextBox, the event is not fired when I press "Enter" (and some special keys like leftarrow,rightarrow). Other Alfa and Numeric keys works fine. How to handle this problem.

    I want, when user press Enter, Rightside cell in same row should make active (or any specific)

    Thanks

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