Change datagrid selected row color programatically using wpf? - wpfdatagrid

I want to change the selected row color dynamically in code behind. I'm using Visual Studio 2008. Can anyone help to do this?

Dim selectedItem As New Style(GetType(DataGridCell))
Dim triggerIsSelected As New Trigger() With {.[Property] = DataGridCell.IsSelectedProperty, .Value = True}
triggerIsSelected.Setters.Add(New Setter(DataGridCell.BackgroundProperty, System.Windows.Media.Color.Red))
selectedItem.Triggers.Add(triggerIsSelected)
Datagrid1.CellStyle = selectedItem

Related

How to access cells from programmatically created datagridview in VB.net?

When creating a datagridview in VB.net I cannot get at the cells from anywhere except the Sub where it was made.
I tried placing the code in Form_Load and tried make the Sub Public.
I use Controls.Find, which finds the datagridview, but I cannot access any rows or cells. I can create textbox and label controls and access their text content from anywhere, but not so with the datagridview. This is odd to me.
Using and learning Visual Basic in Visual Studio 2017, for use in my own home business utility.
Dim dgv As New DataGridView
dgv.Location = New Point(2, 200)
dgv.Size = New Size(300, 50)
dgv.Name = "NewDGV"
Dim ColumnTitles() As String = {"ProdId", "Price"}
Dim ColumnWidths() As Integer = {50, 50)
For i = 0 To UBound(ColumnTitles)
Dim col As New DataGridViewTextBoxColumn
col.DataPropertyName = ColumnTitles(i)
col.HeaderText = ColumnTitles(i)
col.Name = ColumnTitles(i)
col.Width = ColumnWidths(i)
dgv.Columns.Add(col)
Next
Controls.Add(dgv)
‘the following sees the datagridview just fine
‘within the same sub
Dim x = dgv.Rows(0).Cells(0).Value
This is how I am creating this control. Perhaps I am Adding to Controls in the wrong way? I am open to better ways to do this.
Dim dgv As DataGridView = CType(Controls("NewDGV"), DataGridView)
dgv.Rows(0).Cells(0).Value = "I found you!"
What this does is find the first Control in the Controls collection, coerces it into a DataGridView type, and assigns it to the variable dgv. You can than manipulate dgv. In this case I put some text in the first cell on the first row.
You could ask the control collection for the instance of your datagridview
Dim MyDgv As DataGridView = Controls.Cast(Of DataGridView).First(Function(dgv) dgv.Name = "NewDGV")
Then work with "MyDGV" properties, methods, and/or events

How to display text on a programatically created button?

I've created buttons programatically, and they have a text value but for some reason don't show it.
Dim NodeButton As New Control
NodeButton.Name = "Button" & NodeID
NodeButton.BackColor = Color.Red
NodeButton.Text = (NodeID)
Where NodeID is a variabe declared elsewhere in the program
First, as already said in the comments you should change Dim NodeButton As New Control to Dim NodeButton As New Button
In second place I believe that you should add the new control to your form using something like this:
Me.Controls.Add(NodeButton)

show/hide textbox by change value of combobox

i'm having problem regarding combobox in vb.net,
im trying to show & hide specific text boxes, on the base of an item when it is selected from combobox,
here is the code i'm writing, whats mistake in it?
Dim PatSearchID, PatSearchName, PatSearchCat As String
PatSearchID = txtSearchID.Text
PatSearchCat = cmbSearchPat.SelectedItem.ToString()
If PatSearchCat = "RegNo" Then
txtSearchID.Show()
txtSearchName.Hide()
End if
kindly correct what the problem is?
txtSearchName.Visible = False
30chars
instead of this (txtSearchID.Show() txtSearchName.Hide()) try to use the visible property of textbox

Programmatically check a checkbox in ListView

I am trying to programmatically check a checkbox of a ListView (using VB & .NET 4).
The ListView lvVorschlag has been created in the designer, along with three elements. I then do the following:
Dim lviOptimal As New ListViewItem("Optimal")
lviOptimal.SubItems.Add(...) 'several SubItems are added
lvVorschlag.Items.Add(lviOptimal)
lvVorschlag.Items(0).Selected = True
All the SubItems are correctly added and the line lvVorschlag.Items(0).Selected = True does not give me an error. But nothing is checked. Any idea why?
Note: I also tried with lvVorschlag.Items("Optimal").Selected = True but it gives me an error saying that this object is Nothing. Too bad, referring by name would have been easier.
You should use the Checked property to check the item(s) you'd like:
lvVorschlag.Items(0).Checked = True
Set the focus on the item
Dim lviOptimal As New ListViewItem("Optimal")
lviOptimal.SubItems.Add(...) 'several SubItems are added
lvVorschlag.Items.Add(lviOptimal)
lvVorschlag.Items(0).focus()
lvVorschlag.Items(0).Selected = True
http://msdn.microsoft.com/en-us/library/y4x56c0b%28v=vs.100%29.aspx

How to add CheckBox at DevExpress xtraGrid

I want to add CheckBox at DevExpress's xtraGrid column so for this what should i do
Create a new Editor Repository Item (CheckEdit) in the grid's designer (under In-Place Editor Repository) and add it to the ColumnEdit property of the column (under Columns)
EDIT: Adding programmatically
Dim repstryItemCheckEdit As New RepositoryItemCheckEdit
GridControl.RepositoryItems.Add(repstryItemCheckEdit)
Dim chckEdtColumn As GridColumn = myGrid.Columns.AddField("Select")
With chckEdtColumn
.VisibleIndex = myGrid.Columns.Count
End With
myGrid.Columns.Add(grsSalColumn)
myGrid.Columns("Select").ColumnEdit = repstryItemCheckEdit