Hammy
Active member
I have been working through the SAMS teach yourself ASP.NET in
21 Days book, and I very stuck on something that the author
left "to me" to solve.
In a nutshell, he has created an updatable datagrid, that has
three columns, ID, COlor, Change. When you click on the edit link
in the change coloumn, you can type a new color and the rows
back color will be set to that color. He defines the table manually,
and uses an array to store the color value for each row. Then
uses the OnItemCreated event to draw the color for each row.
The problem is, the method he uses, because the OnItemCreated
event fires BEFORE the row is actually created, he colors the row
created previously. Using this method, the last row doesnt get
colored, and cant be changed. He has left it up to us to solve
this problem, and it is driving me NUTS!
The code is below, it is lengthy, I apologize...
Can anyone PLEASe, PLEASE tell me how to solve the mystery of
changing the code to allow the last row to be colored?
Thanks so much,
Hammy
[edit]use VB tags instead of PHP[/edit]
21 Days book, and I very stuck on something that the author
left "to me" to solve.
In a nutshell, he has created an updatable datagrid, that has
three columns, ID, COlor, Change. When you click on the edit link
in the change coloumn, you can type a new color and the rows
back color will be set to that color. He defines the table manually,
and uses an array to store the color value for each row. Then
uses the OnItemCreated event to draw the color for each row.
The problem is, the method he uses, because the OnItemCreated
event fires BEFORE the row is actually created, he colors the row
created previously. Using this method, the last row doesnt get
colored, and cant be changed. He has left it up to us to solve
this problem, and it is driving me NUTS!
The code is below, it is lengthy, I apologize...
Code:
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" debug="true"%>
<%@ Import Namespace="System.Data" %>
<%@ Import NameSpace="System.Data.OleDb" %>
<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
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
create an array of colors
dim i as integer
dim arrColors() as String
if ViewState("Colors") is Nothing then
arrColors = new String(6) {"Red", "orange", "yellow", "green", "blue", "indigo", "Violet"}
ViewState("Colors") = arrColors
Else
arrColors = ViewState("Colors")
End If
create an empty dataset
ds = new DataSet("MyDataSet")
Create a new table and columns
dim dTable as New DataTable("Colors")
dTable.Columns.Add("Color", GetType(String))
dTable.Columns.Add("ID", GetType(Int32))
add table
ds.Tables.Add(dTable)
add rows
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
</script>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form runat="server">
<ASP:DataGrid runat="server" id="dgColors" AutoGenerateColumns="false" Width="200"
OnEditCommand="dgColors_Edit" OnCancelCommand="dgColors_Cancel" OnUpdateCommand="dgColors_Update"
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>
Can anyone PLEASe, PLEASE tell me how to solve the mystery of
changing the code to allow the last row to be colored?
Thanks so much,
Hammy
[edit]use VB tags instead of PHP[/edit]
Last edited by a moderator: