VB DataGridView question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gail248
    New Member
    • Feb 2008
    • 6

    VB DataGridView question

    I have a datagridview that I am using for filling in the detail of an Invoice. So each row has Item, Quantity, Price.

    I want to prevent the user from adding new rows if the Quantity and Price columns are not filled in.

    Also when the user selects an Item (there is a combo fox on that column) I want to get the Price and fill in the Price column - what is the correct event to use for this?
  • abev
    New Member
    • Jan 2008
    • 22

    #2
    Originally posted by Gail248
    I have a datagridview that I am using for filling in the detail of an Invoice. So each row has Item, Quantity, Price.

    I want to prevent the user from adding new rows if the Quantity and Price columns are not filled in.

    Also when the user selects an Item (there is a combo fox on that column) I want to get the Price and fill in the Price column - what is the correct event to use for this?
    To prevent adding new rows you need to handle a focus event from the datagridview. First I would set Addnew = false for the gridview. Then set it to true after meeting your conditions.

    Here's code I have that does something similar from the CellEnter event of the gridview:

    Code:
    Dim m_RowIndex As Integer = e.RowIndex
    If e.ColumnIndex = Me.InsertionOrderDetailDataGridView.Columns("NetCost").Index Then
    
    If IsDBNull(InsertionOrderDetailDataGridView.Rows(m_RowIndex).Cells("CPM").Value) And _
     
                    If MsgBox("Calculate Net Cost?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Calculate?") = MsgBoxResult.Yes Then
    
                      'test the columns here
    
                    End If
                End If
    The second part of your question I would handle the cell leave event of the gridview and calculate the value.

    This should get you started :)

    Comment

    • Gail248
      New Member
      • Feb 2008
      • 6

      #3
      The problem with setting Addnew = False is that is that this has to be done After they begin entering a new Row (and remains set like this until they complete it) but how do I determine (which event) that this has happened?

      Originally posted by abev
      To prevent adding new rows you need to handle a focus event from the datagridview. First I would set Addnew = false for the gridview. Then set it to true after meeting your conditions.

      Here's code I have that does something similar from the CellEnter event of the gridview:

      Code:
      Dim m_RowIndex As Integer = e.RowIndex
      If e.ColumnIndex = Me.InsertionOrderDetailDataGridView.Columns("NetCost").Index Then
      
      If IsDBNull(InsertionOrderDetailDataGridView.Rows(m_RowIndex).Cells("CPM").Value) And _
       
                      If MsgBox("Calculate Net Cost?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Calculate?") = MsgBoxResult.Yes Then
      
                        'test the columns here
      
                      End If
                  End If
      The second part of your question I would handle the cell leave event of the gridview and calculate the value.

      This should get you started :)

      Comment

      • abev
        New Member
        • Jan 2008
        • 22

        #4
        Originally posted by Gail248
        The problem with setting Addnew = False is that is that this has to be done After they begin entering a new Row (and remains set like this until they complete it) but how do I determine (which event) that this has happened?
        Ok gail that makes sense I read it too fast - let's see....

        I assume we have some master info on the form then a detail (gridview) on the bottom? Not knowing all the details I can just throw out suggestions.

        On form load if there are no rows in the detail then automatically add one. OR on gridview_enter if there are no rows you can add one.

        After a row is added you can then set addnew = false. Check to be sure your fields are filled. If they are filled correctly on cellleave in the gridview, programatically add a new row.

        This can get tricky because you probably have an indeterminate number of detail rows in the gridview and dont want to add rows willy nilly.

        What I have done in a similar Master/detail app is to let users add rows to their hearts content, but do not let them save if they have not filled in the required rows in the gridview.

        HTH ot least nudges you in a direction.

        Comment

        Working...