VB.net checking if active control is specific DGV - vb.net

I need to check if my Current DGV is a specific datagridview in my program named DgvDiameterData, so how do I preform this check?
I got this code which sets the active current DGV which works but the check doesnt and no idea how to get it to work
Dim CurrentDGV As DataGridView = ActiveControl
If CurrentDGV = DgvDiameterData Then 'the part after the = doesnt work
End If

Have a look at the Is Operator.
From the link:
The Is operator determines if two object references refer to the same object
So you need:
If CurrentDGV Is DgvDiameterData Then
This is less foolproof but you could also check the Name property.
If CurrentDGV.Name = DgvDiameterData.Name Then

Related

How do I get the updated value of a textbox in VBA Access in a custom Sub?

This is for VBA Access 2003
I've got a textbox I want to use as a filter for a list box rowsource command. I also have a checkbox which adds another filter for the same rowsource command. I've only programmed in C# and I'm trying to write a single Sub which will simply set the RowSource regardless of if my textbox filter is changed or if my checkbox filter is changed. However, my textbox is giving me problems.
If my checkbox filter changes and I run my method the textbox.Text throws an error saying that it must have focus - Text is null. If I do a null check on that property it throws an error saying the control must have focus.
I've used the .Value property, but for whatever reason it doesn't update to the newer values.
My current attempt:
If Me.txtClientFilter.Text = Null Then ' Error 2185
filter = Me.txtClientFilter.Value
Else
filter = Me.txtClientFilter.Text
End If
Should I
Manually add focus then remove it everytime I want to check a
control?
Duplicate my code in each control's event Sub?
Manually set
the .Value property when the change happens?
I fixed my problem with some code which I'll show below. I don't know what was happening behind the scenes, but the .Value was not getting updated with the .Text value. I decided to set it explicitly, which then caused the entire text box value to be selected.
I ended up with the following code which explicitly sets the .Value of the control and also reset the cursor to the end of the text in the control. Thanks to some guy named Brent Spalding here for the cursor code.
Private Sub txtClientFilter_Change()
Me.txtClientFilter.Value = Me.txtClientFilter.Text
ProcessFilter
txtClientFilter.SelStart = Len(Me.txtClientFilter.Text)
txtClientFilter.SelLength = 0
End Sub

Checkbox in a DataGridView column is not getting checked sometimes?

I use this code to check all the check-boxes in a data grid view in VB.NET (2010 - .NET Framework 4.0),
Dim i As Integer
For i = 0 To (DataGridView.Rows.Count - 1)
DataGridView.Rows(i).Cells(0).Value = True
Next
Sometimes when one of the row is selected (not checked but just the entire row is selected) while this code is running, it checks all other check-boxes in the column except the selected one. What is wrong here?
I also try adding this code,
DataGridView.SelectedCells(0).Cells(0).Value = True
Did not work.
Like I said in the comments, the issue is that the box is checked in the datasource, but the GUI hasn't refreshed that cell to reflect it. If you click off of it, the cell should change/refresh to your new value. After you have made your data changes, at the very end you should deselect cells/rows. You can do it by:
Datagridview.ClearSelection()
or
Datagridview.CurrentCell = Nothing 'deselects
You get the idea. This is what has worked for me in the past.

Extend the properties of datagridview and its cell

I am working on the datagridview now days. And I have to assign some custom properties to the datagridview which I am able to do. problem comes when I want to extend the properties of the cell. for example I already have my custom textbox control which user can set the behaviour like if its numeric or alphanumeric, allow negative, allow decimals etc etc. which works fine. Now I want to include that textbox control in my extended grid. So user can set all those properties while adding columns.
First of all is it possible? If yes then any tutorial or help please.
thanks in advance.
I'm not fully certain so you may want to keep looking, but as far as I know you have to manually set each property of the text box to what you want. There isn't a copy all cell style settings to textbox call you can make.
Public Sub funwithDGVs()
Dim DataGridView1 As New DataGridView
Dim DataGridViewCell1 As DataGridViewCell
DataGridViewCell1.ForeColor = Color.Aquamarine
DataGridView1.Item(0, 1).Style.ForeColor = DataGridViewCell1.ForeColor
TextBox1.ForeColor = DataGridViewCell1.ForeColor
End Sub

Searching a bound Combo Box for User Input

