when I choose a value from the first compbox is the value of the remaining compbox is changed - vb.net

I have 3 compobboxes I have created an import code from a SQL table
The problem when I choose a value from the first compbox
the values of other compbox is changed to be like my choice
I made a separate code for each compbox
But I find it impractical because my project has 90 compoboxes
It needs time to run
Is there a more practical solution?
this is my code...
Dim com As New SqlCommand("select Distinct Name1 from TB_dr", Con)
Dim RD As SqlDataReader = com.ExecuteReader
Dim DT As DataTable = New DataTable
DT.Load(RD)
ComboBox1.DisplayMember = "Name1"
ComboBox1.DataSource = DT
ComboBox2.DisplayMember = "Name1"
ComboBox2.DataSource = DT
ComboBox3.DisplayMember = "Name1"
ComboBox3.DataSource = DT

Only populate a combobox on its dropdown event.
That will make your app faster cause your client may not use them all and if he uses one combobox he would populate a small amount of data only.
And no data is populated on load.
As i look at what you are trying to do which is same data for all comboboxes, you can simply put all your comboboxes in a group, and go through your group to populate them all at once.
like for each cb in that group, cb.datasource = dt.
call the datatable once

Related

DataTable And Two Row Sorting

So the boss comes to me and says "I want the value of each agent and the project on one line and the average of all the other agents on the next line so I can easily see if they are above or below average."
the table looks like this:
dt.Columns.Add("AGENT", GetType(String))
dt.Columns.Add("PROJECT", GetType(String))
dt.Columns.Add("Sales", GetType(Integer))
dt.Columns.Add("Declines", GetType(Integer))
dt.Columns.Add("Margin", GetType(Integer))
Ok its all good. One row in the datatable is the agent and project. The next row is the average of all the other agents and project like so:
row 1:
John Smith,
ProjectName,
(other column values)
row 2:
John Smith,
ProjectName & " AVERAGE/TOTAL",
(other column values)
The project name is removed in the SSRS report on the AVERAGE/TOTAL line because of space constraints on the piece of paper it is printed on.
I do the sorting by our standard way of sorting a datatable.
Dim dataView As New DataView(dt1)
dataView.Sort = "AGENT,PROJECT"
dt1 = dataView.ToTable
Return dt1
But now the boss has a new requirement later on. He wants to be able to sort by other columns in the table but keep the two rows the (agent/project and the agent/project AVERAGE/TOTAL) together. So in essence he wants to be able to sort not by one row but the two rows together but the sort value could be "AGENT,Margin". Obviously to keep the two rows together I have to find a way to sort the Project value too.
So I am stumped and would appreciate any thoughts you might have. C# ideas are welcome as well.LINQ is fine but it is going to have to become a datatable.
so you create two tables. One with the row values one one with the average values. Loop through the row values and then do another loop inside that loop to match project names. Its a hack but it worked.
If SortValue = "Default" Then
dt1.Merge(dt)
Dim dataView As New DataView(dt1)
dataView.Sort = "AGENT,PROJECT"
dt1 = dataView.ToTable
Else
Dim dataView As New DataView(dt)
dataView.Sort = SortValue
dt = dataView.ToTable
Dim dtCopy As New DataTable
dtCopy = dt.Clone
For Each row As DataRow In dt.Rows
dtCopy.ImportRow(row)
For i = 0 To dt1.Rows.Count - 1
If dt1.Rows(i).Item("PROJECT").ToString.Replace(" AVERAGE/TOTAL", "") = row.Item("PROJECT") And dt1.Rows(i).Item("AGENT") = row.Item("AGENT") Then
dtCopy.Rows.Add(dt1.Rows(i).Item("AGENT"), dt1.Rows(i).Item("PROJECT"), dt1.Rows(i).Item("SALES"), dt1.Rows(i).Item("Declines"), dt1.Rows(i).Item("Margin"))
End If
Next
Next
dt1 = dtCopy
End If

How to filter a datagridview when using data from datatable? VB.net

I have created a textbox and want it to search through a database of customers by name. Most of the questions are using an external dataset but this is just using a table created in the program using a csv file.
You could take advantage from BindingSource, to be used as DataSource of your DataGridView. That way, acting on the BindingSource Filter property, you could set any type of filters, based on you columns name.
Please check the following snippet:
Dim dt As New DataTable("Sample")
dt.Columns.Add("Id")
dt.Columns.Add("TimeStamp")
For i As Int32 = 0 To 9999
dt.Rows.Add(New Object() {i, DateTime.Now})
Next
Dim bs As New BindingSource
bs.DataSource = dt
bs.Filter = "Id > 10 AND Id < 20"
DataGridView1.DataSource = bs
As you can see, i've defined a DataTable with two columns, namely "Id" and "TimeStamp". Then, with a simple loop i've populated my DataTable with some random records, for Id = 0 to Id = 9999.
After that, we declare a BindingSource, specifying its DataSource is our DataTable. On the Bindinf Source, we could set any filter, using the Filter property, the columns names, and the common logical operators.
In my example, i've requested the filter to be on the only Id column, to visualize those record whose Id is between 11 and 19.
Then, we could use the BindingSource as our DataGridView DataSource.
And note that filters doesn't need to be apply before assigning the DataGridView DataSource: in fact, after the binding, each filter application will reflect immediately on the visualized rows.
Hope this helps

