1

Insert、Edit、およびDelete機能を備えたGridViewを作成しました。挿入はうまく機能しますが、削除と編集は機能しません。[削除] をクリックすると、行が自動的に削除されると思われますが、まだ存在するため発生しません。編集と同じ2番目の問題、[編集]をクリックしてから、そこで何かを変更してから[更新]をクリックします。クリックしても何も変更されず、GridViewでもデータは同じです。私は小さなものを逃していると確信しています...助けてください!

ここでは、HTML の背後にある C# コード、

protected void GridViewServer_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridViewServer.EditIndex = e.NewEditIndex;
        BindGridView();          

    }

    protected void GridViewServer_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridViewServer.EditIndex = -1;
        BindGridView();           
    }

    protected void GridViewServer_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {           
        GridViewRow _row = GridViewServer.Rows[e.RowIndex];
        Label servername = (Label)_row.FindControl("lblServerName");

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database_Shared_NotebookConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand("delete from tblServer where ServerName = '" + servername + "'", conn);
        cmd.CommandType = CommandType.Text;
        conn.Open();
        cmd.ExecuteNonQuery();

        GridViewServer.EditIndex = -1;
        BindGridView();
        conn.Close();

    }

    protected void GridViewServer_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {


        GridViewRow _row = GridViewServer.Rows[e.RowIndex];
        Label servername = (Label)_row.FindControl("lblServerName");
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database_Shared_NotebookConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand("update tblServer set ClusterName = @ClusterName, Architecture = @Architecture, Database_Type = @DataType, Restriction = @Restriction, Business_Continuity_Plan = @BCP, Tenant_Code = @TenantCode where ClusterName =  '" + servername + "'", conn);
        cmd.CommandType = CommandType.Text;
        //cmd.Parameters.AddWithValue("@ServerName", ((Label)_row.FindControl("lblServerName")).Text);
        cmd.Parameters.AddWithValue("@ClusterName", ((DropDownList)_row.FindControl("DropDownClusterName")).Text);
        cmd.Parameters.AddWithValue("@Architecture", ((DropDownList)_row.FindControl("DropDownArchitecture")).Text);
        cmd.Parameters.AddWithValue("@DataType", ((DropDownList)_row.FindControl("DropDownDataType")).Text);
        cmd.Parameters.AddWithValue("@Restriction", ((DropDownList)_row.FindControl("DropDownRestriction")).Text);
        cmd.Parameters.AddWithValue("@BCP", ((TextBox)_row.FindControl("txtBCP")).Text);
        cmd.Parameters.AddWithValue("@TenantCode", ((TextBox)_row.FindControl("txtTenantCode")).Text);

        conn.Open();
        cmd.ExecuteNonQuery();

        GridViewServer.EditIndex = -1;
        BindGridView();
        conn.Close();

    }

public void BindGridView()
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database_Shared_NotebookConnectionString"].ConnectionString);

        conn.Open();

        string sqlquery = ("SELECT * FROM tblServer");

        SqlCommand command = new SqlCommand(sqlquery, conn);

        SqlDataAdapter adp = new SqlDataAdapter(command);

        DataSet ds = new DataSet();

        adp.Fill(ds);

        GridViewServer.DataSource = ds.Tables[0];

        GridViewServer.DataBind();
    }

表示する必要がある場合は、GridView HTML コード、

