Results 1 to 6 of 6

Thread: [2008] Hiding a Column in DataGrid View but Also Get Data From it

Hybrid View

  1. #1
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Hiding a Column in DataGrid View but Also Get Data From it

    If you're binding a DataTable to a DataGridView then most likely that column is irrelevant. Whether it exists or not you should, in most instances, be getting the data from the DataSource anyway. That said, if you don't want the column to be visible then just set its Visible property to False.
    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

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: [2008] Hiding a Column in DataGrid View but Also Get Data From it

    vb Code:
    1. Public Sub LoadDataGridView()
    2.         Connection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DatabaseName)
    3.         Connection.Open()
    4.         Adapter.SelectCommand = New OleDbCommand("SELECT * FROM [Students]")
    5.         Adapter.SelectCommand.Connection = Connection
    6.         DS = New DataSet()
    7.         Adapter.Fill(DS)
    8.         Connection.Close()
    9.         With frmStudents
    10.             Dim DT As DataTable = DS.Tables(0)
    11.             .dgvStudents.DataSource = DT
    12.             .dgvStudents.Columns.Item(0).Visible = False
    13.         End With
    14.     End Sub

    Here's what my current code is. As you see it selects * from students and then fills it into the DataSource.

    now heres the code that gets the selected index and selects the DataGridView and Combo Box Index

    vb Code:
    1. Public Sub SyncDGVAndCombo()
    2.         With frmStudents
    3.             Connection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DatabaseName)
    4.             Connection.Open()
    5.             Adapter.SelectCommand = New OleDbCommand("SELECT [CIndex] FROM [Students]")
    6.             Adapter.SelectCommand.Connection = Connection
    7.             DS = New DataSet()
    8.             Adapter.Fill(DS)
    9.             Connection.Close()
    10. 'RINDEX = the e.RowIndex And/Or cmb.SelectedIndex
    11.             .dgvStudents.Rows(.RIndex).Selected = True
    12.             .tscbStudents.SelectedIndex = .dgvStudents.Rows.Item(.RIndex).Index
    13.         End With
    14.     End Sub

    Now what needs to be done, or what I'm trying to accomplish, is when the user clicks on a column, it gets sorted to alphabetical order.
    When I click on A and its in Index 0 but when I click on the Name Column it in the Index of 3. BUT when I click the Row 3, to call the Sync Code, it gets the Data From Index 3 (C), instead of (A). So. How would I get the Value from CIndex which I Hide(its the Column, Match it up the Row in the Database then retrieve the Data from the Appropriate Row)

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