What control would you use to display unbound data?

  • Thread starter Thread starter Mel
  • Start date Start date
M

Mel

I want to display custom data in some sort of control on a web page.
By custom data I mean it's not in a database yet it just resides in a
List Box on a different tab . I just want to display the data and
allow the user to edit a couple values. What control can I use that
will allow me to manually fill it with whatever data I want and allow
user editing?

(using: asp.net 2.0, vb.net, visual studio 2005, winxp pro sp2)
 

Okay I've got my data table made and now it's bound to my DataGrid. I
have some columns in my DataGrid that I need to allow the user to
edit. Do I need to create all of the Edit Item Template fields
programmatically then?

I tried creating the columns and templates in the DataGrid (using the
Wizard) and then bind the data table to the DataGrid programmatically
but my columns just append to the columns I had defined with the
wizard.
 
Do yourself a *HUGE* favour and ignore all of these wizards - IMO, they
cause more problems than they solve...

1) Create your DataTable and populate it.

2) Create a GridView manually (why are you still using a DataGrid, AAMOI?)
and define the columns to match those in the DataTable.

3) Bind the GridView to the DataTable.

Oh, the example you provided uses a DataGrid. I can use a GridView
though.
 
Oh, the example you provided uses a DataGrid.  I can use a GridView
though.- Hide quoted text -

- Show quoted text -

When you say create the GridView manually, you mean in my aspx.vb
code? So I will need to create the column headings, row data, and
template fields?
 
No - I mean in your aspx markup.


You will need to create the columns and any template fields. The rows,
obviously, will be created when the GridView is bound to the DataTable...

Okay that is what I did but I am not getting correct results in my
GridView. In the Design view of my aspx page I clicked on the
GridView and chose "Edit Columns" (this is the wizard I was referring
to earlier) and I added 6 columns as BoundFields named exactly like
the columns in my DataTable. Then in my vb code I bind the DataTable
I have created and populated to the GridView the DataTable columns
just append to the GridView control. The first 6 columns don't have
any row data and the next 6 do.
 
Hmm - OK, please post the markup for the GridView plus the code-behind...

Here is an example project.

******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 />
&nbsp;<br />
<asp:Button ID="Button1" runat="server" Text="Create DataTable
and Bind to GridView" /><br />
<br />
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:BoundField HeaderText="Task ID" />
<asp:BoundField HeaderText="Task Name" />
<asp:BoundField HeaderText="Resource" />
<asp:BoundField HeaderText="Duration (days)" />
<asp:BoundField HeaderText="Dependent Task" />
<asp:BoundField HeaderText="Type" />
</Columns>
</asp:GridView>
</div>
</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 (days)",
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
End Class
 
<asp:BoundField DataField="Task ID" HeaderText="Task ID" />

etc

It was the DataField property I was missing. It works now. Thank you
SO very much Mark for sticking with me on this topic and also for
putting up with a relatively new person to the web programming world.
Your help is very much appreciated.
 
It was the DataField property I was missing.  It works now.  Thank you
SO very much Mark for sticking with me on this topic and also for
putting up with a relatively new person to the web programming world.
Your help is very much appreciated.

I made a couple of the fields template fields (so I can allow editing
now) but I am getting this error "Exception has been thrown by the
target of an invocation." It is referring to this line in my aspx
code:

<ItemTemplate>
<asp:Label ID="lblDuration" runat="server" Text='<%# Bind("[Duration
(days)]") %>'></asp:Label>
</ItemTemplate>

What am I doing wrong?
 
It was the DataField property I was missing.  It works now.  Thank you
SO very much Mark for sticking with me on this topic and also for
putting up with a relatively new person to the web programming world.
Your help is very much appreciated.

Can anyone show me how I would enable editing for the "Resource"
column? For example, upon editing for the resource column I want a
drop-down list of items they can choose from. I added a command field
to my GridView and I made the resource column a template field but
when I click edit my drop-down box for the resource column does not
appear.
 
<asp:BoundField DataField="Task ID" HeaderText="Task ID" />

