Hi Joey,
Please refer below sample.
Database
CREATE TABLE [dbo].[Vegitables](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL,
[PricePerkg] [int] NULL,
[QtyInKg] [int] NULL,
CONSTRAINT [PK_Vegitables] 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]
GO
HTML
<div>
<asp:GridView ID="gvVegitables" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound" ShowHeaderWhenEmpty="true">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="PricePerKg" HeaderText="PricePerKg" />
<asp:BoundField DataField="QtyInKg" HeaderText="QtyInKg" />
</Columns>
<EmptyDataTemplate>
<div align="center">
Record not found
</div>
</EmptyDataTemplate>
</asp:GridView>
<br />
<br />
<table>
<tr>
<td>Name:</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="rfvtName" runat="server" ControlToValidate="txtName" Display="Dynamic"
ErrorMessage="Name is required" ForeColor="Red"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revName" runat="server" ControlToValidate="txtName" Display="Dynamic" ForeColor="Red"
ErrorMessage="Accepted only alphates" ValidationExpression="[a-zA-Z ]*$"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>PricePerKg:</td>
<td>
<asp:TextBox ID="txtPricePerKg" runat="server"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="rfvPricePerKg" runat="server" ControlToValidate="txtPricePerKg" Display="Dynamic"
ErrorMessage="PricePerKg is required" ForeColor="Red"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revPricePerKg" runat="server" ControlToValidate="txtPricePerKg" Display="Dynamic" ForeColor="Red"
ErrorMessage="Accepted only 0-9 " ValidationExpression="[0-9]*$"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>QtyInKg:</td>
<td>
<asp:TextBox ID="txtQtyInKg" runat="server"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="rfvQtyInKg" runat="server" ControlToValidate="txtQtyInKg" Display="Dynamic"
ErrorMessage="QtyInKg is required" ForeColor="Red"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revQtyInKg" runat="server" ControlToValidate="txtQtyInKg" Display="Dynamic" ForeColor="Red"
ErrorMessage="Accepted only 0-9" ValidationExpression="[0-9]*$"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td></td>
<td colspan="2">
<asp:Button runat="server" Text="Submit" OnClick="OnSubmit" /></td>
</tr>
</table>
</div>
Namespace
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web.UI.WebControls;
Code
#region Events
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
protected void OnSubmit(Object sender, EventArgs e)
{
string name = txtName.Text.Trim();
int pricePrKg = int.Parse(txtPricePerKg.Text);
int qtyInKg = int.Parse(txtQtyInKg.Text);
if (!string.IsNullOrEmpty(name) && pricePrKg > 0 && qtyInKg > 0)
{
string qrery = @"INSERT INTO Vegitables VALUES(@Name,@PricePrKg,@QtyInKg)";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection sqlConnection = new SqlConnection(constr))
{
using (SqlCommand sqlCommand = new SqlCommand(qrery, sqlConnection))
{
sqlCommand.Parameters.AddWithValue("@Name", name);
sqlCommand.Parameters.AddWithValue("@PricePrKg", pricePrKg);
sqlCommand.Parameters.AddWithValue("@QtyInKg", qtyInKg);
sqlConnection.Open();
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
}
}
}
this.BindGrid();
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int quantity = int.Parse(e.Row.Cells[3].Text);
if (quantity <= 5)
{
e.Row.BackColor = Color.Red;
}
}
}
#endregion
#region Private Methods
/// Populates 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, QtyInKg FROM Vegitables", sqlConnection))
{
using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand))
{
using (DataTable dtVegitables = new DataTable())
{
sqlDataAdapter.Fill(dtVegitables);
gvVegitables.DataSource = dtVegitables;
gvVegitables.DataBind();
}
}
}
}
}
#endregion
Screenshot
