CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2007
    Posts
    5

    datagridview help with basics[resolved]

    Hi I am just starting to learn the advanced parts of VB....this datagridview is new to me and I have no idea how to use it properly. Right now when I need to input data into the dgv, I have to do this:

    If vdgv = 0 Then
    dt.Columns.Add("Item")
    dt.Columns.Add(" ")
    dt.Columns.Add("Particulars")
    dt.Columns.Add("QTY")
    vdgv = 1
    End If

    The counter stops the program from adding the headers twice. I then input the data with this

    dgv1.DataSource = dt
    dt.Rows.Add(New Object() {"", txtquan.Text, txtfin.Text, ""})
    dgv1.CurrentCell = dgv1.Rows(0).Cells(0)

    ---------------------------------------------------------------------------------------------
    Is there a proper way for me to add the columns before hand and not have it in the coding???I can add the columns manually but I have no idea how to input the data in....Thanks for your help!!
    Last edited by quantzie; July 16th, 2007 at 07:47 PM. Reason: problem resolved

  2. #2
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: datagridview help with basics


  3. #3
    Join Date
    Jul 2007
    Posts
    5

    Re: datagridview help with basics

    Thanks for the post....guess I didnt notice your post because it was posted some time ago. Just 1 more question though....if my 1st and 3rd column was text and 2nd was a combobox, how do i skip the 2nd??? I tried to use ComboBoxDisplayStyle but it just kept on saying its not a valid value

  4. #4
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: datagridview help with basics

    Code:
            Dim dgvNewRow As New DataGridViewRow
            dgvNewRow.CreateCells(DataGridView1)
    
            dgvNewRow.Cells(DataGridView1.Columns("Column1").Index).Value = "anis"
            'Ignored comboBoxColumn (Column2)
            dgvNewRow.Cells(DataGridView1.Columns("Column3").Index).Value = "khan"
    
            DataGridView1.Rows.Add(dgvNewRow)

  5. #5
    Join Date
    Jul 2007
    Posts
    5

    Re: datagridview help with basics

    your 2nd coding works wonders!!!it cut my lines of coding in half. Thanks!!!

    My columns are now in the form Quantity, Particulars, Unit Price, Total Price

    I would like to show the total price of each item with a quantity * unit price then show a total price at the bottom.

    I tried this coding but it only calculated for the first row then the following rows never even got an output

    Code:
    Private Sub dgv1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv1.CellEndEdit
    Dim dgv1 As DataGridView = DirectCast(sender, DataGridView)
    
            If dgv1.Columns(e.ColumnIndex).Name = "priceu1" Then
    
                For Each r As DataGridViewRow In dgv1.Rows
                    dgv1.Rows(0).Cells("pricet1").Value = dgv1.Rows(0).Cells("priceu1").Value * dgv1.Rows(0).Cells("qty1").Value
                Next
            End If

  6. #6
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: datagridview help with basics

    Code:
        Private Sub DataGridView1_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
            Dim strColumnName As String = DataGridView1.Columns(e.ColumnIndex).Name
    
            'Check whether edited UnitPrice or Quantity Column
            If strColumnName = "UnitPrice" Or strColumnName = "Quantity" Then
                'Get the DataGridCells for UnitPrice and Quantity
                Dim dgvCellUnitPrice As DataGridViewCell = DataGridView1.Item(DataGridView1.Columns("UnitPrice").Index, e.RowIndex)
                Dim dgvCellQuantity As DataGridViewCell = DataGridView1.Item(DataGridView1.Columns("Quantity").Index, e.RowIndex)
    
                'Check whether any of both is not entered then do nothing else calculated Total Price
                If Not (String.IsNullOrEmpty(dgvCellUnitPrice.Value) Or String.IsNullOrEmpty(dgvCellQuantity.Value)) Then
                    DataGridView1.Item(DataGridView1.Columns("TotalPrice").Index, e.RowIndex).Value = dgvCellUnitPrice.Value * dgvCellQuantity.Value
                End If
            End If
        End Sub

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

Featured