Solved

How do I fill in a datagridview from code?

Posted on 2013-02-05
12
247 Views
Last Modified: 2013-02-09
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
Comment
Question by:javagair
  • 6
  • 4
  • 2
12 Comments
 
LVL 3

Expert Comment

by:Nalinkumarbalaji
ID: 38855259
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
 
LVL 3

Expert Comment

by:Nalinkumarbalaji
ID: 38855354
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 38855636
'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
 

Author Comment

by:javagair
ID: 38856945
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 38856985
Try to set sHrDet="OUT OF OFFICE"
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 38857106
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:javagair
ID: 38863789
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 38863998
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
 
LVL 18

Accepted Solution

by:
John (Yiannis) Toutountzoglou earned 500 total points
ID: 38864488
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
 

Author Comment

by:javagair
ID: 38871393
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
 

Author Closing Comment

by:javagair
ID: 38871399
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 38871407
Glad to Help (guide) you gary ....!!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now