datagridview not displaying currency format - vb.net

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.

Related

when I choose a value from the first compbox is the value of the remaining compbox is changed

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

Add column to DataGridView (VB.NET)

I would like to add a new column in my project, but a weird thing happened...
My code
adapter.fill(datatable)
dgv.datasource = datatable
dgv.columns.add("test","testHeader")
The column index of test column should be the last index.
However, I found the column index is 0 and the original 0th index turns to 1
I am not sure what happened. Is there any factor could cause this problem?
property settings of datagridview is wrong?
Try This Code For Adding the Column
Dim col As New DataGridViewTextBoxColumn
col.DataPropertyName = "PropertyName"
col.HeaderText = "SomeText"
col.Name = "colWhateverName"
DataGridView1.Columns.Add(col)
It's simple , get the total column count first and then add the column at the end :
Dim col as New DataGridViewColumn
col.HeaderText = "abc"
dgv.Columns.Insert(dgv.ColumnCount, col)
If the above code doesn't help , try adding column to the IDataTable itself :
datatable.Columns.Add("Header text", GetType(System.String)).SetOrdinal(datatable.Columns.Count)
Your code
adapter.fill(datatable)
dgv.datasource = datatable
dgv.columns.add("test","testHeader")
seems to be working properly.
I guess you are re-filling the datagridview that's why the added column is in index 0.
try to add
dgv.DataSource = Nothing
you will notice that the added column "test","testHeader" will be in index 0.
setting again
dgv.datasource = datatable
will cause the added column to remain at index 0 while the datatable will be added to the succeeding columns.
There's a DataGridViewColumn.DisplayedIndex property to control order of visible columns.
Keep in mind that DataGridView tracks theese values and update them for other columns in your grid. Chaning some of them to 0 will update all other columns to highter values.
This works because we clone an existing column while you cannot append a column that does already exist in a table. this also has the benefit of cloning all the column settings applied to the existing columns.
Clone a existing column:
Dim Newtestcolumn As DataGridViewColumn = Existingcolumn.Clone
Adjust header text:
Newtestcolumn.HeaderText = "test"
Add column to datagridview:
DataGridView1.Columns.Add(test)
If you add columns that does not match the current drawing format you need to update the datagridview object by calling:
DataGridView1.Update()
If you want to write to a specific column or you dont know the column name
you use this to return the amount of columns and then point to column 0 for cloning fx.
Dim Index = DataGridView1.Columns.GetColumnCount(DataGridViewElementStates.None)
You specify the column index like this:
DataGridView1.Columns.Item(index)
going back to the first line we would then write
Dim Newtestcolumn As DataGridViewColumn = DataGridView1.Columns.Item(index).clone

populate combobox from datatable but change datatable selected item

question may need rewording, but here is the issue, and code I am using is posted below
some background so as to understand I have a datatable bound to a datagridview with a column name column names in the code I will only show a portion of column names to save space, this section works but its needed to show issues
DataGridView3.DataSource = fieldDT
DataGridView3.AutoGenerateColumns = True
fieldDT.Columns.Add("Column Name", System.Type.GetType("System.String"))
DataGridView3.Columns(0).Width = 123
fieldDT.Columns.Add("Field", System.Type.GetType("System.String"))
DataGridView3.Columns(1).Width = 86
'columns 2 through 12 are here
fieldDT.Columns.Add("Format", System.Type.GetType("System.String"))
DataGridView3.Columns(13).Width = 45
fieldDT.Columns.Add(" ", System.Type.GetType("System.String"))
DataGridView3.Columns(14).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
I also have a combobox that I want populated with the column name in the above datatable code to bind shown next
ComboBox10.DataSource = fieldDT
ComboBox10.DisplayMember = "Column Name"
ComboBox10.ValueMember = "Column Name"
the combobox populates correctly with all the columnnames in the datatable
now is where the issue that I need a way to fix arises,
if I select a row in the datagridview it is changing the displayed value in the combobox, and if I select a different value in the combobox it changes the selected row in the datagridview my feeling this is by default design but hoping it can be changed
although the combobox needs that column list and needs to be upto date if someone adds another row to the datagridview I don't want them to bind in the way they change each other.
the combo box is used for another purpose that it needs to select one of the available column names
I made the suggested change and now works as intended
thanks
Dim DV_fields As DataView = fieldDT.DefaultView
ComboBox10.DataSource = DV_fields
ComboBox10.DisplayMember = "Column Name"
ComboBox10.ValueMember = "Column Name"
ComboBox10.Text = ""

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.

Combobox keeping old index value

I have a combobox that is bound to a DB and works great.
However, when the user adds a value to the table to which the combobox is bound, the combobox keeps the old value and adds the current items in the table.
In this case the table originally held just one record ("2012"). The user added 2013 so the table now shows two records ("2012" and "2013"), but the combobox shows three records ("2012", "2012", and "2013"). UNTIL I exit the app and restart, in which case it correctly reflects the only two records in the table ("2012" and "2013").
I have tried cboYear.Datasource = Nothing, cboYear.items.clear, cboYear.DataBindings.clear and nothing works.
Here is the code:
Try
Dim asql As String = ("SELECT * FROM YearsAvailable ORDER BY CurrentYear")
Dim da As New OleDbDataAdapter(asql, con)
da.Fill(ds)
cboYear.ValueMember = "CurrentYear"
cboYear.DataSource = ds.Tables(0)
cboYear.SelectedIndex = 0
CurrentYear = cboYear.Text
Me.Text = "MSD of Perry Township Compensation Model: " & CurrentYear
Catch ex As Exception
MsgBox("ERROR filling the YEAR control: " & ex.Message.ToString)
End Try
You need to clear ds before Fill it again:
ds.Clear();
da.Fill(ds);
Check this out: DataAdapter.Fill
You can use the Fill method multiple times on the same DataTable. If a primary key exists, incoming rows are merged with matching rows that already exist. If no primary key exists, incoming rows are appended to the DataTable.