473,657 Members | 2,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3316
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
DataGridTextBox Column. 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 DataGridNoActiv eCellColumn
Inherits DataGridTextBox Column

Private SelectedRow As Integer = -1
Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows. Forms.CurrencyM anager, 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.D ataGridTableSty le.DataGrid.Dat aSource, 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 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
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
DataGridTextBox Column. 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 DataGridNoActiv eCellColumn
Inherits DataGridTextBox Column

Private SelectedRow As Integer = -1
Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows. Forms.CurrencyM anager, 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.D ataGridTableSty le.DataGrid.Dat aSource, 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 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
Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4912
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 are Web Control & not HTML control. One of these buttons whose title is Delete is added on the aspx page in design view & also I double clicked on this button in design view to get the onclick code for this button in the code behind page. & for...
2
1182
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 function used to obtain the data for this datagrid in a page.ispostback=false condition. Everything displays accurately when an event(in the user control) posts the page back to the server, but if a control outside the user control posts the page...
0
993
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 the user to select the row/datagriditem to carry forward in a wizard. The other TemplateColumns contain a System.Web.UI.WebControls.Image. In the ITEMCREATED event for the datagrid, I toggle the ImageURL property for each Image based on certain...
1
1134
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 the user to select the row/datagriditem to carry forward in a wizard. The other TemplateColumns contain a System.Web.UI.WebControls.Image. In the ITEMCREATED event for the datagrid, I toggle the ImageURL property for each Image based on certain...
7
2271
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 Datagrid that will fire off when the Checkbox is changed? regards /Lars
2
3995
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 ListItemType.Pager. The problem I'm having is subscribing to the selectedindex changed event. The datagrid doesn't even seem fire an itemcommand, but that's ok, just the selectedindex changed event would do... Any advice is greatly appreciated!
5
1354
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 of the selections made by the user are available for each dropdownlist on the server. They all simply have values equal to the first record of the datasource.... which is not very useful.
3
1403
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 ASPX page loads in the browser. Now since the DataGrid has got its OnItemCreated event, will it's event function i.e. "InEditMode()" execute before the Page_Load sub executes or will it execute after the Page_Load sub gets executed? Thanks,
2
6750
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(); addedControl.ID = "addedControl"; pagerCell.Controls.Add(addedControl);
0
8845
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8622
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.