• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

How do I fill in a datagridview from code?

Eventually this datagridview will be filled in from a background sql query, but for now I am just experimenting with the datagridview.  When I push the button it should create the month worh of information.  When I run the program it fills in the date and then I get a error box for each column that has a combobox on it that says value assigned is an error. When I click the okay in places the first item in the row and proceds to the next combobox error.  I can fill in the entire table if I click okay enough times.
what is the value I am trying to assign to the column.  Tried item, name column etc nothing I try to assign when trying to build the row seems to work.
the first combo box has three words in it:  HERE, OUT OF OFFICE, WEEKEND.  The value would go into the sHrDet variable.

thanks  Gary

  Private Sub BTNCreateWage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNCreateWage.Click
        Module1.daysInMonth = System.DateTime.DaysInMonth(DTP1.Value.Year, DTP1.Value.Month)
        Dim ADDDATE As Date = Now
        Dim I As Integer
        Dim sDate As String
        Dim sHr, sPto, sHrDet, sNp, sPtoDet, sNpDet As Integer

        For I = 1 To Module1.daysInMonth
            sDate = I & "/" & Module1.HoldMonth & "/" & DTP1.Value.Year

            sHrDet = 1
            sHr = 8
            sNpDet = 1
            sNp = 0
            sPtoDet = 1
            sPto = 0
            DGV1.Rows.Add(New String() {sDate, sHrDet, sHr, sPtoDet, sPto, sNpDet, sNp})

        Next
    End Sub
End Class
0
javagair
Asked:
javagair
  • 6
  • 4
  • 2
1 Solution
 
NalinkumarbalajiCommented:
Bind the data from the SQL itself. Find the below sample. This is using SQL.

Imports System.Data
Imports System.Data.SqlClient

Private Sub LoadGrid()

Dim ConnectionString_Local = "Persist Security Info=False;User ID=sa;pwd=sa; Initial Catalog=DatabaseName;Data Source=ServerName"
Dim SQLConn_Local As SqlClient.SqlConnection
SQLConn_Local = New SqlConnection(ConnectionString_Local)

Dim DAGrid As SqlDataAdapter

Dim DTGrid As New DataTable
DAGrid = New SqlDataAdapter("Select sHr, sPto, sHrDet, sNp, sPtoDet, sNpDet from YourTable", SQLConn_Local)

SQLConn_Local.Open()
DAGrid.Fill(DTGrid)
SQLConn_Local.Close()
DGResultSet.Visible = True
DGResultSet.DataSource = DTGrid
DGResultSet.DataBind()

End Sub

If you are using the Code to Bind the Data instead of SQL then you have to Create the Datatable, then define the Columns, And Import the Colums Using Datarow into to Datatable and then Select the Datasource for Grid and Bind the same.

Check the below link.
http://msdn.microsoft.com/en-in/library/system.data.datarow(v=vs.71).aspx
0
 
NalinkumarbalajiCommented:
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
'the first combo box has three words in it:  HERE, OUT OF OFFICE, WEEKEND'
Your First Combo is waiting for as string or int ? Is there a value member in your First comboBox ...?
Otherwise you have to assign a string as display member only
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
javagairAuthor Commented:
First off there is no database to use yet.
I was hoping to fill the table using some kind of index.
Like I said this is just an experiment I'm doing so I understand the intricacies of the control itself.
Something like if you are in column one use the third item for that combobox in this row.  I don't want to care what words are in the collection just that there are three of them and fill in with the second one.

thanks
gary
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Try to set sHrDet="OUT OF OFFICE"
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
The way you try to add row is correct...But in this way you have to fill the cells in the format you declare...that is why i asked you if sHrDet is waiting a string or int...
if your combobox has 3 items and only that are strings ,set string to your variable.
Dim row0 As String() = {variable1, variable2, variable3, variable4, variable5}
        Me.RecordDatagridView.Rows.Add(row0)

Open in new window


John
0
 
javagairAuthor Commented:
the following code is equivalent to your last post.  The first string is accepted, the second string errors and the third string always chooses the first item in the collection regardless of what I assign here.
Since collections have a index; I still think there has to be a way of calling the index for the column and the index for the collection to get the value there.

