VS 2015 Problem With Adding Data to a DataGridView - (VS 2015)-VBForums
Results 1 to 2 of 2

Thread: Problem With Adding Data to a DataGridView - (VS 2015)

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2016
    Posts
    13

    Post Problem With Adding Data to a DataGridView - (VS 2015)

    Hey guys,

    I am having a problem with my program; I am trying to get my program to allow a user to be able to insert data into a specific cell in my data grid view, but it seems to be unable to do it accurately.

    Below is the code that I am using for the button which adds the data to the data grid. It collects data from combo boxes and time pickers on the form and is then supposed to use it to determine which cell to insert the data into as well as formulate the data that is supposed to be inserted.

    It is able to correctly formulate the data to be inserted, however it doesn't select the right cell.

    Code:
    Dim x = AssignStaffMemberCB.SelectedIndex
    Dim y = AssignDayCB.SelectedIndex
    Dim z = AssignTimeDTP.Value
    Dim u = AssignEndTimeDTP.Value
    
    Timetable_DataGrid.Rows(x).Cells(y).Value = z & (" - ") & u
    I think the problem is that it is selecting the index of the selected item in each of the combo boxes with regards to it's position in the combo box itself and is using that to determine the position of where the data should be inserted, which isn't what I want it to do because it is inserting the data into the wrong cells.

    I want the program to be able to use the actual string itself that the user has selected instead of it's index to determine where the formulated data should go in the data grid by comparison, however I am not sure on how to program this.

    I have tried to change the variables to what I have below but that just gives me an error because it isn't valid:


    Code:
    Dim x = AssignStaffMemberCB.SelectedItem
    Dim y = AssignDayCB.SelectedItem
    Dim z = AssignTimeDTP.Value
    Dim u = AssignEndTimeDTP.Value
    
    Timetable_DataGrid.Rows(x).Cells(y).Value = z & (" - ") & u
    Just for clarification the staff member combo box is simply storing the first names of staff members and the assign day combo box is storing the days of the week, nothing else.

    I would really appreciate it if you guys could help me out with this

    Thanks and have a good day.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,324

    Re: Problem With Adding Data to a DataGridView - (VS 2015)

    If you want the items in the ComboBoxes to be used as Integers then you should add Integer values to the ComboBoxes in the first place. That can be done by data-binding, e.g.
    vb.net Code:
    1. Dim rowIndexes = Enumerable.Range(0, grid.Rows.Count).ToArray()
    2.  
    3. rowComboBox.DataSource = rowIndexes
    or directly, e.g.
    vb.net Code:
    1. Dim rowIndexes = Enumerable.Range(0, grid.Rows.Count).ToArray()
    2.  
    3. rowComboBox.Items.AddRange(rowIndexes)
    One point to note is that the first option will select the first item by default while the second will not.

    Now that the items are Integers, you can use them as Integers:
    vb.net Code:
    1. Dim rowIndex = CInt(rowComboBox.SelectedItem)
    2. Dim columnIndex = CInt(columnComboBox.SelectedItem)
    3.  
    4. grid(columnIndex, rowIndex).Value = someValue
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.