Gridview编辑,更新,删除和选择事件

时间:2017-08-07 17:34:32

标签: c# asp.net events gridview templatefield

我有一个下拉列表,该列表由数据库中表的名称填充。然后当管理员选择或单击表名时。下面的gridview由表中的数据填充。管理员应该能够编辑,更新,删除和选择。但是,gridview提供的命令不起作用。单击编辑时,它不会执行。然后当您单击“选择”时,它将删除。这些命令在某个地方混淆了。所以我将命令更改为模板,以便我自己编写代码。然而,网上找到的解决方案不起作用。因为一个

  1. Gridview不仅显示来自一个表的数据,而且显示多个,因此我不能在我的查询中具体删除某个表的行,但只是说我想删除表中显示的行。这适用于所有其他事件

  2. 类似editing GridView1.EditIndex = e.NewEditIndex的代码不起作用。它显示

  3.   

    System.EventArgs错误。 NewEditIndex没有扩展方法,而且是   不接受System.EventArgs类型的第一个参数可能是   找到。

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;
    using System.Linq;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings ["tlcString"].ConnectionString);
    
        protected void DropDownList1_SelectedIndexChanged (object sender, EventArgs e)
        {
        con.Open ();
        //Populates the gridview with the selectedindex in the list. The selectedindex being the table
        SqlDataAdapter da = new SqlDataAdapter(string.Format ("Select * From {0}", drpTable.SelectedValue), con);
        DataSet ds = new DataSet ();
    da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
        da.Fill(ds);
    
        GridView1.DataSource = ds. Tables [0];
        GridView1.DataBind ();
        con.Close ();
        }
    
        private void BindData ()
        {
        //Binds the data in the gridview with the database table and updating it if changes are ever made. The method is called.
    
        string strQuery = "Select * From {0}";
        SqlCommand cmd = new SqlCommand  (strQuery);
        GridView1.DataSource = GetData (cmd);
        GridView1.DataBind ();
        }
    
        private object GetData (SqlCommand cmd)
        {
        throw new NotImplentedException ();
        }
    
        //this is the code for the commads after l turned them into templatefields. But l only wrote code for the edit button only. It would be appreaciated if code for the other commands is supplied
    
        protected void LinkButton1_Click (object sender, EventArgs e)
        {
        //Set edit index
        GridView1.EditIndex = e.NewEditIndex;
        BindData ();
        }
    //THATS all my code. The commands that were acting up were auto generated in the gridview properties window. 
    
    
    
    
    <asp:GridView ID="GridView1" runat="server" CellPadding="4" 
    ForeColor="#333333" 
    GridLines="None">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
    <asp:TemplateField ShowHeader="False">
    <EditItemTemplate>
    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
    CommandName="Update" Text="Update"></asp:LinkButton>
    &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" 
    CausesValidation="False" 
    CommandName="Cancel" Text="Cancel"></asp:LinkButton>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
    CommandName="Edit" Text="Edit"></asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField ShowHeader="False">
    <ItemTemplate>
    <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
    CommandName="Select" Text="Select"></asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField ShowHeader="False">
    <ItemTemplate>
    <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" 
    CommandName="Delete" Text="Delete"></asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    <EditRowStyle BackColor="#2461BF" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" 
    />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" 
    />
    <SortedAscendingCellStyle BackColor="#F5F7FB" />
    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
    <SortedDescendingCellStyle BackColor="#E9EBEF" />
    <SortedDescendingHeaderStyle BackColor="#4870BE" /></asp:GridView>
    

0 个答案:

没有答案