Hi makenzi.exc
Please refere the below example.
HTML
<asp:GridView ID="gvVegetables" runat="server" AutoGenerateColumns="false" AllowPaging="true"
OnPageIndexChanging="OnPageIndexChanging" PageSize="4" OnSelectedIndexChanged="OnSelectedIndexChanged">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="PricePerKg" HeaderText="PricePerKg" />
<asp:ButtonField Text="Select" CommandName="Select" />
</Columns>
</asp:GridView>
<br />
<br />
<table>
<tr>
<td>Id:</td>
<td>
<asp:Label ID="lblId" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>Name:</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>PricePerKg:</td>
<td>
<asp:TextBox ID="txtPricePerKg" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button runat="server" Text="Update" OnClick="OnUpdate" />
<asp:Button runat="server" Text="Delete" OnClick="OnDelete" /></td>
</tr>
</table>
Database
CREATE TABLE [Vegetables](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL,
[PricePerKg] [varchar](50) NULL,
CONSTRAINT [PK_Vegetables] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
Namespace
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
Code
#region Events
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
protected void OnDelete(object sender, EventArgs e)
{
//Reading the value.
int id = int.Parse(lblId.Text);
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection sqlConnection = new SqlConnection(constr))
{
using (SqlCommand sqlCommand = new SqlCommand("DELETE FROM Vegetables WHERE Id = @Id", sqlConnection))
{
//Passing the parameter with value.
sqlCommand.Parameters.AddWithValue("@Id", id);
//Opening the sql connection.
sqlConnection.Open();
//Executing sql query.
sqlCommand.ExecuteNonQuery();
//Closing the sql connection.
}
sqlConnection.Close();
}
this.BindGrid();
}
protected void OnUpdate(object sender, EventArgs e)
{
//Reading the values.
int id = int.Parse(lblId.Text);
string name = txtName.Text.Trim();
string pricePerkg = txtPricePerKg.Text.Trim();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection sqlConnection = new SqlConnection(constr))
{
using (SqlCommand sqlCommand = new SqlCommand("UPDATE Vegetables SET Name = @Name, PricePerKg = @PricePerKg WHERE Id = @Id", sqlConnection))
{
//Passing the parameters with values.
sqlCommand.Parameters.AddWithValue("@Id", id);
sqlCommand.Parameters.AddWithValue("@Name", name);
sqlCommand.Parameters.AddWithValue("@PricePerKg", pricePerkg);
//Opening the sql connection.
sqlConnection.Open();
//Executing sql query.
sqlCommand.ExecuteNonQuery();
//Closing the sql connection.
sqlConnection.Close();
}
}
this.BindGrid();
}
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvVegetables.PageIndex = e.NewPageIndex;
this.BindGrid();
}
protected void OnSelectedIndexChanged(object seneder, EventArgs e)
{
//Reading the values.
int id = int.Parse(gvVegetables.SelectedRow.Cells[0].Text);
string name = gvVegetables.SelectedRow.Cells[1].Text;
string pricePerKg = gvVegetables.SelectedRow.Cells[2].Text;
lblId.Text = id.ToString();
txtName.Text = name;
txtPricePerKg.Text = pricePerKg;
}
#endregion
#region Private Methods
/// <summary>
/// Populate of GridView.
/// </summary>
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection sqlConnection = new SqlConnection(constr))
{
using (SqlCommand sqlCommand = new SqlCommand("SELECT Id, Name, PricePerKg FROM Vegetables", sqlConnection))
{
using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand))
{
using (DataTable dtCustomers = new DataTable())
{
sqlDataAdapter.Fill(dtCustomers);
gvVegetables.DataSource = dtCustomers;
gvVegetables.DataBind();
}
}
}
}
}
#endregion
Screenshot