<asp:GridView ID="GridViewServer" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="ServerName" GridLines="None" ShowFooter="True" onrowcancelingedit="GridViewServer_RowCancelingEdit" 
        onrowdeleting="GridViewServer_RowDeleting" onrowediting="GridViewServer_RowEditing" 
        onrowupdating="GridViewServer_RowUpdating">
        <Columns>               
            <asp:TemplateField HeaderText="ServerName" SortExpression="ServerName">
                <ItemTemplate>
                    <asp:Label ID="lblServerName" runat="server" Text='<%# Eval("ServerName") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Label ID="txtServerName" runat="server" Text='<%# Eval("ServerName") %>'></asp:Label>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtServerName" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvServerName" runat="server" ControlToValidate="txtServerName" ErrorMessage="Required" ForeColor="Red"></asp:RequiredFieldValidator>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="ClusterName" SortExpression="ClusterName">
                <ItemTemplate>
                    <asp:Label ID="lblClusterName" runat="server" Text='<%# Eval("ClusterName") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownClusterName" runat="server" AutoPostBack="True" DataSourceID="ClusterName" DataTextField="ClusterName" DataValueField="ClusterName">
                    </asp:DropDownList>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="DropDownClusterName" runat="server" DataSourceID="ClusterName" DataTextField="ClusterName" DataValueField="ClusterName">
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="ClusterName" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [ClusterName] FROM [tblCluster]"></asp:SqlDataSource>                       
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Architecture" SortExpression="Architecture">
                <ItemTemplate>
                    <asp:Label ID="lblArchitecture" runat="server" Text='<%# Eval("Architecture") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownArchitecture" runat="server">
                        <asp:ListItem Selected="True"></asp:ListItem>
                        <asp:ListItem>BSN EAP</asp:ListItem>
                        <asp:ListItem>BSN NEA</asp:ListItem>
                        <asp:ListItem>BSNConnect.com</asp:ListItem>
                        <asp:ListItem>BSNDMZ.COM</asp:ListItem>
                        <asp:ListItem>Dow EAP</asp:ListItem>
                        <asp:ListItem>Dow NEA</asp:ListItem>
                     </asp:DropDownList>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="DropDownArchitecture" runat="server">
                        <asp:ListItem Selected="True"></asp:ListItem>
                        <asp:ListItem>BSN EAP</asp:ListItem>
                        <asp:ListItem>BSN NEA</asp:ListItem>
                        <asp:ListItem>BSNConnect.com</asp:ListItem>
                        <asp:ListItem>BSNDMZ.COM</asp:ListItem>
                        <asp:ListItem>Dow EAP</asp:ListItem>
                        <asp:ListItem>Dow NEA</asp:ListItem>
                     </asp:DropDownList>
                     <asp:RequiredFieldValidator ID="rfvArchitecture" runat="server" ControlToValidate="DropDownArchitecture" ErrorMessage="Required" ForeColor="Red"></asp:RequiredFieldValidator>
                 </FooterTemplate>
             </asp:TemplateField>
            <asp:TemplateField HeaderText="Database_Type" SortExpression="Database_Type">
                <ItemTemplate>
                    <asp:Label ID="lblDatabaseType" runat="server" Text='<%# Eval("Database_Type") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownDataType" runat="server" >
                        <asp:ListItem Selected="True"></asp:ListItem>
                        <asp:ListItem>Oracle</asp:ListItem>
                        <asp:ListItem>SQL Server</asp:ListItem>
                     </asp:DropDownList>
                </EditItemTemplate>
                <FooterTemplate>
                     <asp:DropDownList ID="DropDownDataType" runat="server" AutoPostBack="True">
                        <asp:ListItem Selected="True"></asp:ListItem>
                        <asp:ListItem>Oracle</asp:ListItem>
                        <asp:ListItem>SQL Server</asp:ListItem>
                     </asp:DropDownList>
                     <asp:RequiredFieldValidator ID="rfvDatabaseType" runat="server" ControlToValidate="DropDownDataType" ErrorMessage="Required" ForeColor="Red"></asp:RequiredFieldValidator>
                 </FooterTemplate>
             </asp:TemplateField>
            <asp:TemplateField HeaderText="Life_Cycle_Stage" SortExpression="Life_Cycle_Stage">
                <ItemTemplate>
                    <asp:Label ID="lblLCS" runat="server" Text='<%# Eval("Life_Cycle_Stage") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownLCS" runat="server">
                        <asp:ListItem Selected="True"></asp:ListItem>
                        <asp:ListItem>Dev</asp:ListItem>
                        <asp:ListItem>Prod</asp:ListItem>
                        <asp:ListItem>Test</asp:ListItem>
                     </asp:DropDownList>
                </EditItemTemplate>
                <FooterTemplate>
                     <asp:DropDownList ID="DropDownLCS" runat="server">
                        <asp:ListItem Selected="True"></asp:ListItem>
                        <asp:ListItem>Dev</asp:ListItem>
                        <asp:ListItem>Prod</asp:ListItem>
                        <asp:ListItem>Test</asp:ListItem>
                     </asp:DropDownList>
                     <asp:RequiredFieldValidator ID="rfvLCS" runat="server" ControlToValidate="DropDownLCS" ErrorMessage="Required" ForeColor="Red"></asp:RequiredFieldValidator>
                 </FooterTemplate>
             </asp:TemplateField>
            <asp:TemplateField HeaderText="Restriction" SortExpression="Restriction">
                <ItemTemplate>
                    <asp:Label ID="lblRestriction" runat="server" Text='<%# Eval("Restriction") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownRestriction" runat="server">
                        <asp:ListItem Selected="True"></asp:ListItem>
                        <asp:ListItem>Export Controlled</asp:ListItem>
                        <asp:ListItem>SOX Critical</asp:ListItem>
                        <asp:ListItem>Standard</asp:ListItem>
                     </asp:DropDownList>
                </EditItemTemplate>
                <FooterTemplate>
                     <asp:DropDownList ID="DropDownRestriction" runat="server">
                        <asp:ListItem Selected="True"></asp:ListItem>
                        <asp:ListItem>Export Controlled</asp:ListItem>
                        <asp:ListItem>SOX Critical</asp:ListItem>
                        <asp:ListItem>Standard</asp:ListItem>
                     </asp:DropDownList>
                     <asp:RequiredFieldValidator ID="rfvRestriction" runat="server" ControlToValidate="DropDownRestriction" ErrorMessage="Required" ForeColor="Red"></asp:RequiredFieldValidator>
                 </FooterTemplate>
             </asp:TemplateField>
            <asp:TemplateField HeaderText="Business_Continuity_Plan" SortExpression="Business_Continuity_Plan">
                <ItemTemplate>
                    <asp:Label ID="lblBCP" runat="server" Text='<%# Eval("Business_Continuity_Plan") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtBCP" runat="server" Text='<%# Eval("Business_Continuity_Plan") %>'></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                     <asp:TextBox ID="txtBCP" runat="server"></asp:TextBox>
                     <asp:RequiredFieldValidator ID="rfvBCP" runat="server" ControlToValidate="txtBCP" ErrorMessage="Required" ForeColor="Red"></asp:RequiredFieldValidator>
                 </FooterTemplate>
             </asp:TemplateField>
            <asp:TemplateField HeaderText="Tenant_Code" SortExpression="Tenant_Code">
                <ItemTemplate>
                    <asp:Label ID="lblTenantCode" runat="server" Text='<%# Eval("Tenant_Code") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtTenantCode" runat="server" Text='<%# Eval("Tenant_Code") %>'></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                     <asp:TextBox ID="txtTenantCode" runat="server"></asp:TextBox>
                     <asp:RequiredFieldValidator ID="rfvTenantCode" runat="server" ControlToValidate="txtTenantCode" ErrorMessage="Required" ForeColor="Red"></asp:RequiredFieldValidator>
                 </FooterTemplate>
             </asp:TemplateField>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" CausesValidation="false" />
            <asp:TemplateField HeaderText="" SortExpression="Insert">                  
                <FooterTemplate>
                    <asp:LinkButton ID="lnkInsert" runat="server" Text ="Insert" OnClick="lnkInsert_Click"></asp:LinkButton>
                    </FooterTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="White" ForeColor="#000066" />
        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
        <EditRowStyle BackColor="White" />
        <RowStyle ForeColor="#000066" />
        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#007DBB" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#00547E" />
    </asp:GridView>
4

1 に答える 1

0

Labelプロパティの代わりにを使用していLabel.Textます。

したがって、0行を編集/削除しています。コードは次のとおりです。

    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database_Shared_NotebookConnectionString"].ConnectionString);
    SqlCommand cmd = new SqlCommand("delete from tblServer where ServerName = '" + servername.Text + "'", conn);

    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database_Shared_NotebookConnectionString"].ConnectionString);
    SqlCommand cmd = new SqlCommand("update tblServer set ClusterName = @ClusterName, Architecture = @Architecture, Database_Type = @DataType, Restriction = @Restriction, Business_Continuity_Plan = @BCP, Tenant_Code = @TenantCode where ClusterName =  '" + servername.Text + "'", conn);

が変更されたことに注意してくださいservername.Text

于 2012-11-15T21:02:58.527 に答える