etc

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?
 
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 />
&nbsp;<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:DropDownList ID="ddlRes" runat="server"
DataSourceID="sqlRes" DataTextField="Resource Name"
DataValueField="Resource Name">
</asp:DropDownList>
</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:DropDownList ID="ddlType" runat="server"
DataSourceID="sqlTaskLink" DataTextField="Abbrev"
DataValueField="Abbrev">
</asp:DropDownList>
</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
Customer
Architect
Engineering
Finish

My PMJ Task Link table has the following data (fs = finish-to-start,
ff = finish-to-finish, etc.):
FS
SS
FF
SF
 
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 />
        &nbsp;<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:DropDownList ID="ddlRes" runat="server"
DataSourceID="sqlRes" DataTextField="Resource Name"
                            DataValueField="Resource Name">
                        </asp:DropDownList>
                    </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:DropDownList ID="ddlType" runat="server"
DataSourceID="sqlTaskLink" DataTextField="Abbrev"
                            DataValueField="Abbrev">
                        </asp:DropDownList>
                    </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 »

I figured out how to edit the duration column and update the
DataTable; I had to rebind the data upon editing and updating (see
http://www.velocityreviews.com/foru...und-datatable-how-to-get-updates-working.html).
Very cool by the way. The only thing I have left to do is when they
edit the Resource and a drop-down box of possible selections appears
set the SelectedValue equal to the current cell value.
 
read more »

I got the default values on the drop-down boxes to work as well. If
anyone is interested here is the code:

**** DEFAULT.ASPX CODE SNIPPET ****
<asp:GridView ID="gvTaskDetails" runat="server"
AutoGenerateColumns="False" AutoGenerateEditButton="True"
CellPadding="4" DataKeyNames="Task ID,Task
Description,Resource,Duration,Dependent Task,Type" EmptyDataText="No
task data. Choose them on the Project Tasks tab." ForeColor="#333333"
OnSelectedIndexChanged="gvTaskDetails_SelectedIndexChanged">
<RowStyle BackColor="#E3EAEB" HorizontalAlign="Left" Wrap="False">
</RowStyle>
<EmptyDataRowStyle Wrap="False"></EmptyDataRowStyle><Columns >
<asp:BoundField DataField="Task ID" HeaderText="Task ID"
SortExpression="Task ID" ReadOnly="True" />
<asp:BoundField DataField="Task Description" HeaderText="Task
Description" SortExpression="Task Description" ReadOnly="True" />
<asp:TemplateField HeaderText="Resource"
SortExpression="Resource">
<EditItemTemplate>
<asp:DropDownList ID="ddlRes" runat="server"
DataSourceID="sqlRes" DataTextField="Resource Name"
DataValueField="Resource Name" SelectedValue='<%# Bind("Resource")
%>'>
</asp:DropDownList>
</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]") %>' Width="56px"></asp:TextBox><br />
<asp:RequiredFieldValidator ID="rfvDuration" runat="server"
ControlToValidate="txtDuration"
ErrorMessage="Enter a Duration (number of days)."
SetFocusOnError="True" Display="Dynamic"></
asp:RequiredFieldValidator><br />
<asp:RangeValidator ID="rvDuration" runat="server"
ControlToValidate="txtDuration"
ErrorMessage="Enter a number between 0 and 365 days."
MaximumValue="365"
MinimumValue="0" SetFocusOnError="True" Display="Dynamic"
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<br />
(separate multiples with comma)<br />
<asp:TextBox ID="txtDepTask" runat="server" Text='<%#
Bind("[Dependent Task]") %>' Width="224px"></asp:TextBox><br />
<asp:CustomValidator ID="cvDepTask" runat="server"
ControlToValidate="txtDepTask"
Display="Dynamic" ErrorMessage="Invalid Entry. Examples: 1 or 1,2"
</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:DropDownList ID="ddlType" runat="server"
DataSourceID="sqlTaskLink" DataTextField="Abbrev"
DataValueField="Abbrev" SelectedValue='<%# Bind("Type") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblType" runat="server" Text='<%# Bind("Type")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#1C5E55" Font-Bold="True"
ForeColor="White" Wrap="False" />
<PagerStyle BackColor="#666666" ForeColor="White"
HorizontalAlign="Center" Wrap="False" />
<SelectedRowStyle BackColor="#C5BBAF" Wrap="False" Font-
Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True"
ForeColor="White" HorizontalAlign="Center" Wrap="False" />
<EditRowStyle BackColor="Yellow" Wrap="True" />
<AlternatingRowStyle BackColor="White" Wrap="False" />
</asp:GridView>
<asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:SQLSERVER
DbConnectionString %>" ID="sqlTaskLink" ProviderName="<%$
ConnectionStrings:SQLSERVER DbConnectionString.ProviderName %>"
runat="server" SelectCommand="SELECT Abbrev FROM dbo.[PMJ TASK LINK]
ORDER BY Abbrev" />
<asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:SQLSERVER
DbConnectionString %>" ID="sqlRes" ProviderName="<%$
ConnectionStrings:SQLSERVER DbConnectionString.ProviderName %>"
runat="server" SelectCommand="SELECT [Resource Name] FROM dbo.[PMJ
RESOURCES] ORDER BY [Resource Name]" />

