Remove row/item from RadGridView WPF - wpf

I have a code similar to next to load a data from a SQL query.....
Dim myDataset as new dataset = myMethod(params) 'This is a methos that fills a common dataset.
With Me.myRadDataGrid
.AutoGenerateColumns = True
.ItemsSource = myDataset.Tables(0).Rows
End With
So far so good....but when I tried to remove a line the object items do not nothing; the line do not show any exception, but nothing happend...
Me.myRadDataGrid.Items.RemoveAt(myIndex) 'Nohitng happend
Me.myRadDataGrid.Items.Remove(Me.myRadDataGrid.SelectItem) 'Nothig happend
Me.myRadDataGrid.Items.Refresh()
Me.myRadDataGrid.Rebind()
At the end the dataset collection into a RadGridView has the same elements....do not remove any row.
Thanks to all....

You need to remove the item from the ItemsSource, not from the Items collection.
So remove the item from myDataset.Tables(0).Rows, and you'll probably need to refresh the grid manually since I'm fairly sure a DataTable will not automatically raise a change notification like an ObservableCollection does when an item gets removed.

Related

Changed value in DGV not saved when main form moves to next row

I have a form displaying data from a parent table one row at a time. The user moves from one row to another using the form's binding navigator. This same form also contains a DGV that displays data from a related child table. When this form is closed, it saves the data back to the source databases using the data adapters used to populate the form and the DGV.
The problem I have is if the user changes a cell on the DGV without moving to another cell or row in the DGV but simply uses the binding navigator to move to another row and then back, the change in the DGV cell is lost. How can I prevent this?
I tried changing the binding navigator's MoveNextItem property to "None" and then added the following code:
Private Sub BindingNavigatorMoveNextItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorMoveNextItem.Click
BSExps.EndEdit()
BS.MoveNext()
Where "BSExps" is the binding source used to populate the child table and "BS" is the binding source for the parent table and binding navigator. However, this does not resolve the issue.
How can I programmatically save any changes to the DGV if the user simply moves to another row of the parent table and does not click any other cell or row in the DGV?
Thanks in advance.
I changed the "BSExps.EndEdit" to:
Me.DataGridView1.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange)
This seems to have solved the problem, now any change to the DGV is saved when the user moves to another record and then moves back.

How to Populate Label with String Array on ComboBox Item Selection in VB.Net

I have an application in VB.Net where I'm trying to fill a label with some string data that I have in an array. I have a ComboBox which holds some states as the index collection/values. When I select a particular value in the combobox, I want to pull string data from the assigned array, and populate the label with it as a "clickable link a browser window. I'm lost on this, yet here is what I have in my code stub:
Private Sub cboSelectState_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboSelectState.SelectedIndexChanged
'Create a string array
Dim AlabamaCities() As String = {"http://www.rolltide.com/", "http://www.crimsontidehoops.com/", "http://centralalabamapride.org/"}
Dim strAlabama As String
'Populate label with the array data, on a particular value selection in combo box.
If cboSelectState.SelectedValue("Georgia") Then
strAlabama = CStr(AlabamaCities(3))
lblLinkOutput.Text = strAlabama
End If
End Sub
So when I pick Alabama in my combo box, I want the label to show:
http://www.rolltide.com
http://www.crimsontidehoops.com
http://centralalabamapride.org
The links will be clickable from the label and populate in the same tab whenever clicked. I haven't tried the clickable link part yet, and I will try once I get this down.
I know it's probably bad starting form out the gate. But I'm trying to get the form down to gain the knowledge and plan out a bigger project, and accomplish something better when I think of it. I appreciate your knowledge and assistance.
Firstly, it would make sense to use a Dictionary to store the data. The state names will be the keys and the values would be the arrays of URLs. You would then display the keys in the ComboBox and, when a selection is made, use the selected key to get the corresponding value from the Dictionary.
At that point, you won't be using a Label if you want clickable links. You should use a TableLayoutPanel as a container and then add one LinkLabel to the table for each URL in the array. You can then use a single handler for all the LinkClicked events.

Devexpress checkbox column in a gridcontrol c#

I have a datagrid from devexpress in a winform application.I populated the datagrid from code (datasource=my datatable with 2 strings columns and one of type int (receive 0 and 1 values-should be checkbox on form).
I added a datagrid and from designer I added 3 columns.The last one of type checkbox.I set
UnboundType=Integer,
UnboundExpression=my column name from datatable :
DisplayFormat = Numeric,
columnedit = checkbox,
Displaycheckedvalue = 1,
displayuncheckedvalue = 0.
When I run the application always my checkboxes are blue.If i press one checkbox appear a check but when i go to the next row the previous value is again blue.What should i do so why the application keep the checks?
Don't set the column to be unbound, it is in your datasource after all.
What you want to do is just to set Displaycheckedvalue and displayuncheckedvalue as you have done, nothing more.
You can make a class which got a property for each column of your table. The int value would be a bool and so you can use List as datasource. The Grid will automaticly add checkboxes for the bool value.
If you want to use DataTable see Stig's post but in my opinion it is more beautiful to use classes and List.

Datagrid Row Selection On Button Click In WPF

I am having a Datagrid that gets populated with values from a DataTable. In my program i have four buttons: Goto First, Last, Next and Previous, as the name name indicates i have to select the rows based on the selection made using these buttons. Everything seems well if i use the below code to get the row (for example first row).
DataGridRow row =(DataGridRow)userControl.m_DataGrid.ItemContainerGenerator.ContainerFromIndex(0);
row.IsSelected = true;
But the code throws null value when there is more rows than the height of the Datagrid(When scrollbar comes into picture).
Please help me out of this issue. I think this is because of the view problem.
Due to virtualization the containers are only created when the object is in view, so you could first scroll the item into view using the respective method, wait for the creation of the container and then select it.
As this is rather messy i would suggest binding the IsSelected to a property on your item using a style for DataGridRow (set it as ItemContainerStyle). Then you can just set the property to true and scroll the item into view if need be.

Windows Forms ComboBox problem

I have a combobox that goes out to the database to load it's content on clicking the dropdown arrow. if after the dropdown box is shown with data and I don't select anything, than the current value of the combobox is blown away. Inside my routine to load data, I tried saving the current value and restoring it back after the loading is done. doesn't work.
[Edit] Added the code from the comment here for legibility
MyUltraCombo myultracombo = new MyUltraCombo();
//MyUltraCombo inherits from UltraCombo inside MyUltraCombo, I keep the table name to load from
MyUltraCombo.BeforeDropDown += new System.ComponentModel.CancelEventHandler(cb_BeforeDropDown);
//inside the eventhandler
myultracombo.Load();
//inside the Load method
datatable = DataUtility.GetAllRecords(tablename);
combobox.datasource = datatable;
If you store "the current value" as just the SelectedItem property, then it will probably not be an object that is present in the new list, since it will consist of completely new objects. What you will have to do is store an ID of the current value (if you're lucky to have a unique id) and then search the newly created list for an object with the same id (or whatever you use to uniquely identify an item, maybe just ToString()...).

Resources