thanks
gary

  Private Sub BTNCreateWage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNCreateWage.Click
        Module1.daysInMonth = System.DateTime.DaysInMonth(DTP1.Value.Year, DTP1.Value.Month)
        Dim ADDDATE As Date = Now
        Dim I As Integer
        Dim sDate, sNpDet, sHrDet, sPtoDet As String
        Dim sHr, sPto, sNp As Integer

        For I = 1 To Module1.daysInMonth
            sDate = I & "/" & Module1.HoldMonth & "/" & DTP1.Value.Year

            sHrDet = "WEEKEND"  'last item in combo collection
            sHr = 8                          'integer for hours
            sNpDet = "NON-PAID" 'last item in combo collection
            sNp = 0                          'integer for hours
            sPtoDet = "PTO"           'last item in this column's collecton
            sPto = 0                         'integer for hours
            DGV1.Rows.Add(New String() {sDate, sHrDet, sHr, sPtoDet, sPto, sNpDet, sNp})

        Next
    End Sub
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Are you Storing these value in a database?
if no ...There is no need to use a DatagridViewCombobox...just insert the cell value in the desired format..
otherwise you can use not the index but the Value Memeber of the corresponding DatagridViewCombobox..

Set the appearance -> display style to nothing
The non Combobox columns at the default cell style set the desire format of the specific column ...
then while you inserting the row ,try the values to have the same format .
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
I can not see anything wrong ...
you pass an integer as time...it works for me try this...
 Me.DataGridView1.Columns(1).DefaultCellStyle.Format = "N0"
        Me.DataGridView1.Columns(3).DefaultCellStyle.Format = "N0"
        Me.DataGridView1.Columns(5).DefaultCellStyle.Format = "N0"
        Dim row0 As String() = {"WEEKEND", "8", "NON-PAID", "9", "PTO", "10"}
Me.DataGridView1.Rows.Add(row0)

Open in new window

Columns(1)->sHr
Columns(3)->sNp
Columns(5)->sPto

just include the integer with " "
0
 
javagairAuthor Commented:
this code works
not exactly sure why PTO started filling in but it does, added a null answer to the top of the collection.  Now I get the entire table to fill.

gary

Private Sub BTNCreateWage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNCreateWage.Click
    If LBLName.Text <> "NAME" Then

        Module1.daysInMonth = System.DateTime.DaysInMonth(DTP1.Value.Year, DTP1.Value.Month)
        Dim ADDDATE As Date = Now
        Dim I As Integer
        Dim sHrDet, sPtoDet, MyDate As String
        Dim sHr, sPto As Integer
        Dim sDate, checkDate As Date
        Dim check As Boolean
        Dim Holiday() As Object = {"20130101", "20130121", "20130218", "20130527", "20130704", "20130902", "20131014", "20131111", "20131128", "20131225"}
        sHrDet = ""
        sPtoDet = ""
        sPto = 0
        sHr = 0
        For I = 1 To Module1.daysInMonth
            ' MyDate = I & "/" & Module1.HoldMonth & "/" & DTP1.Value.Year
            MyDate = DTP1.Value.Year & String.Format("{0:0#}", Module1.HoldMonth) & String.Format("{0:0#}", I)
            sDate = Module1.GetDateYMMDD(MyDate)
            check = False
            For j = 0 To Holiday.Length - 1
                checkDate = Module1.GetDateYMMDD(Holiday(j))
                If sDate = checkDate Then
                    check = False
                    sHrDet = "OUT OF OFFICE"
                    sPto = 8
                    sHr = 0
                    sPtoDet = "PTO"           'last item in this column's collecton
                    Exit For
                Else
                    check = True
                End If


            Next
            If check = True Then
                If DateTime.Parse(sDate).DayOfWeek = DayOfWeek.Saturday Or DateTime.Parse(sDate).DayOfWeek = DayOfWeek.Sunday Then
                    sHrDet = "WEEKEND"
                    sHr = 0
                    sPto = 0
                    sPtoDet = ""           'last item in this column's collecton
                Else
                    sHrDet = "HERE"
                    sHr = 8
                    sPto = 0
                    sPtoDet = ""           'last item in this column's collecton
                End If
            End If



            DGV1.Rows.Add(New String() {sDate, sHrDet, sHr, sPtoDet, sPto})

        Next


    Else
        MsgBox("You must Fill in User Number and Soc. Sec Number", MsgBoxStyle.Information)
        Exit Sub

    End If


End Sub
0
 
javagairAuthor Commented:
John

took all of your comments added  them together and got the datagrid to work.
accepted only one but all were useful

thanks

gary
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Glad to Help (guide) you gary ....!!!
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.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now