Hide column in datagrid (Framework 2.0)

I'm building an app for Win CE 5.0 in VB.net framwork 2.0. I have a DataGrid (note: no DataGridView! :-)) with a DataTable as source.
In this case I want the last column to be hidden. I still want this value in this column if i call for it, but I want it invisible for the user. How can i do that..?
Edit: This is what i have
Dim Table As DataTable = New DataTable("myTable")
' Filling my table ... '
MyDataGrid.DataSource = Table
I have tried this before setting the datasource, but no luck (i try with index 0 just to test)
Dim Table As DataTable = New DataTable("myTable")
' Filling my table ... '
Table.Columns(0).ColumnMapping = MappingType.Hidden
MyDataGrid.DataSource = Table
But no column is hidden.

datagridview not displaying currency format

Firstly I used the following to populate the DataGridView:
dta = New OleDbDataAdapter("Select * From [" & ActName & "$B6:E" & LastEntryRow & "]", cn)
dts = New DataSet
dta.Fill(dts, "Detailtable")
DataGridView1.DataSource = dts
DataGridView1.DataMember = "Detailtable"
I then formatted the DataGridView which included the following code:
Dim currencyCellStyle As New DataGridViewCellStyle
currencyCellStyle.Format = "C2"
With Me.DataGridView
.Columns(1).DefaultCellStyle = currencyCellStyle
.Columns(2).DefaultCellStyle = currencyCellStyle
End with
This worked well. Columns displayed their values as $1234.00.
When new values were added to the columns they immediately displayed as $1234.00. (working so far)
If a column did not have any values when the dataset was made, no values showed in the datagridview for that column. (no problem so far)
However, all new values added to the blank column display as 1234.00. Not $1234.00.
I have tried refreshing the DataGridView
I have re-formatted the DataGridView after the change to the cell.
It still displays as 1234.00.
If I save the changes, recreate the DataSet and repopulate the DataGridView all is OK.
I need the DataGridView to reflect the correct format ($1234.00) when new values are added directly to the column?????
If you use this line
DataGridView1.Columns(1).DefaultCellStyle.Format = "C2"
it must be work but if your database column format is not Decimal (but TEXT), this type of format it'll never works.

Fastest way of filling a combobox from a datatable in VB.Net

The data table in the following code is filled with 7500-+ records. This all loads quickly from the server. The problem is that it takes a while to loop through the data rows to add them to the combo box. Is there any alternative way of setting the data source of the combo box or a way of speeding this process up?
Dim dtColours As New DataTable
Dim daColours As New SqlDataAdapter
Dim i As Integer
ConnectToSQL()
daColours = New SqlDataAdapter("SELECT DISTINCT Rtrim(UPPER(Colour)) As Colour FROM invStockColour WHERE InUse = 1 ORDER BY Colour", dbSQL)
daColours.Fill(dtColours)
For i = 0 To dtColours.Rows.Count - 1
cboColours.Items.Add(dtColours.Rows(i).Item(0).ToString)
Next
dbSQL.Close()
The fasted way would be to use the AddRange method instead of using Add, something like:
Dim items = dtColours.AsEnumerable().Select(Function(d) DirectCast(d(0).ToString(), Object)).ToArray()
cboColours.Items.AddRange(items)
I did a simple check and using AddRange is ~3x faster than using Add.
Of course allocating an array and filling it with a For loop would probably some milliseconds faster than using Linq.
Dim daColours As New SqlDataAdapter("SELECT DISTINCT Rtrim(UPPER(Colour)) As Colour FROM invStockColour WHERE InUse = 1 ORDER BY Colour", dbSQL)
Dim dtColours As New DataTable
daColours.Fill(dtColours)
cboColours.DataSource=dtColours
cboColours.DisplayMember="Colour"
You could also bind your ComboBox DataSource property to the DataTable. This has the added advantage of binding other column data (such as key values you might not want the user to see).
You should be able to return a DataTable object from your SQLAdapter.
cboColours.DataSource = dtColours
cboColours.DisplayMember = dtColours.Columns("Colour").ToString
cboColours.ValueMember = dtColours.Columns("Colour_id").ToString
Try this:
cboColours.DataSource = dtColours 'For Windows Forms or
cboColours.ItemsSource = dtColours 'For WPF