I created a user control with few labels, buttons and a dataGridView. I add the control on the fly and based on some conditions I apply different styles to data row.
1 ) styling works when i test on a form directly, however it doesn't when applying it in the user control
2 ) is it possible to hide the cell at the row level ?
A DataGridView is a DataGridView so there's no reason that anything that works with a grid on the form shouldn't work with a grid on the user control. I'll wager that you're not using the correct reference or something like that. Can you show us exactly what you're doing in each case, i.e. what works with the form and what doesn't work with the user control?
i add the user control multiple times on the main form, would there be a referrencing issue the way im doing it ?
in the usercontrol constructor i set the grid data source and then check rows data to apply cell styling
here is a sample example, it display correct data on all added user controls
Code:
sub new()
with grdView
.autogeneratecolumns=false
.datasource=---
.columns(0).datapropertyname="..."
.
.
.
end with
dim n as integer=0
for each orow as datarow in table
if orow(1)="I"
grdView.rows(n).DefaultCellStyle=oStyle1
end if
n+=1
next
end sub
Hmmm... nothing obvious there. Mind you, you obviously haven't posted the actual code, which I'm never a fan of. More than once I've seen people post what they think they have rather than what they actually have. Have you stepped through that code to see that it actually does what you expect?
By the way, shouldn't that loop be a For loop rather than For Each. Why keep a separate loop counter when a For loop inherently keeps a loop counter? Just use the loop counter to index both collections. Alternatively, use a For Each loop to go through the grid rows and use the DataBoundItem to access the bound data, negating the need for a loop counter altogether.
Private Sub Screen_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
loadCards()
End Sub
Sub loadCards(Optional nNo As Integer = 1)
Dim strServed As String = "served" & nStationNo.ToString.Trim
cmdstr = String.Format("select * from ordhdr where batchNo={0} and {1}=0 order by id desc", oCom.batchNo, strServed)
If oCom.sqlfill(cmdstr, "c_service") > 0 Then
For Each orow As DataRow In oCom.ds.Tables("c_service").Rows
Dim ucCard1 As New ucCard(orow("id"), orow("ordDt"), orow("Num")) With {.Name = "uc-" & orow("id")}
Me.Controls.Add(ucCard1)
Next
End If
End Sub
and this is the code the User control :
Code:
Sub New(nordId As Integer, dt As DateTime, ordNum As Integer)
' This call is required by the designer.
InitializeComponent()
ingCellStyle = New DataGridViewCellStyle
With ingCellStyle
.ForeColor = Color.Red
.BackColor = Color.AliceBlue
.Font = New Font("Arial", 12.0!, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
.Alignment = DataGridViewContentAlignment.TopCenter
End With
loadDetails()
End Sub
Sub loadDetails()
With Grd
.AutoGenerateColumns = False
.DataSource = oCom.ds.Tables(ctable)
.Columns(0).DataPropertyName = "sort"
.Columns(1).DataPropertyName = "Desc"
End With
' Restyle
'=========
Dim i As Integer = 0
For Each orow As DataRow In oCom.ds.Tables(ctable).Rows
If orow("Type") = "I" Then
'MsgBox(orow("itemdesc"))
' Grd.Rows(i).DefaultCellStyle = ingCellStyle
Try
Me.Grd.Rows(i).DefaultCellStyle.ApplyStyle(ingCellStyle)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End If
i += 1
Next
End Sub
Er ... the New Sub contains a predefined style and there doesn't appear to be any facility to change it. The loadDetails Sub (is it intended to be Public?) has no parameters so I can't see where exactly you think style changes could be introduced.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
ingCellStyle is declared globally, i set its property in the New() sub. then on the loadDetails i apply this style to rows that pass the conditon. I have had a similar issue before, where i was loading multiple Tabs with a GridView on each tab, evenThough i'd called the restyling on the load event, only the focused Tab seemed to display the changes. The work around was to call the Restyling method on the Tab.selectedIndex change.
MsgBox(ex.ToString) ' what's the point of this? There's nothing anyone can do about it!
End Try
End If
i += 1
Next
' So we've been through this once for the initialisation. What happens if a new row is added or the pertinent value changed?
End Sub
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
MsgBox(ex.ToString) ' what's the point of this? There's nothing anyone can do about it! --- >>> [ Good point, again just for testing as vs2010 (on 64bit) does not report errors when debuging]
End Try
End If
i += 1
Next
' So we've been through this once for the initialisation. What happens if a new row is added or the pertinent value changed? -- >>> [ once loaded, its static now data will be added]
[ i actually use the parameters here and load the datatable cTable used below, i took out to simpllify as it nor relevant to my question]
You may not consider it relevant to the question but it is extremely relevant to our understanding of the Class. Do you think you could zip up your project in full and attach it here so that we can see it in action?
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!