如何将新行添加到数据绑定gridview?

时间:2015-04-29 14:10:54

标签: asp.net vb.net gridview

每当用户点击“添加行”按钮时,我都会尝试向网格视图添加新的空白行。理想情况下,无论他们单击此按钮哪一行,都会在该行下方插入一个新行。 我已经阅读了很多以前的问题,但我仍然无法让我的gridview显示一个新的空白行。

网页代码:

<table>
    ...
    ...
<td colspan="3" align="left" style="border: thin solid #000000; vertical-align: top; background-color: #4B6C9E">
   <asp:UpdatePanel ID="UpdatePanel1" runat="server" >
     <ContentTemplate>
        <asp:GridView ID="grdHours" runat="server" AutoGenerateColumns="False" Width="100%" CellPadding="4" 
   ForeColor="#333333" GridLines="None" >
    <AlternatingRowStyle BackColor="White" VerticalAlign="Top" Height="20px"/>
                        <Columns>
                            <asp:TemplateField ShowHeader="False" HeaderText="Task">
                                <ItemTemplate>                                                
                                    <asp:Label ID="BufferLabel" runat="server" Text="" Visible="False"></asp:Label>
                                    <asp:TextBox ID="TaskList" runat="server" CssClass="myDropDownSearch" Width="80%"
                                        onfocus="inputFocus(this)" onblur="inputBlur(this)" value="Select a Task..."></asp:TextBox>    
                                    <asp:AutoCompleteExtender ServiceMethod="SearchEmployees" 
                                        MinimumPrefixLength="3"
                                        CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
                                        TargetControlID="TaskList" 
                                        ID="AutoCompleteExtender3" runat="server" FirstRowSelected = "false">
                                    </asp:AutoCompleteExtender>
                                </ItemTemplate> 
                                <ItemStyle Width="15%" />
                            </asp:TemplateField> 
                            <asp:BoundField DataField="Weekday" HeaderText="Weekday" >
                                <HeaderStyle HorizontalAlign="Left" />
                                <ItemStyle HorizontalAlign="Left" Width="4%" />
                            </asp:BoundField>
                            <asp:BoundField DataField="Date" DataFormatString="{0:d}" HeaderText="Date" >
                                <HeaderStyle HorizontalAlign="Center" />
                                <ItemStyle HorizontalAlign="Center" Width="8%" />
                            </asp:BoundField>
                            <asp:TemplateField HeaderText="Hours">
                                <ItemTemplate>
                                    <asp:Label ID="lblHours" Visible='<%# NOT IsInEditMode %>' runat="server" Text='<%# Eval("Hours") %>' CssClass="hoursLabel" />
                                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Hours") %>' 
                                        Width="90%" Visible='<%# IsInEditMode %>'>
                                    </asp:TextBox>
                                </ItemTemplate>
                                <HeaderStyle HorizontalAlign="Center" />
                                <ItemStyle Width="5%" HorizontalAlign="Center" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Minutes">
                                <ItemTemplate>
                                    <asp:Label ID="lblMins" Visible='<%# NOT IsInEditMode %>' runat="server" Text='<%# Eval("Minutes") %>' CssClass="hoursLabel" />
                                    <asp:DropDownList ID="DropDownList2" runat="server" CssClass="myDropDown" selectedValue='<%# Eval("Minutes") %>'  Visible='<%# IsInEditMode %>' >
                                        <asp:ListItem></asp:ListItem>
                                        <asp:ListItem>0</asp:ListItem>
                                        <asp:ListItem>15</asp:ListItem>
                                        <asp:ListItem>30</asp:ListItem>
                                        <asp:ListItem>45</asp:ListItem>
                                    </asp:DropDownList>
                                </ItemTemplate>
                                <HeaderStyle HorizontalAlign="Center" />
                                <ItemStyle Width="5%" HorizontalAlign="Center" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Activity" >
                                <ItemTemplate>
                                    <asp:Label ID="lblActivity" Visible='<%# NOT IsInEditMode %>' runat="server" Text='<%# Eval("Activity") %>' CssClass="hoursLabel" />
                                    <asp:DropDownList ID="DropDownList1" runat="server" width="75%" selectedValue='<%# Eval("Activity") %>' Visible='<%# IsInEditMode %>'>
                                        <asp:ListItem Selected="True">Select Activity</asp:ListItem>  
                                        <asp:ListItem></asp:ListItem>
                                        <asp:ListItem>Prescope</asp:ListItem>
                                        <asp:ListItem>Scope</asp:ListItem>
                                        <asp:ListItem>Design/Build Configuration</asp:ListItem>
                                        <asp:ListItem>Testing</asp:ListItem>
                                        <asp:ListItem>Training</asp:ListItem>
                                        <asp:ListItem>Implementation/Validation</asp:ListItem>
                                        <asp:ListItem>Maintenance & Ongoing Support</asp:ListItem>                    
                                        <asp:ListItem>Project Management</asp:ListItem>
                                        <asp:ListItem>Shared Time</asp:ListItem>                
                                   </asp:DropDownList>
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" visible="false" ControlToValidate="DropDownList1"></asp:RequiredFieldValidator>
                                </ItemTemplate>
                                <HeaderStyle HorizontalAlign="Center" />
                                <ItemStyle Width="20%" HorizontalAlign="Center" />
                            </asp:TemplateField>
                            <asp:TemplateField ShowHeader="False" HeaderText="AddEntries">
                                <ItemTemplate>
                                    <asp:Button ID="btnHoursRow" runat="server" Text="+" Visible='<%# IsInEditMode %>' OnClick="btnNewEntryRow_Click" />
                                </ItemTemplate>
                                <ItemStyle Width="5%" />
                            </asp:TemplateField>
                            <asp:TemplateField ShowHeader="False" HeaderText="AddNotes">
                                <ItemTemplate>
                                    <asp:Label ID="lblNotes" Visible='<%# NOT IsInEditMode %>' runat="server"  CssClass="hoursLabel" />
                                    <asp:Button ID="btnAddNotes" runat="server" Text="Add Notes" Visible='<%# IsInEditMode %>' />
                                    <asp:ModalPopupExtender ID="btnAddNotes_ModalPopupExtender" runat="server" 
                                        DynamicServicePath="" Enabled="True" PopupControlID="pnlNotes" 
                                        TargetControlID="btnAddNotes" 
                                        OkControlID="btnNotesDone">
                                    </asp:ModalPopupExtender>
                                </ItemTemplate>
                                <ItemStyle Width="5%" />
                            </asp:TemplateField>
                            <asp:TemplateField ShowHeader="False"  HeaderText="Validator">
                                <ItemTemplate>            
                                        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Whole Numbers Only" ControlToValidate="TextBox1" ValidationGroup="HoursasIntegers"
                                            ValidationExpression="\d+" SetFocusOnError="False" Width="75%" Font-Bold="True" ForeColor="Red" Display="Dynamic"></asp:RegularExpressionValidator>                                 
                                </ItemTemplate> 
                                <ItemStyle Width="10%" />
                            </asp:TemplateField> 
                            <asp:TemplateField ShowHeader="False" HeaderText="ErrMessage">
                                <ItemTemplate>                                                
                                    <asp:Label ID="ActivityErrorMessage" runat="server" Text="You must select an Activity for entered hours." Visible="False" ForeColor="#FF0000" Font-Bold="True"></asp:Label>
                                </ItemTemplate> 
                                <ItemStyle Width="19%" />
                            </asp:TemplateField> 
                            <asp:TemplateField  HeaderText="TimeNotes">
                                <ItemTemplate>
                                    <asp:Panel ID="pnlNotes" runat="server" Visible='<%# IsInEditMode %>' Width="75%" BackColor="#4B6C9E" Height="300px">
                                        <table width="100%">
                                            <tr>
                                                <td>
                                                    <asp:Label ID="Label1" runat="server" Text="Enter Notes Below:" ForeColor="White" Font-Size="Medium"></asp:Label>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td align="center">
                                                    <asp:TextBox ID="txtNotes" runat="server" CssClass="myTextbox"
                                                        TextMode="MultiLine"  Text='<%# Eval("TimeNotes") %>' Height="200"></asp:TextBox>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <asp:Button ID="btnNotesDone" runat="server" Text="Done" />
                                                    <asp:Button ID="btnNotesCancel" runat="server" Text="Cancel" OnClick="btnNotesCancel_Click"  Visible="False" />
                                                </td>
                                            </tr>
                                        </table>
                                    </asp:Panel>
                                </ItemTemplate>
                                <ItemStyle Width="4%" />
                            </asp:TemplateField>
                        </Columns>
                        <EditRowStyle BackColor="#2461BF" />
                        <FooterStyle BackColor="White" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                        <RowStyle BackColor="#EFF3FB" Height="20px" VerticalAlign="Top" />
                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                        <SortedAscendingCellStyle BackColor="#F5F7FB" />
                        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                        <SortedDescendingCellStyle BackColor="#E9EBEF" />
                        <SortedDescendingHeaderStyle BackColor="#4870BE" />
                    </asp:GridView>
                    </ContentTemplate>
                    </asp:UpdatePanel>
                 </td>