**** DEFAULT.ASPX.VB CODE SNIPPET****
Protected Sub gvTaskDetails_RowUpdating(ByVal sender As Object, ByVal
e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles
gvTaskDetails.RowUpdating
Dim ddlResource As DropDownList =
gvTaskDetails.Rows(e.RowIndex).Cells(3).FindControl("ddlRes") 'get the
new resource selection

Dim txtD As TextBox =
gvTaskDetails.Rows(e.RowIndex).Cells(2).FindControl("txtDuration")
'get the new duration

Dim txtDT As TextBox =
gvTaskDetails.Rows(e.RowIndex).Cells(3).FindControl("txtDepTask")
'get the new dependent task(s)

Dim ddlT As DropDownList =
gvTaskDetails.Rows(e.RowIndex).Cells(4).FindControl("ddlType")
'get the new type selection

Dim ID As Integer =
gvTaskDetails.Rows(e.RowIndex).Cells(1).Text
'select the row in the Data Table - 'set the id to the Task ID
Dim dt As System.Data.DataTable = GetCustomMadeDataTable()
Dim rows As Data.DataRow() = dt.Select("[Task ID] = " +
ID.ToString()) 'select the Task ID row
rows(0)("Resource") =
ddlResource.Text
'set the resource to the new value
rows(0)("Duration") =
txtD.Text
'set the duration to the new value
rows(0)("Dependent Task") =
txtDT.Text
'set the dependent task to the new value
rows(0)("Type") =
ddlT.Text
'set the type to the new value
gvTaskDetails.EditIndex =
-1
'done editing

BindGrid()
'show the changes by rebinding the grid
End Sub
Protected Sub gvTaskDetails_RowEditing(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.GridViewEditEventArgs) Handles
gvTaskDetails.RowEditing
gvTaskDetails.EditIndex = e.NewEditIndex
BindGrid()
End Sub
Protected Sub gvTaskDetails_RowCancelingEdit(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs)
Handles gvTaskDetails.RowCancelingEdit
e.Cancel = True
BindGrid()
End Sub
Protected Sub BindGrid()
Dim objDT As System.Data.DataTable = GetCustomMadeDataTable()
gvTaskDetails.DataSource = objDT
gvTaskDetails.DataBind()
End Sub
Protected Function GetCustomMadeDataTable() As System.Data.DataTable
Dim key As String = "TaskDetailsDataTable"
Dim dt As DataTable = Session(TaskDetKey)
Dim i As Integer, iCt As Integer

If dt Is Nothing Then
dt = New DataTable
dt.Columns.Add("Task ID", GetType(Integer))
dt.Columns.Add("Task Description", GetType(String))
dt.Columns.Add("Resource", GetType(String))
dt.Columns.Add("Duration", GetType(Integer))
dt.Columns.Add("Dependent Task", GetType(String))
dt.Columns.Add("Type", GetType(String))

For i = 0 To lstTasks.Items.Count - 1
If i = 0 Then
iCt = 1
Else
iCt = iCt + 1

End If
dt.Rows.Add(New String() {iCt, lstTasks.Items(i).Value.ToString,
"ABC Company", 0, iCt - 1, "FS"})
Next
Session(TaskDetKey) = dt

End If
Return dt
End Function
 
Back
Top