467,122 Members | 1,256 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,122 developers. It's quick & easy.

DataGrid Conditional ReadOnly Cell

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

Nov 21 '05 #1
  • viewed: 2277
Share:
4 Replies
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.
Nov 21 '05 #2
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
"Rulin Hong" <Ru*******@discussions.microsoft.com> wrote in message
news:95**********************************@microsof t.com...
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.

Nov 21 '05 #3
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?

"Rulin Hong" <Ru*******@discussions.microsoft.com> wrote in message
news:95**********************************@microsof t.com...
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.

Nov 21 '05 #4
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
"Doug Bell" <dug@bigpond> wrote in message
news:ea*************@TK2MSFTNGP14.phx.gbl...
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

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Bill C. | last post: by
2 posts views Thread by CSL | last post: by
3 posts views Thread by Lyners | last post: by
8 posts views Thread by Scott Meddows | last post: by
2 posts views Thread by Flack | last post: by
6 posts views Thread by Doug Bell | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.