代码背后:

    Protected Sub btnNewEntryRow_Click(ByVal sender As Object, ByVal e As EventArgs)

            '    Dim clickedRow = TryCast(DirectCast(sender, Button).NamingContainer, GridViewRow)
            '    Dim clickedIndex = clickedRow.RowIndex

        'Just hard coded a value for now
            BindGrdHours(3)

    End Sub

  Private Sub BindGrdHours(ByVal rowIndex As Integer)

        Dim rowIndex As Integer = 0

        If ViewState("CurrentGridTable") IsNot Nothing Then

            Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentGridTable"), DataTable)
            Dim drCurrentRow As DataRow = Nothing

            If dtCurrentTable.Rows.Count > 0 Then
                For i As Integer = 1 To dtCurrentTable.Rows.Count

                    Dim TaskList As TextBox = DirectCast(grdHours.Rows(rowIndex).Cells(1).FindControl("TaskList"), TextBox)
                    'Dim TextBoxAge As BoundField
                    'TextBoxAge.DataField = grdHours.Rows(rowcount).Cells(2).FindControl("Weekday").ToString
                    'Dim TextBoxAddress As BoundField
                    'TextBoxAddress.DataField = grdHours.Rows(rowcount).Cells(3).FindControl("Date").ToString
                    Dim Hours As TextBox = DirectCast(grdHours.Rows(rowIndex).Cells(4).FindControl("TextBox1"), TextBox)
                    Dim Minutes As DropDownList = DirectCast(grdHours.Rows(rowIndex).Cells(5).FindControl("DropDownList2"), DropDownList)
                    Dim Activity As DropDownList = DirectCast(grdHours.Rows(rowIndex).Cells(6).FindControl("DropDownList1"), DropDownList)
                    Dim AddEntries As Button = DirectCast(grdHours.Rows(rowIndex).Cells(7).FindControl("btnHoursRow"), Button)
                    drCurrentRow = dtCurrentTable.NewRow()
                    ' drCurrentRow("RowNumber") = i + 1

                    dtCurrentTable.Rows(i - 1)("Task") = TaskList.Text.ToString
                    dtCurrentTable.Rows(i - 1)("Weekday") = grdHours.Rows(rowIndex).Cells(2).Text.ToString
                    dtCurrentTable.Rows(i - 1)("Date") = grdHours.Rows(rowIndex).Cells(3).Text.ToString
                    dtCurrentTable.Rows(i - 1)("Hours") = Hours.Text.ToString
                    dtCurrentTable.Rows(i - 1)("Minutes") = Minutes.SelectedValue.ToString
                    dtCurrentTable.Rows(i - 1)("Activity") = Activity.SelectedValue.ToString
                    dtCurrentTable.Rows(i - 1)("AddEntries") = AddEntries.Text.ToString
                    dtCurrentTable.Rows(i - 1)("AddNotes") = String.Empty
                    dtCurrentTable.Rows(i - 1)("Validator") = String.Empty
                    dtCurrentTable.Rows(i - 1)("ErrMessage") = String.Empty
                    dtCurrentTable.Rows(i - 1)("TimeNotes") = String.Empty
                    rowIndex += 1
                Next

                dtCurrentTable.Rows.Add(drCurrentRow)
                ViewState("CurrentGridTable") = dtCurrentTable

                grdHours.DataSource = dtCurrentTable
                grdHours.DataBind()

                Dim txn As TextBox = DirectCast(grdHours.Rows(rowIndex).Cells(1).FindControl("TextBox1"), TextBox)
                ' txn.Focus;
                txn.Focus()

            End If

        End If

        enterHours(True)

    End Sub

当我单击按钮时,正在调用btnNewEntryRow_Click函数,该函数又成功调用BindGrdHours(),但在完成后,没有任何事情发生。屏幕仍然包含原始gridview。

关于我哪里出错的任何想法?谢谢你的期待。

0 个答案:

没有答案