By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,709 Members | 2,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,709 IT Pros & Developers. It's quick & easy.

DataGrid question (no ItemCreated event?)

P: n/a
(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 ItemDataBound
but I can't figure out how to do this in Windows Forms.

Please Help/
Lars


Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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 ItemDataBound
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
DataGridTextBoxColumn. 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.

FullDisclosure: This was used from some website... No idea who
Public Class DataGridNoActiveCellColumn
Inherits DataGridTextBoxColumn

Private SelectedRow As Integer = -1
Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows.Forms.CurrencyManager, 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.Count + 1 Then
Me.DataGridTableStyle.DataGrid.UnSelect(SelectedRo w)
End If
SelectedRow = rowNum
Me.DataGridTableStyle.DataGrid.Select(SelectedRow)
End Sub

End Class

You can use the following to check what the value of your column is:
Dim DT as DataTable =
DirectCast(Me.DataGridTableStyle.DataGrid.DataSour ce, DataTable)
Debug.WriteLine(DT.Rows(RowNum)("ColumnName"))

Remember to cast the datasource to whatever data object you gave it.

Hope it helps
Chris
Nov 21 '05 #2

P: n/a
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 DataGridTextColumn 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
"ItemDataBound" instead..

/Lras
"Chris" <no@spam.com> wrote in message
news:uu****************@TK2MSFTNGP14.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
ItemDataBound 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
DataGridTextBoxColumn. 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.

FullDisclosure: This was used from some website... No idea who
Public Class DataGridNoActiveCellColumn
Inherits DataGridTextBoxColumn

Private SelectedRow As Integer = -1
Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows.Forms.CurrencyManager, 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.Count + 1 Then
Me.DataGridTableStyle.DataGrid.UnSelect(SelectedRo w)
End If
SelectedRow = rowNum
Me.DataGridTableStyle.DataGrid.Select(SelectedRow)
End Sub

End Class

You can use the following to check what the value of your column is:
Dim DT as DataTable =
DirectCast(Me.DataGridTableStyle.DataGrid.DataSour ce, DataTable)
Debug.WriteLine(DT.Rows(RowNum)("ColumnName"))

Remember to cast the datasource to whatever data object you gave it.

Hope it helps
Chris

Nov 21 '05 #3

P: n/a
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 DataGridTextColumn 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
"ItemDataBound" instead..

/Lras
"Chris" <no@spam.com> wrote in message
news:uu****************@TK2MSFTNGP14.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
ItemDataBound 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
DataGridTextBoxColumn. 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.

FullDisclosure: This was used from some website... No idea who
Public Class DataGridNoActiveCellColumn
Inherits DataGridTextBoxColumn

Private SelectedRow As Integer = -1
Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows.Forms.CurrencyManager, 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.Count + 1 Then
Me.DataGridTableStyle.DataGrid.UnSelect(SelectedRo w)
End If
SelectedRow = rowNum
Me.DataGridTableStyle.DataGrid.Select(SelectedRow)
End Sub

End Class

You can use the following to check what the value of your column is:
Dim DT as DataTable =
DirectCast(Me.DataGridTableStyle.DataGrid.DataSo urce, DataTable)
Debug.WriteLine(DT.Rows(RowNum)("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
datagridtextboxcolumn. 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 LateListTextBoxColumn
Inherits DataGridNoActiveCellColumn

Protected Overloads Overrides Sub Paint(ByVal g As
System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal
source As System.Windows.Forms.CurrencyManager, 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(DataGridTableStyle.DataGrid.DataSource,
DataView).Table.Rows(rowNum)("Completed")
If Not Completed Is DBNull.Value AndAlso CBool(Completed) =
True Then
g.FillRectangle(backBrush, bounds)
g.DrawString(Me.GetColumnValueAtRow(source,
rowNum).ToString, New Font(DataGridTableStyle.DataGrid.Font,
FontStyle.Strikeout), foreBrush, bounds.X, bounds.Y)
Else
MyBase.Paint(g, bounds, source, rowNum, backBrush,
foreBrush, alignToRight)
End If

End Sub

End Class
Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.