|
-
Oct 8th, 2008, 08:11 PM
#1
Thread Starter
Hyperactive Member
[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.
-
Oct 8th, 2008, 08:20 PM
#2
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.
-
Oct 8th, 2008, 08:38 PM
#3
Thread Starter
Hyperactive Member
Re: [2008] Hiding a Column in DataGrid View but Also Get Data From it
vb Code:
Public Sub LoadDataGridView()
Connection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DatabaseName)
Connection.Open()
Adapter.SelectCommand = New OleDbCommand("SELECT * FROM [Students]")
Adapter.SelectCommand.Connection = Connection
DS = New DataSet()
Adapter.Fill(DS)
Connection.Close()
With frmStudents
Dim DT As DataTable = DS.Tables(0)
.dgvStudents.DataSource = DT
.dgvStudents.Columns.Item(0).Visible = False
End With
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:
Public Sub SyncDGVAndCombo()
With frmStudents
Connection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DatabaseName)
Connection.Open()
Adapter.SelectCommand = New OleDbCommand("SELECT [CIndex] FROM [Students]")
Adapter.SelectCommand.Connection = Connection
DS = New DataSet()
Adapter.Fill(DS)
Connection.Close()
'RINDEX = the e.RowIndex And/Or cmb.SelectedIndex
.dgvStudents.Rows(.RIndex).Selected = True
.tscbStudents.SelectedIndex = .dgvStudents.Rows.Item(.RIndex).Index
End With
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)
-
Oct 8th, 2008, 08:46 PM
#4
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.
-
Oct 8th, 2008, 08:47 PM
#5
Thread Starter
Hyperactive Member
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.
-
Oct 8th, 2008, 08:57 PM
#6
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|