Design page :
<asp:gridview ID="Gridview1" runat="server" ShowFooter="True" Width="600px" AutoGenerateColumns="False" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" ForeColor="Black" CellSpacing="2" OnRowDataBound="Gridview1_RowDataBound">
<Columns>
<%--<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" Visible="false" runat="server" />
</ItemTemplate>
</asp:TemplateField>--%>
<asp:TemplateField HeaderText="Product Name">
<ItemTemplate>
<asp:DropDownList ID="txtdes" runat="server" AutoPostBack="True" ValidationGroup="g">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator14" runat="server" ControlToValidate="txtdes" ErrorMessage="*" ForeColor="#CC3300" InitialValue="Select Product" ValidationGroup="g"></asp:RequiredFieldValidator>
</ItemTemplate>
<ControlStyle Width="150px" />
<ItemStyle Width="10px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txtquan" onkeypress="return validate(event)" runat="server" AutoPostBack="True" OnTextChanged="txtquan_TextChanged" ValidationGroup="g">0</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ControlToValidate="txtquan" ErrorMessage="*" ForeColor="#CC3300" ValidationGroup="g"></asp:RequiredFieldValidator>
</ItemTemplate>
<ControlStyle Width="150px" />
<ItemStyle Width="10px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:TextBox ID="ddlDay" runat="server" ValidationGroup="g" OnTextChanged="ddlDay_TextChanged" AutoPostBack="True">0</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator12" runat="server" ControlToValidate="ddlDay" ErrorMessage="*" ForeColor="#CC3300" ValidationGroup="g"></asp:RequiredFieldValidator>
</ItemTemplate>
<ControlStyle Width="150px" />
<ItemStyle Width="10px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:TextBox ID="txtTotal" runat="server" ValidationGroup="g" AutoPostBack="True" ReadOnly="True">0</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ControlToValidate="txtTotal" ErrorMessage="*" ForeColor="#CC3300" ValidationGroup="g"></asp:RequiredFieldValidator>
</ItemTemplate>
<ControlStyle Width="150px" />
<FooterStyle HorizontalAlign="Right" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:gridview>
C# page :
Namespace :
using System.IO;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Configuration;
Paste in page load:
if (!IsPostBack)
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Cname", typeof(String)));
dt.Columns.Add(new DataColumn("Qty", typeof(String)));
dt.Columns.Add(new DataColumn("Price", typeof(String)));
dt.Columns.Add(new DataColumn("Total", typeof(String)));
dr = dt.NewRow();
dr["Cname"] = string.Empty;
dr["Qty"] = string.Empty;
dr["Price"] = string.Empty;
dr["Total"] = string.Empty;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Cname"] = string.Empty;
dr["Qty"] = string.Empty;
dr["Price"] = string.Empty;
dr["Total"] = string.Empty;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Cname"] = string.Empty;
dr["Qty"] = string.Empty;
dr["Price"] = string.Empty;
dr["Total"] = string.Empty;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Cname"] = string.Empty;
dr["Qty"] = string.Empty;
dr["Price"] = string.Empty;
dr["Total"] = string.Empty;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Cname"] = string.Empty;
dr["Qty"] = string.Empty;
dr["Price"] = string.Empty;
dr["Total"] = string.Empty;
dt.Rows.Add(dr);
Gridview1.DataSource = dt;
Gridview1.DataBind();
ddl();
GrandTotal();
}
}
public void ddl() //to bind product in dropdown
{
foreach (GridViewRow row in Gridview1.Rows)
{
DropDownList ddl = (DropDownList)row.FindControl("txtdes");
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbcn"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("select distinct(Cname) from Table3", con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
ddl.Items.Clear();
ddl.Items.Add("-Select Share-");
SqlDataReader rd = cmd.ExecuteReader();
while (rd.Read())
{
ddl.Items.Add(rd[0].ToString());
}
string product = ddl.Text;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string name = DropDownList4.Text;
foreach (GridViewRow row in Gridview1.Rows)
{
DropDownList ddl = (DropDownList)row.FindControl("txtdes");
string product = ddl.Text;
if (product == "-Select Share-")
{
}
else
{
TextBox txt1 = (TextBox)row.FindControl("txtquan");
string quantity = txt1.Text;
TextBox txt2 = (TextBox)row.FindControl("ddlDay");
string price = txt2.Text;
TextBox txt3 = (TextBox)row.FindControl("txtTotal");
string total = txt3.Text;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbcn"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("insert into Table4 values('" + name + "','" + product + "','" + quantity + "','" + price + "','" + total + "')", con);
cmd.ExecuteNonQuery();
con.Close();
}
}
Response.Redirect("AddTransactions.aspx");
}
private void GrandTotal() //to find total dynamically in footer
{
int grandtotal = 0;
int n;
foreach (GridViewRow row in Gridview1.Rows)
{
//n = Convert.ToInt32(row.Cells[3].Text);
TextBox tb = (TextBox)row.FindControl("txtTotal");
n = Convert.ToInt32(tb.Text);
grandtotal = grandtotal + n; //Where Cells is the column. Just changed the index of cells
}
Gridview1.FooterRow.Cells[2].Text = "Grand Total";
Gridview1.FooterRow.Cells[3].Text = grandtotal.ToString();
}
protected void txtquan_TextChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in Gridview1.Rows)
{
int quantity = 0;
int price = 0;
int total = 0;
TextBox txt1 = (TextBox)row.FindControl("txtquan");
quantity = Convert.ToInt32(txt1.Text);
TextBox txt2 = (TextBox)row.FindControl("ddlDay");
price = Convert.ToInt32(txt2.Text);
TextBox txt3 = (TextBox)row.FindControl("txtTotal");
total = (Convert.ToInt32(quantity) * Convert.ToInt32(price));
txt3.Text = total.ToString();
GrandTotal();
}
}
protected void ddlDay_TextChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in Gridview1.Rows)
{
//TextBox txt1 = (TextBox)row.FindControl("txtquan");
//string quantity = txt1.Text;
//TextBox txt2 = (TextBox)row.FindControl("ddlDay");
//string price = txt2.Text;
//TextBox txt3 = (TextBox)row.FindControl("txtTotal");
//string total = (Convert.ToInt32(quantity) * Convert.ToInt32(price)).ToString();
//txt3.Text = total;
////GrandTotal();
int quantity = 0;
int price = 0;
int total = 0;
TextBox txt1 = (TextBox)row.FindControl("txtquan");
quantity = Convert.ToInt32(txt1.Text);
TextBox txt2 = (TextBox)row.FindControl("ddlDay");
price = Convert.ToInt32(txt2.Text);
TextBox txt3 = (TextBox)row.FindControl("txtTotal");
total = (Convert.ToInt32(quantity) * Convert.ToInt32(price));
txt3.Text = total.ToString();
GrandTotal();
}
}
<asp:gridview ID="Gridview1" runat="server" ShowFooter="True" Width="600px" AutoGenerateColumns="False" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" ForeColor="Black" CellSpacing="2" OnRowDataBound="Gridview1_RowDataBound">
<Columns>
<%--<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" Visible="false" runat="server" />
</ItemTemplate>
</asp:TemplateField>--%>
<asp:TemplateField HeaderText="Product Name">
<ItemTemplate>
<asp:DropDownList ID="txtdes" runat="server" AutoPostBack="True" ValidationGroup="g">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator14" runat="server" ControlToValidate="txtdes" ErrorMessage="*" ForeColor="#CC3300" InitialValue="Select Product" ValidationGroup="g"></asp:RequiredFieldValidator>
</ItemTemplate>
<ControlStyle Width="150px" />
<ItemStyle Width="10px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txtquan" onkeypress="return validate(event)" runat="server" AutoPostBack="True" OnTextChanged="txtquan_TextChanged" ValidationGroup="g">0</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ControlToValidate="txtquan" ErrorMessage="*" ForeColor="#CC3300" ValidationGroup="g"></asp:RequiredFieldValidator>
</ItemTemplate>
<ControlStyle Width="150px" />
<ItemStyle Width="10px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:TextBox ID="ddlDay" runat="server" ValidationGroup="g" OnTextChanged="ddlDay_TextChanged" AutoPostBack="True">0</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator12" runat="server" ControlToValidate="ddlDay" ErrorMessage="*" ForeColor="#CC3300" ValidationGroup="g"></asp:RequiredFieldValidator>
</ItemTemplate>
<ControlStyle Width="150px" />
<ItemStyle Width="10px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:TextBox ID="txtTotal" runat="server" ValidationGroup="g" AutoPostBack="True" ReadOnly="True">0</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ControlToValidate="txtTotal" ErrorMessage="*" ForeColor="#CC3300" ValidationGroup="g"></asp:RequiredFieldValidator>
</ItemTemplate>
<ControlStyle Width="150px" />
<FooterStyle HorizontalAlign="Right" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:gridview>
C# page :
Namespace :
using System.IO;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Configuration;
Paste in page load:
if (!IsPostBack)
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Cname", typeof(String)));
dt.Columns.Add(new DataColumn("Qty", typeof(String)));
dt.Columns.Add(new DataColumn("Price", typeof(String)));
dt.Columns.Add(new DataColumn("Total", typeof(String)));
dr = dt.NewRow();
dr["Cname"] = string.Empty;
dr["Qty"] = string.Empty;
dr["Price"] = string.Empty;
dr["Total"] = string.Empty;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Cname"] = string.Empty;
dr["Qty"] = string.Empty;
dr["Price"] = string.Empty;
dr["Total"] = string.Empty;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Cname"] = string.Empty;
dr["Qty"] = string.Empty;
dr["Price"] = string.Empty;
dr["Total"] = string.Empty;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Cname"] = string.Empty;
dr["Qty"] = string.Empty;
dr["Price"] = string.Empty;
dr["Total"] = string.Empty;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Cname"] = string.Empty;
dr["Qty"] = string.Empty;
dr["Price"] = string.Empty;
dr["Total"] = string.Empty;
dt.Rows.Add(dr);
Gridview1.DataSource = dt;
Gridview1.DataBind();
ddl();
GrandTotal();
}
}
public void ddl() //to bind product in dropdown
{
foreach (GridViewRow row in Gridview1.Rows)
{
DropDownList ddl = (DropDownList)row.FindControl("txtdes");
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbcn"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("select distinct(Cname) from Table3", con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
ddl.Items.Clear();
ddl.Items.Add("-Select Share-");
SqlDataReader rd = cmd.ExecuteReader();
while (rd.Read())
{
ddl.Items.Add(rd[0].ToString());
}
string product = ddl.Text;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string name = DropDownList4.Text;
foreach (GridViewRow row in Gridview1.Rows)
{
DropDownList ddl = (DropDownList)row.FindControl("txtdes");
string product = ddl.Text;
if (product == "-Select Share-")
{
}
else
{
TextBox txt1 = (TextBox)row.FindControl("txtquan");
string quantity = txt1.Text;
TextBox txt2 = (TextBox)row.FindControl("ddlDay");
string price = txt2.Text;
TextBox txt3 = (TextBox)row.FindControl("txtTotal");
string total = txt3.Text;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbcn"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("insert into Table4 values('" + name + "','" + product + "','" + quantity + "','" + price + "','" + total + "')", con);
cmd.ExecuteNonQuery();
con.Close();
}
}
Response.Redirect("AddTransactions.aspx");
}
private void GrandTotal() //to find total dynamically in footer
{
int grandtotal = 0;
int n;
foreach (GridViewRow row in Gridview1.Rows)
{
//n = Convert.ToInt32(row.Cells[3].Text);
TextBox tb = (TextBox)row.FindControl("txtTotal");
n = Convert.ToInt32(tb.Text);
grandtotal = grandtotal + n; //Where Cells is the column. Just changed the index of cells
}
Gridview1.FooterRow.Cells[2].Text = "Grand Total";
Gridview1.FooterRow.Cells[3].Text = grandtotal.ToString();
}
protected void txtquan_TextChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in Gridview1.Rows)
{
int quantity = 0;
int price = 0;
int total = 0;
TextBox txt1 = (TextBox)row.FindControl("txtquan");
quantity = Convert.ToInt32(txt1.Text);
TextBox txt2 = (TextBox)row.FindControl("ddlDay");
price = Convert.ToInt32(txt2.Text);
TextBox txt3 = (TextBox)row.FindControl("txtTotal");
total = (Convert.ToInt32(quantity) * Convert.ToInt32(price));
txt3.Text = total.ToString();
GrandTotal();
}
}
protected void ddlDay_TextChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in Gridview1.Rows)
{
//TextBox txt1 = (TextBox)row.FindControl("txtquan");
//string quantity = txt1.Text;
//TextBox txt2 = (TextBox)row.FindControl("ddlDay");
//string price = txt2.Text;
//TextBox txt3 = (TextBox)row.FindControl("txtTotal");
//string total = (Convert.ToInt32(quantity) * Convert.ToInt32(price)).ToString();
//txt3.Text = total;
////GrandTotal();
int quantity = 0;
int price = 0;
int total = 0;
TextBox txt1 = (TextBox)row.FindControl("txtquan");
quantity = Convert.ToInt32(txt1.Text);
TextBox txt2 = (TextBox)row.FindControl("ddlDay");
price = Convert.ToInt32(txt2.Text);
TextBox txt3 = (TextBox)row.FindControl("txtTotal");
total = (Convert.ToInt32(quantity) * Convert.ToInt32(price));
txt3.Text = total.ToString();
GrandTotal();
}
}
No comments:
Post a Comment