Results 1 to 6 of 6

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

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

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

    Alright. I have a Datagridview, and I have a about 5 Columns in it. I only need 4 of them to be visible but the one hidden one "CIndex" needs to be their so I can call from it and get the Proper index so my OleDb Code will work. How would I go about Hiding the Column CIndex (Very First Column) but then Grab Value from it? I'm new to Datagridview but decent in OleDb.

  2. #2
    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

  3. #3

    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)

  4. #4
    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

    This can be done without a BindingSource but I suggest that you use one regardless. Instead of binding your DataTable to your grid, bind the DataTable to a BindingSource and the BindingSource to the grid. Now, the rows in the grid will correspond index for index to the items in the BindingSource. You can simply index the BindingSource to get a DataRowView object. That row will contain all the same columns as the DataTable regardless of what's in the grid.

    Not only that, if you only want the data from the currently selected row you can get that DataRowView from the Current property of the BindingSource.

    Like I said, the grid is basically irrelevant. It's for the user. In code you should interact with the data source.
    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

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

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

    Any Good Tutorials that show how to work with the DataSource? I'm new to this all this Data Stuff expect for Oledb stuff which you've helped with me before.

  6. #6
    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

    In this case the data source is the BindingSource. I'd suggest reading the MSDN documentation for the class for a start. Note that, if you bind a DataTable to the BindingSource, each item is a DataRowView. If you bind a different type of list then the items will be some other type.
    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
  •  



Click Here to Expand Forum to Full Width