网格视图,编辑更新

本文关键字:更新 编辑 视图 网格 | 更新日期: 2023-09-27 18:06:34

我的输出如下图

网格输出

这是我设计页面的HTML来源:

  <form id="form1" runat="server">
        <asp:ScriptManager ID="script1" runat="server"></asp:ScriptManager>
    <div>
        <table align="center" style="width:50%;">
            <tr>
                <td class="auto-style1">
                    <asp:Label ID="Label1" runat="server" Text="Country Name" Font-Bold="True" ForeColor="Red"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="Text1" runat="server" Width="180px"></asp:TextBox>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style1">
                    <asp:Label ID="Label2" runat="server" Text="Country Notes" Font-Bold="True" ForeColor="Red"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="Text2" runat="server" Width="180px"></asp:TextBox>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>
                    <br />
                    <asp:Button ID="Button1" runat="server" Text="Add" BackColor="#990000" ForeColor="White" OnClick="Button1_Click" />
                </td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
        </table>
        <br />
        <br />
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
               <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="PageIndexChanging" />
                </Triggers>
        <ContentTemplate>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="CountryID" DataSourceID="SqlDataSource1"  >
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                <asp:BoundField DataField="CountryID" HeaderText="CountryID" ReadOnly="True" SortExpression="CountryID" InsertVisible="False" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="CountryNotes" HeaderText="CountryNotes" SortExpression="CountryNotes" />
            </Columns>
            <FooterStyle BackColor="White" ForeColor="#000066" />
            <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
            <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>
            </ContentTemplate>
    </asp:UpdatePanel>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ATSConnectionString %>" DeleteCommand="DELETE FROM [country1] WHERE [CountryID] = @original_CountryID AND (([Name] = @original_Name) OR ([Name] IS NULL AND @original_Name IS NULL)) AND (([CountryNotes] = @original_CountryNotes) OR ([CountryNotes] IS NULL AND @original_CountryNotes IS NULL))" InsertCommand="INSERT INTO [country1] ([Name], [CountryNotes]) VALUES (@Name, @CountryNotes)" SelectCommand="SELECT * FROM [country1]" UpdateCommand="UPDATE [country1] SET [Name] = @Name, [CountryNotes] = @CountryNotes WHERE [CountryID] = @original_CountryID AND (([Name] = @original_Name) OR ([Name] IS NULL AND @original_Name IS NULL)) AND (([CountryNotes] = @original_CountryNotes) OR ([CountryNotes] IS NULL AND @original_CountryNotes IS NULL))" ConflictDetection="CompareAllValues" OldValuesParameterFormatString="original_{0}">
            <DeleteParameters>
                <asp:Parameter Name="original_CountryID" Type="Int32" />
                <asp:Parameter Name="original_Name" Type="String" />
                <asp:Parameter Name="original_CountryNotes" Type="String" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="CountryNotes" Type="String" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="CountryNotes" Type="String" />
                <asp:Parameter Name="original_CountryID" Type="Int32" />
                <asp:Parameter Name="original_Name" Type="String" />
                <asp:Parameter Name="original_CountryNotes" Type="String" />
            </UpdateParameters>
        </asp:SqlDataSource>
</div>

当我点击编辑按钮在gridview的任何一行,我应该得到值的文本框分别在页面顶部和添加按钮必须更改为更新按钮,我不应该得到编辑文本框在gridview的行,谁能帮助这个问题

这是我的程序

public partial class ACT : System.Web.UI.Page
{
    SqlConnection con;
    SqlDataAdapter adapter;
    DataSet ds;
    SqlCommand cmd;
    public void Page_Load(object sender, EventArgs e)
    {
       if(!IsPostBack)
       {
           fillDataGrid();
       }
    }
    public void fillDataGrid()
    {
        con = new SqlConnection("Data Source=GARGI003-PC;Initial Catalog=ATS;Integrated Security=True");
        cmd = new SqlCommand("select * from country1", con);
        con.Open();
        adapter = new SqlDataAdapter(cmd);
        ds = new DataSet();
        adapter.Fill(ds, "country1");
        GridView1.DataBind();
        con.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        con = new SqlConnection("Data Source=GARGI003-PC;Initial Catalog=ATS;Integrated Security=True");
        cmd = new SqlCommand("insert into country1 (Name,CountryNotes) values(@Name, @CountryNotes)", con);
            cmd.Parameters.AddWithValue("@Name", Text1.Text);
            cmd.Parameters.AddWithValue("@CountryNotes", Text2.Text);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            if (IsPostBack)
            {
                Text1.Text = "";
                Text2.Text = "";
                fillDataGrid();                   
            }
    }
    protected void datagridview_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        fillDataGrid();
    }

网格视图,编辑更新

第一:添加Gridview,也可以添加DataKeyNames并将您的BoundField转换为TemplateField

OnSelectedIndexChanging="GridView1_SelectedIndexChanging"
onRowEditing ="GridView1_Editing"
OnRowDeleting="GridView1_Deleting"

你的Gridview是

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="CountryID" DataSourceID="SqlDataSource1" 
    OnSelectedIndexChanging="GridView1_SelectedIndexChanging"
        onrowEditing ="GridView1_Editing" OnRowDeleting="GridView1_Deleting" DataKeyNames="ID">

删除命令字段按钮并添加

<asp:TemplateField HeaderText="Action" HeaderStyle-Width="5%" HeaderStyle-ForeColor="#004c98">
                                                                        <ItemTemplate>
                                                                            <asp:ImageButton ID="EditButtonVM" runat="server" ImageUrl="~/Images/icon-edit.png"
                                                                                CommandName="Edit" Text="Edit" CausesValidation="false" ToolTip="Edit"></asp:ImageButton>
                                                                            <asp:ImageButton ID="linkbtnDeleteVM" runat="server" OnClientClick="javascript:return confirm('Do you want to Delete Record?');"
                                                                                CommandName="Delete" Text="Delete" ImageUrl="~/Images/icon-delete.png" ToolTip="Delete"></asp:ImageButton>
</ItemTemplate>
                                                                    </asp:TemplateField>

c#编程:在这里找到标签控件并为您的文本框赋值

删除
protected void Gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
 {
   // YOur Code
}
编辑

protected void Gridview1_RowEditing(object sender, GridViewEditEventArgs e)
  {
  }

选定索引

protected void Gridview1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
}

Karthikeyan Muthukumaran,点击链接。

https://blogs.msdn.microsoft.com/codefx/2009/09/06/how-to-implement-insert-edit-delete-paging-and-sorting-functions-in-an-asp-net-gridview-control/

http://www.webcodeexpert.com/2013/07/bindsaveeditupdatecanceldeletepaging.html

http://www.aspsnippets.com/Articles/Simple-Insert-Select-Edit-Update-and-Delete-in-ASPNet-GridView-control.aspx

http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html

http://www.aspneto.com/add-insert-edit-update-delete-show-gridview-data-in-asp-net-c-vb.html