Re: What control would you use to display unbound data?

Tech-Archive recommends: Speed Up your PC by fixing your registry



On Oct 29, 10:11 am, Mel <MLights...@xxxxxxxxx> wrote:
On Oct 28, 8:15 am, Mel <MLights...@xxxxxxxxx> wrote:

On Oct 27, 2:34 pm, Mel <MLights...@xxxxxxxxx> wrote:

On Oct 24, 2:12 pm, "Mark Rae [MVP]" <m...@xxxxxxxxxxxxxxxxx> wrote:

"Mel" <MLights...@xxxxxxxxx> wrote in message

news:50393690-95c0-47bd-bffe-baa6d2d615b2@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Make sure the GridView's AutoGenerateColumns property is set to "false"

Yeah I tried that. When I set AutoGenerateColumns = False I get 6
columns with only column headings and no row data.

Hmm - OK, please post the markup for the GridView plus the code-behind...
 <asp:GridView ID="GridView1" runat="server">

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">

<asp:BoundField HeaderText="Task ID" />

<asp:BoundField DataField="Task ID" HeaderText="Task ID" />

etc

--
Mark Rae
ASP.NET MVPhttp://www.markrae.net

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."...

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"
OnServerValidate="cvDepTask_ServerValidate"></asp:CustomValidator><br /

</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
.


Quantcast