Lars Netzel wrote:
Sounds a bit complicated...
I found another way.. going the the BindingContext and then on the current
check what I needed and then disable the DataGridTextCol umn for that row.
Okay it's only visible when actually selecting the row but it keeps the user
from typing at least.
Would have been nicer to have different colors on different rows thru a
"ItemDataBo und" instead..
/Lras
"Chris" <no@spam.com> wrote in message
news:uu******** ********@TK2MSF TNGP14.phx.gbl. ..
Lars Netzel wrote:
(applies to Windows Form .NET 2003)
I'm filling a datagrid from a Datatable and applying a DataGridStyle.
The Source Fields are "Name", "Value", "Locked"
and the Style's Columns are "Name", "Value"
Depending on the "Locked" field in the source... I want the "Value" cell
to be readonly or not for each row
In ASP.NET this would be a piece of cake with ItemCreated or
ItemDataBoun d but I can't figure out how to do this in Windows Forms.
Please Help/
Lars
I agree, in windows they should have added an itemcreated or itemdatabound
event.
The way I end up doing this in windows is to create an inherited
DataGridTextB oxColumn. The example below is used to make it so no column
can be selected but selects the entire row instead. You can modify this
to do what you want based on your values.
FullDisclosur e: This was used from some website... No idea who
Public Class DataGridNoActiv eCellColumn
Inherits DataGridTextBox Column
Private SelectedRow As Integer = -1
Protected Overloads Overrides Sub Edit(ByVal source As
System.Window s.Forms.Currenc yManager, ByVal rowNum As Integer, ByVal
bounds As System.Drawing. Rectangle, ByVal [readOnly] As Boolean, ByVal
instantText As String, ByVal cellIsVisible As Boolean)
'make sure previous selection is valid
If SelectedRow > -1 And SelectedRow < source.List.Cou nt + 1 Then
Me.DataGridTabl eStyle.DataGrid .UnSelect(Selec tedRow)
End If
SelectedRow = rowNum
Me.DataGridTabl eStyle.DataGrid .Select(Selecte dRow)
End Sub
End Class
You can use the following to check what the value of your column is:
Dim DT as DataTable =
DirectCast(Me .DataGridTableS tyle.DataGrid.D ataSource, DataTable)
Debug.WriteLi ne(DT.Rows(RowN um)("ColumnName "))
Remember to cast the datasource to whatever data object you gave it.
Hope it helps
Chris
You can do different colors by overriding the onpaint event of the
datagridtextbox column. This example changes the font based on values in
the datagrid, but to change the color, just change the fore/back brush
Chris
Public Class LateListTextBox Column
Inherits DataGridNoActiv eCellColumn
Protected Overloads Overrides Sub Paint(ByVal g As
System.Drawing. Graphics, ByVal bounds As System.Drawing. Rectangle, ByVal
source As System.Windows. Forms.CurrencyM anager, ByVal rowNum As Integer,
ByVal backBrush As System.Drawing. Brush, ByVal foreBrush As
System.Drawing. Brush, ByVal alignToRight As Boolean)
Dim Completed As Object =
DirectCast(Data GridTableStyle. DataGrid.DataSo urce,
DataView).Table .Rows(rowNum)(" Completed")
If Not Completed Is DBNull.Value AndAlso CBool(Completed ) =
True Then
g.FillRectangle (backBrush, bounds)
g.DrawString(Me .GetColumnValue AtRow(source,
rowNum).ToStrin g, New Font(DataGridTa bleStyle.DataGr id.Font,
FontStyle.Strik eout), foreBrush, bounds.X, bounds.Y)
Else
MyBase.Paint(g, bounds, source, rowNum, backBrush,
foreBrush, alignToRight)
End If
End Sub
End Class