In effetti hai ragione. Io ho convertito l'esempio quando studiavo, utilizzando il Codebehind. Ma tu non lo usi (e fai male) perciò:
1) inserisci questo spazio dei nomi: <%@ Import Namespace="System.Drawing"%>
2) Guarda il controllo datagrid e vedrai che è dichiarato l'evento:
OnItemCreated="ChangeColor"
Perciò, vai alla procedura ChangeColor e aggiungi il codice che ti ho suggerito
Ti rimando la pagina completa. La formattazione fa schifo e mi vergogno, ma non ho tempo di sistemare.
codice:
<%@ Page Language="VB"%>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<%@ Import Namespace="System.Drawing"%>
<script runat="server">
dim ds as DataSet
dim blnSet as Boolean = false
sub Page_Load(obj as Object, e as EventArgs)
ds = CreateDataSet
blnSet = true
if not Page.IsPostBack then
BindGrid
end if
end sub
sub BindGrid()
dgColors.DataSource = ds
dgColors.DataMember = "Colors"
DataBind()
end sub
sub ChangeColor(obj as object, e as DataGridItemEventargs)
dim intIndex as Integer = e.Item.ItemIndex
if blnSet then
if intIndex > 0 then
dgColors.Items(intIndex - 1).BackColor = Drawing.Color.FromName(ds.Tables("Colors").Rows(intIndex-1)("Color"))
dgColors.Items(intIndex - 1).ForeColor = Drawing.Color.FromName(ds.Tables("Colors").Rows(6-intIndex)("Color"))
end if
end if
Select Case e.Item.ItemType
Case ListItemType.EditItem
If e.Item.Cells(1).Controls.Count > 0 Then
If TypeOf e.Item.Cells(1).Controls(0) Is TextBox Then
Dim t As TextBox = DirectCast(e.Item.Cells(1).Controls(0), TextBox)
t.TextMode = TextBoxMode.MultiLine
t.BorderColor = Color.Blue
t.Width = Unit.Pixel(300)
End If
End If
End Select
end sub
sub dgColors_Edit(obj as object, e as DataGridCommandEventArgs)
dgColors.EditItemIndex = e.Item.ItemIndex
BindGrid()
end sub
sub dgColors_Cancel(obj as object, e as DataGridCommandEventArgs)
dgColors.EditItemIndex = -1
BindGrid()
end sub
sub dgColors_Update(obj as object, e as DataGridCommandEventArgs)
dim strColor as String = Ctype(e.Item.Cells(1).Controls(0), TextBox).Text
ds.Tables("Colors").Rows(e.Item.ItemIndex) ("Color") = strColor
ViewState("Colors")(e.Item.ItemIndex) = strColor
dgColors.EditItemIndex = -1
BindGrid
end sub
function CreateDataSet as DataSet
dim i as integer
dim arrColors() as string
if ViewState("Colors") is nothing then
arrColors = new String(6) {"red", "orange", "yellow", "green", "blue", "indigo", "white"}
ViewState("Colors") = arrColors
else
arrColors = ViewState("Colors")
end if
'crea l'oggetto dataset vuoto
ds = new DataSet("MyDataSet")
'Crea una nuova tabella e le relative colonne
dim dTable as New DataTable("Colors")
dTable.Columns.Add("Color", GetType(String))
dTable.Columns.Add("ID", GetType(Int32))
'add table
ds.Tables.Add(dTable)
'aggiungo le righe
for i=0 to 6
dim dr as DataRow = dTable.NewRow()
dr(0) = arrColors(i).ToString
dr(1) = i
dTable.Rows.Add(dr)
next
return ds
end function
Private Sub dgColors_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
Select Case e.Item.ItemType
Case ListItemType.EditItem
If e.Item.Cells(1).Controls.Count > 0 Then
If TypeOf e.Item.Cells(1).Controls(0) Is TextBox Then
Dim t As TextBox = DirectCast(e.Item.Cells(1).Controls(0), TextBox)
t.TextMode = TextBoxMode.MultiLine
t.BorderColor = Color.Blue
t.Width = Unit.Pixel(300)
End If
End If
End Select
End Sub
</script>
<html>
<body>
<form runat="server" ID="Form1">
<asp:datagrid id="dgColors" runat="server" AutoGenerateColumns="false" Width="200" OnEditCommand="dgColors_Edit"
OnUpdateCommand="dgColors_Update" OnCancelCommand="dgColors_Cancel" OnItemCreated="ChangeColor">
<columns>
<asp:templatecolumn HeaderText="id">
<itemtemplate>
<asp:label ID="lblID" runat="server" Text='<%# Container.DataItem("ID")%>'/>
</itemtemplate>
</asp:templatecolumn>
<asp:boundcolumn DataField="Color" HeaderText="Color" />
<asp:editcommandcolumn HeaderText="Change" EditText="Edit" UpdateText="Change" CancelText="Cancel" />
</columns>
</asp:datagrid>
</form>
</body>
</html>
ACTUNG: per ricopiare il codice, quota, selezioni tutto e copia, altrimenti avresti codice corrotto.
L'ho provato e funziona. Ciao