Gridview,编辑UPdate

时间:2016-09-12 11:16:51

标签: c# sql asp.net gridview

我输出如下图像

Grid output

这是我的设计页面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();
    }

2 个答案:

答案 0 :(得分:0)

首先:添加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)
{

}

答案 1 :(得分:0)