Urgent--Set default value for the column in the data grid view

I have a data grid view populated with values from the database in my windows form.
I have added a new column in the data grid view, where I can enter the number of labels I want to print per item.
how can I set the default value of the column added by me(not from the database) to zero.
Private Sub GetDataForNPrnted()
        Dim numLbl As String
        Dim dt As New DataTable
        Dim DS As New DataSet
        'Select the data from the table and sets it to the dataset
        sSQL = "SELECT ID , LN_NBR, ITEM_ID" & _
              "FROM RECV_PRINT WHERE CODE = 'N' ORDER BY LINE_NO"
        If GetDataSet(DS, sSQL) Then
            With DS.Tables("Table")
                count = .Rows.Count
                If .Rows.Count > 0 Then
                    dt = DS.Tables("Table")
                    RDGVW.DataSource = dt
                    With RDGVW.Columns
                        .Add(numLbl, "NUM. OF LABELS")
                    End With
                End If
            End With
        End If
    End Sub

Open in new window

code.doc
LVL 1
SGUDAPATAsked:
Who is Participating?
 
kdwoodConnect With a Mentor Commented:
Also, make sure you put quotes around numLbl here:

For Each row As DataGridViewRow In RDGVW.Rows
            row.Cells("numLbl").Value = 0
Next
 

0
 
kdwoodCommented:
There may be a cleaner way to do this, but here is one way that I know of:

Regards,

Keith
 


Private Sub GetDataForNPrnted()
        Dim numLbl As String
        Dim dt As New DataTable
        Dim DS As New DataSet
        'Select the data from the table and sets it to the dataset
        sSQL = "SELECT ID , LN_NBR, ITEM_ID" & _
              "FROM RECV_PRINT WHERE CODE = 'N' ORDER BY LINE_NO"
        If GetDataSet(DS, sSQL) Then
            With DS.Tables("Table")
                count = .Rows.Count
                If .Rows.Count > 0 Then
                    dt = DS.Tables("Table")
                    RDGVW.DataSource = dt
                    With RDGVW.Columns
                        .Add(numLbl, "NUM. OF LABELS")
                    End With
                    
                    ' New Code
 
                      For Each row As DataGridViewRow In dgTest.Rows
                                row.Cells(numLbl).Value = 0
                      Next
 
                End If
            End With
        End If
    End Sub

Open in new window

0
 
kdwoodCommented:
oops, I have a type, try this instead:


Keith
 
 
 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
 Private Sub GetDataForNPrnted()
        Dim numLbl As String
        Dim dt As New DataTable
        Dim DS As New DataSet
        'Select the data from the table and sets it to the dataset
        sSQL = "SELECT ID , LN_NBR, ITEM_ID" & _
              "FROM RECV_PRINT WHERE CODE = 'N' ORDER BY LINE_NO"
        If GetDataSet(DS, sSQL) Then
            With DS.Tables("Table")
                count = .Rows.Count
                If .Rows.Count > 0 Then
                    dt = DS.Tables("Table")
                    RDGVW.DataSource = dt
                    With RDGVW.Columns
                        .Add(numLbl, "NUM. OF LABELS")
                    End With
                    
                    ' New Code
 
                      For Each row As DataGridViewRow In RDGVW.Rows
                           row.Cells(numLbl).Value = 0
                      Next
 
                 End If
            End With
        End If
    End Sub
 

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
SGUDAPATAuthor Commented:
HI Keith,

when I am added
For Each row As DataGridViewRow In RDGVW.Rows
                           row.Cells(numLbl).Value = 0
                      Next
 
in my code it is giving an "ArgumentNullException was unhandled-Value cannot be null.Parameter name:columnName" exception.
0
 
kdwoodCommented:
Greetings,

I looks like it's not finding your column name.  One thing I noticed in your code is that you are never stuffing a value into your "numLbl" variable.  You are declaring it as a string, but I don't see where you actually store anything to it.  So instead, try this:


Private Sub GetDataForNPrnted()
        Dim dt As New DataTable
        Dim DS As New DataSet
        'Select the data from the table and sets it to the dataset
        sSQL = "SELECT ID , LN_NBR, ITEM_ID" & _
              "FROM RECV_PRINT WHERE CODE = 'N' ORDER BY LINE_NO"
        If GetDataSet(DS, sSQL) Then
            With DS.Tables("Table")
                count = .Rows.Count
                If .Rows.Count > 0 Then
                    dt = DS.Tables("Table")
                    RDGVW.DataSource = dt
                    
                     With RDGVW.Columns
                        .Add("numLbl", "NUM. OF LABELS")  <--- changed here
                    End With
                    
                    ' New Code
 
                      For Each row As DataGridViewRow In RDGVW.Rows
                           row.Cells("numLbl").Value = 0
                      Next
 
                 End If
            End With
        End If
    End Sub
  
Open in New Window Select All 

Open in new window

0
 
SGUDAPATAuthor Commented:
Thanks a bunch for the solution.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.