Sorting of Data grid view which is bound table - vb.net

I want my first column sort of data grid view when form load. but i have no code. Below i attach my loding code.
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'EmployeeTBDataSet.Table1' table. You can move, or remove it, as needed.
Me.Table1TableAdapter.Fill(Me.EmployeeTBDataSet.Table1)
End Sub

You have presumably dragged a table from the Data Sources window onto your form to create that DataGridView and the code that populates the DataTable it is bound to. That will also have created a BindingSource and that is where you perform most actions related to the bound data, including sorting. After that call to Fill, set the Sort property of that BindingSource to the name of the column you want to sort by, e.g.
Me.Table1BindingSource.Sort = "Table1ID"
That will sort in ascending order implicitly. You can also sort in ascending or descending order explicitly, e.g.
Me.Table1BindingSource.Sort = "Table1ID DESC"

Related

Active filtering datagridview with texbox when datagridview has a checkbox column

Ιn VB.NET i have a datagridview which has as datasource a datatable.
In order to achieve active filtering, i have a textbox with the follwing code on TextChanged event.
Private Sub KryptonTextBox1_TextChanged(sender As Object, e As EventArgs) Handles KryptonTextBox1.TextChanged, KryptonTextBox5.TextChanged
DirectCast(KryptonDataGridView1.DataSource, DataTable).DefaultView.RowFilter = String.Format("userid like '%{0}%' or name like '%{0}%'", KryptonTextBox1.Text)
End Sub
When i decided to use a checkbox type column , i saw that when a user has made some checks and then he filters, the checks are lost beacuse the datatable is reloaded ...
Any ideas ? (except storing the checked values in the database because i have no rights to do this in the specific case)

How to Fill ComboBox with DataTable Column Names in VB.NET?

I need to fill a combobox on a form with the column names from an existing data table. I don't want to manually enter the column names in case they change in the future. I also want to keep my code tidy, so I don't want a standard loop. How can I fill a combobox with the column names most efficiently?
Thanks to examples from here, here, and here, I was able to piece together the following LINQ query and set it directly to the data source on the combobox when the form loads.
Private Sub Form_Load(sender As Object, e As EventArgs) Handles Me.Load
comboBox.DataSource = (From dc In dataTable.Columns
Select dc.ColumnName).ToArray()
End Sub

Dynamically added DataGridView

I have a datagridview that i use to filter another datagridview containing data.The filterGrid is added dynamically.
The screen shot shows that i have a MODAL form in order to look up for a record in the database.When I click where the arrow is ,this modal form appears with 2 datagridview.Everyhting is working fine.BUT when i want to look up fagain or a record from another window the filter datagridview is added with columns of the previous form.I think there's something that i must dispose.
In the event leave of the form i'm disposing the header grid to clean it in order to have a new filter grid added in the nex search that i will do accoriding to the datagridview that contains data but it doesn't work
Private Sub Frm_listeRecherche_Leave(sender As Object, e As System.EventArgs) Handles Me.Leave
Me.Controls.Remove(HeaderGrid)
HeaderGrid.Dispose()
End Sub
Public Sub New()
InitializeComponent()
End Sub

ComboBox and Datagridview VB.Net

I'm working with a combox and datagridview. What I am trying to accomplish (this is homework) is show a combobox where the user can pick an employee ID. When the user chooses that ID the datagrid view displays sales for that employee's ID.
I am coming up with all kinds of issues so I would like to restart and walk through the process. My form is fairly simple. Here is what I have done so far, please bear with me.
First I set my employeeID to a combobox (from the datasource on the datasources pane) and dragged and dropped to my form. I clicked and set it to use databound items and set it to the same datasource as my datagrid view. Then I chose EmployeeID for my display member.
The datagrid view will load just fine and brings up every order. The employeeID combobox will load several ID's and there are duplicates because an ID can be associated with seperate orders. If I pick an ID it isolates the order that ID is associated with. This is where I am stuck.
I don't have much code to show because of the way this was programmed (drag and drop) but I can show you what came up:
Public Class EmployeeOrdersForm
Private Sub EmployeeOrdersForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'NORTHWNDDataSet.Employees' table. You can move, or remove it, as needed.
Me.EmployeesTableAdapter.Fill(Me.NORTHWNDDataSet.Employees)
'TODO: This line of code loads data into the 'NORTHWNDDataSet.Employees' table. You can move, or remove it, as needed.
Me.EmployeesTableAdapter.Fill(Me.NORTHWNDDataSet.Employees)
'TODO: This line of code loads data into the 'NORTHWNDDataSet.Employees' table. You can move, or remove it, as needed.
Me.EmployeesTableAdapter.Fill(Me.NORTHWNDDataSet.Employees)
'TODO: This line of code loads data into the 'NORTHWNDDataSet.Order_Details' table. You can move, or remove it, as needed.
Me.OrdersTableAdapter.Fill(Me.NORTHWNDDataSet.Orders)
End Sub
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
Private Sub EmployeeIDComboBoxToolStripLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
End Class
Can someone point me in the right direction? Is it possible to sort the combobox so that way I can have it only display the employeeID once and then when clicked sort through the orders?
Thank you,
J
THis isnt the official answer but I was able to find a walk through in the book. It still has errors but I guess it's better than nothing.

Datagridview not saving changes to DB when Navigator save changes clicked

This is a VB.NET, Winforms App, Using EF. On my form I have a datagridview, a databinding source, and a bindingNavigator... I can edit the cells of the datagridview but when I click save changes the value is only saved until I reload the form.?.? Looking at the database table directly I can see the value never actually changes.. Below is the sub that handles the click..
Private Sub UnitBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UnitBindingNavigatorSaveItem.Click
UnitDataGridView.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange)
db.SaveChanges()
End Sub
From what I have read this seems like all I need to have but obviously its wrong somehow..
I made a work around for it for the time being... I simply use the CellEndEdit event and get the row info from that. Next I get the value of the column that holds the id and update the database from there. Seems like a long way around but is the only way I can get it to write any data at the moment...