I have a dropdownlist that is populated by the names of the tables in the database. Then when the admin chooses or clicks a table name. The gridview below is populated by the data in the table. The admin should be able to edit, update, delete and select. However the commands supplied by the gridview are not working. When you click edit it does not execute. Then when you click select it deletes. The commands have a mix up somewhere. So l changed the commands into templates so l can code them myself. However the solutions l found online do not work. Because one
The Gridview does not only show data from one table but multiple so l cannot be specific in my query that l want delete the row of a certain table but just say l want to delete the row of the table showing. This goes for all the other events
Code for like
editing GridView1.EditIndex = e.NewEditIndexdoes not work. It displays a
System.EventArgs error. NewEditIndex has no extension method and is not accepting a first argument of type System.EventArgs could be found.
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>
<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>