473,320 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

DataGrid question (no ItemCreated event?)

(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
3 3292
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: vinayak | last post by:
Hi I am displaying data in Datagrid in ASP.NET with Edit/Update functionality for each row. On the same page I have 2 Button controls which submits the request to server. These button controls...
2
by: Jason | last post by:
I have a datagrid inside a user control. I use the itemcreated event to manipulate column values. I then add those values up and place the total in the footer of the datagrid. I have wrapped the...
0
by: Steve R | last post by:
I have a Datagrid with several TemplateColumns. I am creating ALL (BoundColumn & TemplateColumn) of the columns for the DataGrid during the PAGE_INIT. One TemplateColumn is a checkbox that allows...
1
by: Steve R | last post by:
I have a Datagrid with several TemplateColumns. I am creating ALL (BoundColumn & TemplateColumn) of the columns for the DataGrid during the PAGE_INIT. One TemplateColumn is a checkbox that allows...
7
by: Lars Netzel | last post by:
If I put a checkbox in a datagrid (ASP.NET) and set the Autopostback to true I can catch OnChange event on checkbox but how do I then catch what DataGridItemIndex is? Can I use some Event in the...
2
by: Ben | last post by:
Hi, I'd like to have a datagrid that has a dropdownlist in the pager control for setting the page size. I can get the control into the pager inside the datagrid itemcreated event by checking for...
5
by: Richard Myers | last post by:
Hello I have a datagrid that has a TemplatedColumn containing a dropdown list (DDL). This DDL is filled for each datagrid row via the itemcreated created event. The first time i postback none...
3
by: Arpan | last post by:
A DataGrid displays data from a DataSet. The DataGrid has the OnItemCreated event. Assume that the OnItemCreated event calls a sub named "InEditMode()". The DataGrid gets rendered as soon as this...
2
by: DC | last post by:
Hi, I am doing something like this in the ItemCreated event (ASP.Net 1.1): DataGridItem pagerRow = e.Item; TableCell pagerCell = pagerRow.Cells; Control addedControl = new Control();...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.