将值从dataview传递到datatable rowcell时出错

时间:2017-11-29 05:47:52

标签: asp.net vb.net gridview

我有一个有两个gridview的页面;一个在模态弹出窗口(grdEmpName)中,另一个在页面中(gvUser)。现在,当用户选择搜索按钮时,将弹出模态弹出扩展器,然后搜索员工。当用户选择某人时,所选数据应显示在另一个gridview(gvUser)中的文本框中。

一切都在模态弹出扩展器gridview(grdEmpName)中进行选择,但是当我选择没有数据传递给其他gridview时。 (gvUser)。我需要在文本框中显示所选数据。

ASPX:

<asp:GridView ID="gvUser" runat="server" Width="1024px"
                PageSize="10"  
                AutoGenerateColumns = "False"  
                AllowSorting="true"  
                AllowPaging="true"  
                ShowFooter="True" 
                OnSorting="gvUser_Sorting"  
                OnPageIndexChanging="gvUser_PageIndexChanging"  
                OnRowCreated="gvUser_RowCreated"
                CssClass="Grid"  
                AlternatingRowStyle-CssClass="alt" 
                PagerStyle-CssClass="pgr"  
                FooterStyle-HorizontalAlign="center" > 
                <PagerSettings Mode="NumericFirstLast" PageButtonCount="5" FirstPageImageUrl="~/images/gridview/first_16x16.png" LastPageImageUrl="~/images/gridview/last_16x16.png"  />
                <Columns>
                    <asp:TemplateField> 
                        <ItemTemplate >      
                            <asp:ImageButton ID="ibtnEdit" runat="server"  ImageUrl="~/images/gridview/file_edit_24x24.png"  title = "Edit"  _CommandArgument='<%# Eval("USR_ID") & "|Edit|" &  CType(Container,GridViewRow).RowIndex & "|-" %>'  />
                            <asp:ImageButton ID="ibtnUpdate" runat="server" ImageUrl="~/images/gridview/file_save_24x24.png" visible="false" title="Update" _CommandArgument='<%# Eval("USR_ID") & "|Update|" &  CType(Container,GridViewRow).RowIndex  & "|-" %>' />  
                            <asp:ImageButton ID="ibtnCancel" runat="server" ImageUrl="~/images/gridview/file_cancel_24x24.png" Visible="false" title="Cancel" _CommandArgument='<%# Eval("USR_ID")& "|Cancel|" &  CType(Container,GridViewRow).RowIndex & "|-" %>'  /> 
                            <asp:ImageButton ID="ibtnDelete" runat="server" ImageUrl="~/images/gridview/file_delete_24x24.png" title="Delete"  _CommandArgument='<%# Eval("USR_ID")& "|Delete|" &  CType(Container,GridViewRow).RowIndex & "|" & Eval("USR_IDNTY") %>'   /> 
                            <asp:ImageButton ID="btnSearchUser" runat="server" ImageUrl="~/images/gridview/file_search_24x24.png" title="Search" _CommandArgument= "btnSearchUser" OnClick = "btnSearchUser_Click"  /> 
                        </ItemTemplate> 

                        <FooterTemplate>
                            <asp:ImageButton ID="ibtnAdd" runat="server" ImageUrl="~/images/gridview/file_add_24x24.png" title="Add" _CommandArgument='<%# Eval("USR_ID") & "|Add|" &  CType(Container,GridViewRow).RowIndex & "|-" %>' /> 

                        </FooterTemplate>

                        <HeaderStyle Wrap="False" />
                        <ItemStyle Wrap="False" Width="50px"  />            
                    </asp:TemplateField>
                <asp:TemplateField HeaderText="User Identity" visible ="false" >

                        <ItemTemplate>
                            <asp:Label ID="lblUSR_IDNTY" runat="server" Text='<%# Bind("USR_IDNTY")%>' />
                        </ItemTemplate>

                        <EditItemTemplate>
                            <asp:Label ID="lblUSR_IDNTY" runat="server" Width="95%"  Text='<%# Bind("USR_IDNTY")%>' />
                        </EditItemTemplate>  

                        <HeaderStyle Wrap="False" />
                        <ItemStyle Wrap="False" />
                 </asp:TemplateField>                        

                    <asp:TemplateField HeaderText="User ID" SortExpression="USR_ID">
                        <ItemTemplate>
                            <asp:Label ID="lblUSR_ID" runat="server" Text='<%# Bind("USR_ID")%>' />
                        </ItemTemplate>

                        <EditItemTemplate>
                            <asp:Label ID="txtUSR_ID" runat="server" Width="95%"  Text='<%# Bind("USR_ID")%>' />
                        </EditItemTemplate>  

                         <FooterTemplate>
                            <asp:TextBox ID="txtUSR_ID" runat="server" Width="95%"/>
                        </FooterTemplate>                    

                        <HeaderStyle Wrap="False" />
                        <ItemStyle Wrap="False" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Name"  SortExpression="NAME">
                        <ItemTemplate>
                            <asp:Label ID="lblNAME" runat="server" Text='<%# Bind("NAME")%>' />
                        </ItemTemplate>

                        <EditItemTemplate>
                            <asp:TextBox ID="txtNAME" runat="server" Width="95%" Text='<%# Bind("NAME")%>'/>
                        </EditItemTemplate>        

                        <FooterTemplate>
                            <asp:TextBox ID="txtNAME" runat="server" Width="95%"/>
                        </FooterTemplate>

                        <HeaderStyle Wrap="False" />
                        <ItemStyle Wrap="False" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Email"  SortExpression="EMAIL">
                        <ItemTemplate>
                            <asp:Label ID="lblEMAIL" runat="server" Text='<%# Bind("EMAIL")%>' />
                        </ItemTemplate>

                        <EditItemTemplate>
                            <asp:TextBox ID="txtEMAIL" runat="server" Width="95%"  Text='<%# Bind("EMAIL")%>'/>
                        </EditItemTemplate>        

                        <FooterTemplate>
                            <asp:TextBox ID="txtEMAIL" runat="server" Width="95%"/>
                        </FooterTemplate>

                        <HeaderStyle Wrap="False" />
                        <ItemStyle Wrap="False" />
                    </asp:TemplateField> 
                    <asp:TemplateField HeaderText="Active"  SortExpression="ACTIVE">
                        <ItemTemplate>
                             <asp:CheckBox ID="chkACTIVE"  runat="server" Checked ='<%# Bind("ACTIVE")%>' Enabled="false"   />
                        </ItemTemplate>

                        <EditItemTemplate>
                            <asp:CheckBox  ID="chkACTIVE" runat="server" Width="95%" Checked ='<%# Bind("ACTIVE")%>'/> 
                        </EditItemTemplate>        

                        <FooterTemplate>
                            <asp:CheckBox  ID="chkACTIVE" runat="server" Width="95%"/>
                        </FooterTemplate>

                        <HeaderStyle Wrap="False" />
                        <ItemStyle HorizontalAlign="Center"  Wrap="False"/> 
                    </asp:TemplateField>                                                                                                  
                </Columns>
               <EmptyDataTemplate>
                    <div style="height:315px;border-style:hidden;" >
                        <table class="table" >
                            <tr>
                                <td style="padding:5px;" >
                                    <img id="Img1" style="height:24px;width:24px; "  runat="server" src="images/no_data_64x64.png" />    
                                </td>
                                <td style="padding:5px;font-size:large;">
                                     <b>No data found.</b>
                                </td> 
                            </tr>
                        </table>
                    </div>  
                </EmptyDataTemplate>

                <PagerStyle CssClass="pgr" />
                <AlternatingRowStyle CssClass="alt"  />
                <FooterStyle HorizontalAlign="Center" />
                <EmptyDataRowStyle CssClass="EmptyData" />
              </asp:GridView>
          </div>                                        

       <%-- Picker--%>

       <cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
        </cc1:ToolkitScriptManager>


       <cc1:ModalPopupExtender   runat="server" TargetControlID="hdControl" ID="mdlEmpListProcess"  PopupControlID="empPanel" CancelControlID="btnClose"  OnCancelScript="getFlickerSolved();" >
       </cc1:ModalPopupExtender>

      <asp:HiddenField runat="server" ID="hdControl" />
      <asp:Panel runat="server" ID="empPanel"  Width="909px" Height="300"  BorderStyle="Ridge" BorderColor="Gray" Style="display:none;" BackColor="Control">

       <table>
                     <tr>
                        <td>
                            &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
                            <asp:TextBox runat="server" id="txtEmpName" CssClass="textbox" Width="240"></asp:TextBox>
                            &nbsp;<asp:Button runat="server" ID="btnSearchEmp" CssClass="button" Text="SEARCH" OnClientClick="getFlickerSolved();"/>
                                   <asp:Button runat="server" ID="btnClose" CssClass="button" Text="CLOSE" />
                        </td>
                     </tr>
       </table>   &nbsp;<br />
           &nbsp; &nbsp;
        <div style="overflow:scroll; width:882px; height:233px;"> 
                           <asp:GridView runat="server" ID="grdEmpName"   Width="863px" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" Height="35px" GridLines="Vertical">
                                <Columns>
                                        <asp:TemplateField>
                                                <ItemTemplate>
                                                        <asp:RadioButton runat="server" ID="optEmp" AutoPostBack="true" _CommandArgument= "Select" OnCheckedChanged="selectedEmployee" />
                                                </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:BoundField DataField="USR_ID" HeaderText="USER ID"/>
                                        <asp:BoundField DataField="NAME"  HeaderText="NAME"/>
                                        <asp:BoundField DataField="EMAIL" HeaderText="EMAIL ADDRESS">  
                                            <HeaderStyle Width="300px" />
                                        </asp:BoundField>        
                                </Columns>
                               <RowStyle CssClass="rowStyle" />
                               <HeaderStyle CssClass="headerStyle" BackColor="Black" Font-Bold="True" ForeColor="White" />
                               <FooterStyle BackColor="#CCCCCC" />
                               <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                               <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                               <AlternatingRowStyle BackColor="#CCCCCC" />
                          </asp:GridView>        
         </div>
          </asp:Panel>

         <%-- End Picker--%>