Alright Gents,
Im trying to search a vb.net combo box for an item. The combo box is already bound to dataset. The display member is set to display just a single column of the selected record. I had it set initially so the objects displayed in the combo were a customized class. In this class I specified all the properties I wanted to keep track of and that seemed to work well. However now that I am using the combo box in its bound state it is much more difficult to manipulate the data.
Mission:
To have the user type a number, if the number is contained in the ComboBox, the combo box should then move to that record so that all the other items bound to that control will update as well.
Research:
I have looked into the System.Windows.Forms.BingingManagerBase class and that seems to have the information I need. I just can't figure out the bridge between that and what I'm trying to do. I want to throw something together so I tried to simply do a SQL search against the dataset for the text in the combobox. Unfortunately that requires late binding and my targeted version of the .Net compact framework does not support that.
Here is an example of the late binding I was attempting. (Im working with VB.net 2005, Compact Framework 3.5 I believe:
For i as integer = 0 to combobox.items.count - 1
dim Dsr as Dataset.Row
dim dv as dataview
Dsr = DirectCase(Dv.row, Dataset.Row)
If Dsr(i).DesiredColumn = DesiredRow.Desiredcolumn then
'Do such and such code
End If
Next
I want to be able to search the dataset for a specific record matching a query. After I find that row matching the query I want to be able to move my combo box to the row found in the SQL query. The main problem seems to be that the Combo works in Datarowviews and my datasets are mostly cast to rows pertaining to the DS.
Anyone have some insight on this it would be much appreciated.
Thanks again!
If you know the item that should be set as selected in the combobox you can just set the ComboBox.SelectedItem Property
If you actually do really need to loop through all the items bound to the combobox then once you reach the correct one you can set the ComboBox.SelectedIndex Property.
For i As Integer = 0 To ComboBox.Items.Count - 1
Dim drv As System.Data.DataRowView = Nothing
Dim desiredColumn As String = String.Empty
drv = DirectCast(ComboBox.Items.Item(i), DataRowView)
desiredColumn = Convert.ToString(drv("Tag"))
Debug.WriteLine(desiredColumn)
Next
This seems to find the column value for every record in the combo box allowing me to find the correct index of the text I am searching for. Like I said though, if I could find a way to search through the list of items in the combobox without having to address each one, I would be grateful.

How to set default values for data bound controls for addition in VB.NET

I have a vb.net 2010 form with 22 data bound controls from two tables held in a dataset which is navigated by a bindingnavigator. This successfully adds deletes and updates. However what I need is when adding a new record I need some of the fields to be pre filled out. More specifically I have points balance fields etc which could be any value but will normally be 0 on a new customer so I want to initialise them to 0 when adding new records.
I located an AddingNew event on my datasource but this is called before the new item is added and thus all my initialisation is lost.
any help on this would be appreciated.
kind regards
Feldoh
Since you are using DataTables, you can manually set the DefaultValue property of the DataColumn:
Dim dt As New DataTable
Dim dc As New DataColumn("test", GetType(String))
dc.DefaultValue = "hello"
dt.Columns.Add(dc)
dt.Rows.Add()
Debug.WriteLine(dt.Rows(0)("test").ToString)
Result: hello
Answer including multiple linked tables solution:
Like LarsTech said using
dataset.table.Columns.Item("someField").DefaultValue = someValue
works nicely if you have a standard default but it also works for generated defaults, if you reset this default on saving to the next value you want if a new addition is made. However if you are using a dataset as your data source where a child table record cannot feasibly be generated (like in my case where an account id will be generated only on saving using a database trigger to link all different kinds of accounts) you can still set defaults for the other fields, the ones from the sibling (or non-existent parent) table but
dataset.siblingTable.Columns.Item("someField").DefaultValue = someValue
WILL NOT WORK as the binding navigator only generates the first sibling and the parent is not generated until the trigger. However on the BindingSource.PositionChanged event of the binding source that IS generated by clicking add you can freely put defaults into the actual controls on the screen and they will not be overwritten by nulls, clearly you need to restrict this so it only happens if the user pressed add by adding a variable to the Clicked event of the add button to tell you when add has been clicked and resetting this variable on save or roll-back. It is also possible to modify the bindings themselves which have an if null or empty default value.
Hope this helps someone else :)
Use the MouseUp event on the 'Add Row' button in the BindingNavigator instead of the "Click" event. This is becouse the new ROW does not exist in the datagridview until AFTER the click event is completed.
Private Sub BindingNavigatorAddNewItem_MouseUp(sender As Object, e As MouseEventArgs) Handles BindingNavigatorAddNewItem.MouseUp
'works with add row from the binding navigator
myDataGridView.Item(3, myDataGridView.CurrentRow.Index).Value = txtDefaultDescrip.Text.Trim
End Sub
to do this same thing when adding data directly in you DataGridView do this.
Private Sub myDataGridView_DefaultValuesNeeded(sender As Object, e As DataGridViewRowEventArgs) Handles myDataGridView.DefaultValuesNeeded
'works with add row inside a datagridview
e.Row.Cells(3).Value = txtDefaultDescrip.Text.Trim
End Sub
In this example I have a editable default value shown on my from. If you have a non-changing default value (e.g., is always 1 for column 3) then you should do that when setting up your datasource.