简体   繁体   中英

delete a row from the gridview from .cs

This is my aspx page

<asp:GridView ID="GridViews1" runat="server" CellPadding="4" 
    ForeColor="#333333" GridLines="None" 
    >
   <Columns>

    <asp:TemplateField HeaderText = "S.No">
         <ItemTemplate>
          <asp:Label ID="Sno" runat="server" Text= '<%#Eval("id")%>' ></asp:Label>
         </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText = "Name">
         <ItemTemplate >
         <a href= "<%# Eval("Photo") %>" > <%# Eval("name") %> </a>
         </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText  = "Photo">
         <ItemTemplate>
         <img src='<%# Eval("Photo") %>' alt='<%# Eval("Name") %>' height= "50px" width = "50px"/>
         </ItemTemplate>
         </asp:TemplateField>

    </Columns>
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />

    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#E9E7E2" />
    <SortedAscendingHeaderStyle BackColor="#506C8C" />
    <SortedDescendingCellStyle BackColor="#FFFDF8" />
    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>

and this is my .cs file

public partial class people_db_mysql : System.Web.UI.Page
{

String MyConString = "SERVER=localhost;" +
      "DATABASE=shortandsweet;" +
      "UID=root;" +
      "PASSWORD=;";
protected void Page_Load(object sender, EventArgs e)
{
    MySqlConnection conn = new MySqlConnection(MyConString);
    MySqlCommand cmd = new MySqlCommand("SELECT * FROM people_details;", conn);
    conn.Open();
    DataTable dataTable = new DataTable();
    MySqlDataAdapter da = new MySqlDataAdapter(cmd);
    da.Fill(dataTable);
    GridViews1.DataSource = dataTable;
     GridViews1.DataBind();
}}

now the output i get is

[ S.NO, Name , Photo , sno, name and photo ]

the first three fields are from the aspx page which i want it to be displayed and i dont want the other 3 fields to be displayed any ideas how i might achieve that ? and moreover ive tried this

 GridViews1.Columns[3or4or5].Visible = false;  //and it says array out of bounds 

however i can hide the 0,1,2 fields with the same command, is there a way to hide the rows generated through the .cs files ?

if i try to limit the rows through the select query it doesnt display anything, i just get a blank screen.

The Gridview control has a property 'AutoGenerateColumns' which has to be set to 'false', otherwise it will autogenerate the columns, even if you have manually added them.

<asp:GridView ID="GridViews1" runat="server" CellPadding="4" 
    ForeColor="#333333" GridLines="None" AutoGenerateColumns="false" 
    >

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM