Can anyone assist me on how I edit a field? Using the
EditItemTemplate and I am assuming I would need to update the
DataTable also?
Sorry I had trouble posting yesterday. All of my posts did not appear
on the newsgroup until this morning.
Here is where I am at today. I had to change the "Duration (days)"
column heading to just "Duration"; that seemed to get rid of the
"Exception has been thrown by the target of an invocation" error when
the data was bound to the GridView. I added a drop-down box in the
EditItemTemplate for the Resource and Type columns that are bound to
an SQL table (example table data is below). Now when I click the edit
button (I have to click it twice) I get this error: "Failed to load
viewstate. The control tree into which viewstate is being loaded must
match the control tree that was used to save viewstate during the
previous request. For example, when adding controls dynamically, the
controls added during a post-back must match the type and position of
the controls added during the initial request."
****** DEFAULT.ASPX******
<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Sample DataTable Bound to GridView</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="lstTasks" runat="server" Height="88px"
SelectionMode="Multiple" Width="296px">
<asp:ListItem>Receipt of Order</asp:ListItem>
<asp:ListItem>Review Drawings</asp:ListItem>
<asp:ListItem>Produce Shop Drawings</asp:ListItem>
<asp:ListItem>Program Machines</asp:ListItem>
</asp:ListBox><br />
<br />
<asp:Button ID="Button1" runat="server" Text="Create DataTable
and Bind to GridView" /><br />
<br />
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" CellPadding="4" Font-Names="Verdana" Font-
Size="Small" ForeColor="#333333" AutoGenerateEditButton="True">
<Columns>
<asp:BoundField HeaderText="Task ID" DataField="Task
ID" ReadOnly="True" SortExpression="Task ID" />
<asp:BoundField HeaderText="Task Name" DataField="Task
Name" ReadOnly="True" SortExpression="Task Name" />
<asp:TemplateField HeaderText="Resource"
SortExpression="Resource">
<EditItemTemplate>
<asp
ropDownList ID="ddlRes" runat="server"
DataSourceID="sqlRes" DataTextField="Resource Name"
DataValueField="Resource Name">
</asp
ropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblRes" runat="server" Text='<
%# Bind("Resource") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Duration"
SortExpression="Duration">
<EditItemTemplate>
<asp:TextBox ID="txtDuration" runat="server"
Text='<%# Bind("[Duration]") %>'></asp:TextBox><br />
<asp:RequiredFieldValidator ID="rfvDuration"
runat="server" ControlToValidate="txtDuration"
Display="Dynamic" ErrorMessage="Enter a
Duration (number of days)." Font-Names="Verdana"
Font-Size="Small" SetFocusOnError="True"></
asp:RequiredFieldValidator><asp:RangeValidator
ID="rvDuration" runat="server"
ControlToValidate="txtDuration" Display="Dynamic"
ErrorMessage="Enter a numeric Duration
(number of days)." Font-Names="Verdana"
Font-Size="Small" MaximumValue="365"
MinimumValue="0" SetFocusOnError="True"
Type="Integer"></asp:RangeValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblDuration" runat="server"
Text='<%# Bind("[Duration]") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dependent Task"
SortExpression="Dependent Task">
<EditItemTemplate>
Enter Dependent Task ID (separate multiples
with comma)<br />
<asp:TextBox ID="txtDepTask" runat="server"
Text='<%# Bind("[Dependent Task]") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblDepTask" runat="server"
Text='<%# Bind("[Dependent Task]") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type"
SortExpression="Type">
<EditItemTemplate>
<asp
ropDownList ID="ddlType" runat="server"
DataSourceID="sqlTaskLink" DataTextField="Abbrev"
DataValueField="Abbrev">
</asp
ropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblType" runat="server" Text='<
%# Bind("Type") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#E3EAEB" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True"
ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True"
ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True"
ForeColor="White" />
<EditRowStyle BackColor="Yellow" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:SqlDataSource ID="sqlRes" runat="server"
ConnectionString='Data Source=MYSQLSERVER;Initial
Catalog="MYDb";Integrated Security=True'
ProviderName="System.Data.SqlClient" SelectCommand="SELECT
[Resource Name] FROM dbo.[PMJ RESOURCES] ORDER BY [Resource Name]">
</asp:SqlDataSource>
</div>
<asp:SqlDataSource ID="sqlTaskLink" runat="server"
ConnectionString='Data Source=MYSQLSERVER;Initial
Catalog="MYDb";Integrated Security=True'
ProviderName="System.Data.SqlClient" SelectCommand="SELECT
Abbrev FROM dbo.[PMJ TASK LINK] ORDER BY Abbrev">
</asp:SqlDataSource>
</form>
</body>
</html>
******DEFAULT.ASPX.VB******
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim objDT As System.Data.DataTable = GetCustomMadeDataTable()
GridView1.DataSource = objDT
GridView1.DataBind()
End Sub
Protected Function GetCustomMadeDataTable() As
System.Data.DataTable
'Create a new DataTable object
Dim objDataTable As New System.Data.DataTable
Dim i As Integer, iCt As Integer
objDataTable.Columns.Add("Task ID", String.Empty.GetType)
objDataTable.Columns.Add("Task Name", String.Empty.GetType)
objDataTable.Columns.Add("Resource", String.Empty.GetType)
objDataTable.Columns.Add("Duration", String.Empty.GetType)
objDataTable.Columns.Add("Dependent Task",
String.Empty.GetType)
objDataTable.Columns.Add("Type", String.Empty.GetType)
'display all tasks in a DataTable which will then be bound to
a GridView
For i = 0 To lstTasks.Items.Count - 1
If i = 0 Then
iCt = 1
Else
iCt = iCt + 1
End If
objDataTable.Rows.Add(New String() {iCt,
lstTasks.Items(i).Value.ToString, "ABC Company", "0", iCt - 1, "Finish-
To-Start"})
Next
Return objDataTable
End Function
Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs)
Handles GridView1.RowCancelingEdit
e.Cancel = True
End Sub
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.GridViewEditEventArgs) Handles
GridView1.RowEditing
GridView1.EditIndex = e.NewEditIndex
End Sub
Protected Sub GridView1_RowUpdated(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles
GridView1.RowUpdated
GridView1.EditIndex = -1
End Sub
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal
e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles
GridView1.RowUpdating
End Sub
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
GridView1.SelectedIndexChanged
End Sub
End Class
My Resources table has the following data:
ABC Company ...
read more »