DataGrid Conditional ReadOnly Cell

D

Doug Bell

Hi,
I needed to make a TextBox on a DataGrid ReadOnly based on the condition of
another cell on its row.

I achieved this using a Custom Data Column Class "DacDGTextColLotNo" but I
can't get the TextBox to display its value when you leave the TextBox.

I figure that I need to use the SetColumnValueAtRow Method but I am
struggling to understand how.

I would appreciate any guidance.

My Create Grid Style Code on the Form:
........
CreateGridStyle = New DataGridTableStyle
........
........
If PurchStyle <> 1 Then
'Set Lot Nos Column
Dim DGTxtBoxLotNo AS New DacDGTextColLotNo
With DGTxtBoxLotNo
.MappingName = "LotNo"
.HeaderText = "Lot"
.Width = 50
.Alignment = HorizontalAlignment.Left
.NullText = ""
.Format = ""
.ReadOnly = True

End With

CreateGridStyle.GridColumnStyles.Add(DGTxtBoxLotNo)

..........

My Custom Column Style Class Code:

Public Class DataGridTextBoxColumn

Inherits DataGridTextBoxColumn

Public Sub New()
MyBase.New()
Me.MappingName = MappingName
Me.Format = Format
Me.Alignment = Alignment
Me.Width = Width
Me.ReadOnly = ReadOnly
Me.Headertext = Headertext
Me.NullText = NullText
End Sub

Protected Overrides Function GetColumnValueAtRow _
(ByVal source As System.Windows.Forms.CurrencyManager, _
ByVal rowNum As Integer) As Object

Dim drv As DataRowView = CType([source].Current, DataRowView)
Try
Me.ReadOnly = Cint(drv("LotFlg")) <> 2
Catch
Me.ReadOnly = True
End try

End Function

End Class


Thanks

Doug
 
G

Guest

DataGrid just displays data from datasource, let's say from datatable. When
you change data in textbox, there is nothing changed in datatable. Then you
leave the textbox, DacDGTextColLotNo needs update data in datatable which is
done by SetColumnValueAtRow Method.
 
D

Doug Bell

As said in my question, I figured that SetColumnValueAtRow Method was
required!

But the basis of my question was that I have not been able to work out how
to get it to work.


Doug
 
D

Doug Bell

In fact the data entered is passed to the underlying DataTable, it is just
not being displayed in the cell whenever you mover to another row or column?

So does that indicate that SetColumnValueAtRow is not an issue?

Where else do I look?
 
D

Doug Bell

Hi,
I have resolved this problem.

For anyone interested, I believe that the problem was occuring because my
Class DataGridTextBoxColumn function "GetColumnValueAtRow" was over-riding
the Base method and the Row/Column value was not being writen back to the
Grid
The following code works:


Public Class DataGridTextBoxColumn

Inherits DataGridTextBoxColumn

Public Sub New()
MyBase.New()
Me.MappingName = MappingName
Me.Format = Format
Me.Alignment = Alignment
Me.Width = Width
Me.ReadOnly = ReadOnly
Me.Headertext = Headertext
Me.NullText = NullText
End Sub

Protected Overrides Function GetColumnValueAtRow _
(ByVal source As System.Windows.Forms.CurrencyManager, _
ByVal rowNum As Integer) As Object

Dim s as Object = MyBase.GetColumnValueAtRow([source], rowNum)
Dim drv As DataRowView = CType([source].Current, DataRowView)
Dim fReadOnly As Boolean
Try
fReadOnly = drv("LotFlg").ToString <> "2"
Catch
fReadOnly = True
End try

Me.ReadOnly = fReadOnly

Return s

End Function

Doug
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top