现在,这里可以为selectedEmployee(单选按钮)编码

Protected Sub selectedEmployee(ByVal sender As Object, ByVal e As System.EventArgs)


    Dim lnBTNDone As RadioButton = DirectCast(sender, RadioButton)
    Dim row As GridViewRow = DirectCast(lnBTNDone.NamingContainer, GridViewRow)

    Dim RowIndex As Integer = gvUser.EditIndex
    Dim dt As New DataTable
    dt = TryCast(Session("dbCache_User"), DataView).Table.Copy


    Dim dv As New DataView(dt, "", "USR_IDNTY", DataViewRowState.OriginalRows)
    Dim s As String = TryCast(gvUser.Rows(RowIndex).FindControl("lblUSR_IDNTY"), Label).Text
    Dim idx As Integer = dv.Find(s)

    dv(idx)("USR_ID") = row.Cells(1).Text
    dv(idx)("NAME") = row.Cells(2).Text
    dv(idx)("EMAIL") = row.Cells(3).Text


    gvUser.DataSource = dv
    gvUser.DataBind()

    Me.mdlEmpListProcess.Hide()

End Sub

但调试 system.data.dataexception未被用户代码处理时,我遇到此错误。传递值时无法设置名称

dv(idx)("NAME") = row.Cells(2).Text

1 个答案:

答案 0 :(得分:1)

根据你的评论NAME是在运行时给出的只读字段错误,如下所示只读错误,在读取值后在代码中添加相同的行

keyword

工作示例

long

我建议先检查一下你是否能找到行

 dt.Columns["NAME"].ReadOnly = false