Code in aspx:
<asp:GridView ID="GridView1" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" runat="server" Width="100%" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowDataBound="GridView1_RowDataBound1" SelectedIndex="0" CellPadding="0" ForeColor="Black" GridLines="None">
<HeaderStyle CssClass="HeaderCSS" BackColor="#CCCCCC" Font-Bold="True" ForeColor="Black" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" />
<RowStyle ForeColor="#333333" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" />
<%-- BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" HorizontalAlign="Center"--%>
<Columns>
<asp:TemplateField HeaderStyle-CssClass="gridViewHeader" HeaderText="Account Name-Code" ItemStyle-CssClass="ItemStyle">
<HeaderStyle BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" />
<ItemStyle BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" />
<ItemTemplate>
<asp:DropDownList ID="gvDdlAccountName" CssClass="ddlCSS" runat="server" DataField="ActName" Width="197px" AutoPostBack="False">
</asp:DropDownList>
</ItemTemplate>
<%-- <HeaderStyle CssClass="gridViewHeader" />--%>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-CssClass="gridViewHeader" HeaderText=" Debit" ItemStyle-CssClass="ItemStyle">
<HeaderStyle BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" />
<ItemStyle BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" />
<ItemTemplate>
<asp:TextBox ID="debitTxt" DataField="Debit" runat="server" CssClass="txtCssDr" Width="180px"></asp:TextBox>
</ItemTemplate>
<HeaderStyle CssClass="gridViewHeader" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-CssClass="gridViewHeader" HeaderText=" Credit" ItemStyle-CssClass="ItemStyle">
<HeaderStyle BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" />
<ItemStyle BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" />
<ItemTemplate>
<asp:TextBox ID="creditTxt" runat="server" DataField="Credit" Width="180px" CssClass="txtCssCr"></asp:TextBox>
</ItemTemplate>
<HeaderStyle CssClass="gridViewHeader" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-CssClass="gridViewHeader" HeaderText="Action" ItemStyle-CssClass="cssAction">
<ItemTemplate>
<asp:Button ID="btnAdd" runat="server" Text="Add" Width="85px" OnClick="btnAdd_Click" UseSubmitBehavior="False" />
</ItemTemplate>
<HeaderStyle CssClass="gridViewHeader" />
</asp:TemplateField>
</Columns>
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<asp:GridView ID="GridView1" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" runat="server" Width="100%" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowDataBound="GridView1_RowDataBound1" SelectedIndex="0" CellPadding="0" ForeColor="Black" GridLines="None">
<HeaderStyle CssClass="HeaderCSS" BackColor="#CCCCCC" Font-Bold="True" ForeColor="Black" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" />
<RowStyle ForeColor="#333333" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" />
<%-- BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" HorizontalAlign="Center"--%>
<Columns>
<asp:TemplateField HeaderStyle-CssClass="gridViewHeader" HeaderText="Account Name-Code" ItemStyle-CssClass="ItemStyle">
<HeaderStyle BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" />
<ItemStyle BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" />
<ItemTemplate>
<asp:DropDownList ID="gvDdlAccountName" CssClass="ddlCSS" runat="server" DataField="ActName" Width="197px" AutoPostBack="False">
</asp:DropDownList>
</ItemTemplate>
<%-- <HeaderStyle CssClass="gridViewHeader" />--%>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-CssClass="gridViewHeader" HeaderText=" Debit" ItemStyle-CssClass="ItemStyle">
<HeaderStyle BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" />
<ItemStyle BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" />
<ItemTemplate>
<asp:TextBox ID="debitTxt" DataField="Debit" runat="server" CssClass="txtCssDr" Width="180px"></asp:TextBox>
</ItemTemplate>
<HeaderStyle CssClass="gridViewHeader" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-CssClass="gridViewHeader" HeaderText=" Credit" ItemStyle-CssClass="ItemStyle">
<HeaderStyle BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" />
<ItemStyle BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" />
<ItemTemplate>
<asp:TextBox ID="creditTxt" runat="server" DataField="Credit" Width="180px" CssClass="txtCssCr"></asp:TextBox>
</ItemTemplate>
<HeaderStyle CssClass="gridViewHeader" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-CssClass="gridViewHeader" HeaderText="Action" ItemStyle-CssClass="cssAction">
<ItemTemplate>
<asp:Button ID="btnAdd" runat="server" Text="Add" Width="85px" OnClick="btnAdd_Click" UseSubmitBehavior="False" />
</ItemTemplate>
<HeaderStyle CssClass="gridViewHeader" />
</asp:TemplateField>
</Columns>
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
C# Code:
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList gvDdlAccountName = (DropDownList)e.Row.FindControl("gvDdlAccountName");
TextBox debitTextBox = (TextBox)e.Row.FindControl("debitTxt");
TextBox creditTxtBox = (TextBox)e.Row.FindControl("creditTxt");
Button btn = (Button)e.Row.FindControl("btnAdd");
string ab = "";
if (gvDdlAccountName != null )
{
gvDdlAccountName.DataSource =(DataTable) Session["AllAccount"];
ab =((DataRowView) e.Row.DataItem)[2].ToString();
if(ab!="")
gvDdlAccountName.SelectedValue = ab;
gvDdlAccountName.DataTextField = "ActName";
gvDdlAccountName.DataValueField = "ActCode";
gvDdlAccountName.DataBind();
}
if (debitTextBox != null)
{
debitTextBox.Text = String.Format("{0:0.00}", ((DataRowView)e.Row.DataItem)[4]);
if(debitTextBox.Text!="")
totalDebit += Convert.ToDouble(debitTextBox.Text);
}
if (creditTxtBox != null)
{
creditTxtBox.Text = String.Format("{0:0.00}", ((DataRowView)e.Row.DataItem)[5]);
if (creditTxtBox.Text != "")
totalCredit += Convert.ToDouble(creditTxtBox.Text);
}
if (ab != "")
{
btn.Text = "Delete";
btn.CommandArgument = ((DataRowView)e.Row.DataItem)[0].ToString();
}
}
}
catch (Exception exp)
{
Show(exp.Message);
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
try
{
string comm = ((Button) sender).CommandArgument;
if (comm == "")
{
String user = HttpContext.Current.User.Identity.Name;
DropDownList gvDdlAccountName =
(DropDownList) GridView1.Rows[GridView1.SelectedIndex].FindControl("gvDdlAccountName");
TextBox debitTextBox = (TextBox) GridView1.Rows[GridView1.SelectedIndex].FindControl("debitTxt");
TextBox creditTxtBox = (TextBox) GridView1.Rows[GridView1.SelectedIndex].FindControl("creditTxt");
if (txtDate.Text.Trim() == "")
{
Show("Please insert date");
return;
}
double debit = 0;
double credit = 0;
if (debitTextBox.Text.Trim() != "")
debit = Convert.ToDouble(debitTextBox.Text.Trim());
if (creditTxtBox.Text.Trim() != "")
credit = Convert.ToDouble(creditTxtBox.Text.Trim());
if (debit == 0 || credit == 0)
{
AccTransectionInfo ati = new AccTransectionInfo
{
ActCode = gvDdlAccountName.SelectedValue.Trim(),
ActName = gvDdlAccountName.SelectedItem.Text.Trim().Split('-')[0],
Debit = debit,
Credit = credit,
EntryBy = user,
Comments = txtComment.Text.Trim()
};
u.CommonSqlExecutionBool(
"INSERT INTO [dbo].[AccTransTemp]([EntryBy] ,[ActCode],[ActName],[Debit],[Credit],[Comments])" +
" VALUES ( '" + ati.EntryBy + "','" + ati.ActCode + "','" + ati.ActName + "','" + ati.Debit +
"' ,'" +
ati.Credit + "','" + ati.Comments + "') ");
FillGrid();
ClearPage();
}
else
{
Show("Debit Or Credit Must be 0.");
}
}
else
{
u.CommonSqlExecutionBool("DELETE FROM AccTransTemp WHERE Sl='" + comm + "'");
FillGrid();
}
}
catch (Exception ex)
{
Show(ex.Message.ToString());
}
}