Hi akhter,
Check this example. Now please take its reference and correct your code.
HTML
<table class="nav-justified">
<tr>
<td style="width: 217px" class="modal-sm"> </td>
<td style="width: 756px"> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td style="width: 217px" class="modal-sm"> </td>
<td style="width: 756px"> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td style="width: 217px" class="modal-sm"> </td>
<td style="width: 756px"> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td style="width: 217px" class="modal-sm"> </td>
<td style="width: 756px"> </td>
<td>
<asp:Button ID="btn_add" runat="server" Text="Add" OnClick="btn_add_Click" Width="66px" />
</td>
<td> </td>
</tr>
<tr>
<td style="width: 217px" class="modal-sm"> </td>
<td colspan="2" rowspan="5">
<div>
<asp:HiddenField ID="hfDebit" runat="server" />
<asp:HiddenField ID="hfCredit" runat="server" />
<asp:GridView ID="gvtrans" runat="server" AllowSorting="True" AutoGenerateColumns="False" CssClass="active right"
BorderColor="#E7E7FF" itemstyle-wrap="True" GridLines="Horizontal" ShowHeaderWhenEmpty="True" OnRowDataBound="gvtrans_RowDataBound"
ShowFooter="True" BackColor="White" BorderStyle="None" BorderWidth="1px" CellPadding="3" Style="margin-top: 0px" OnRowCreated="gvtrans_RowCreated" Width="1006px">
<AlternatingRowStyle CssClass="altrowstyle" BackColor="#F7F7F7" />
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle CssClass="headerstyle" BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle CssClass="rowstyle" BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="S.NO" />
<asp:TemplateField HeaderText="Code">
<ItemTemplate>
<asp:TextBox ID="txtcode" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Head">
<ItemTemplate>
<asp:Label ID="lbfourdr" runat="server" Visible="false" />
<asp:DropDownList runat="server" AutoPostBack="true" ID="ddlfourdr" OnSelectedIndexChanged="ddlfourdr_SelectedIndexChanged" CssClass="form-control js-example-placeholder-single" ToolTip="Select " Height="85px" Width="300px">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Debit">
<ItemTemplate>
<asp:TextBox ID="txtDebit" runat="server" AutoPostBack="true" OnTextChanged="txtDebit_TextChanged"></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" runat="server"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Credit">
<ItemTemplate>
<asp:TextBox ID="txtCredit" runat="server" AutoPostBack="true" OnTextChanged="txtCredit_TextChanged"></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotalcr" runat="server"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type">
<ItemTemplate>
<asp:DropDownList ID="ddlType" runat="server" Width="70px">
<asp:ListItem Text="Credit" Value="1"></asp:ListItem>
<asp:ListItem Text="Debit" Value="2"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<SortedAscendingCellStyle BackColor="#F4F4FD" />
<SortedAscendingHeaderStyle BackColor="#5A4C9D" />
<SortedDescendingCellStyle BackColor="#D8D8F0" />
<SortedDescendingHeaderStyle BackColor="#3E3277" />
</asp:GridView>
</div>
</td>
<td> </td>
</tr>
<tr>
<td style="width: 217px" class="modal-sm"> </td>
<td> </td>
</tr>
<tr>
<td style="width: 217px" class="modal-sm"> </td>
<td> </td>
</tr>
<tr>
<td style="width: 217px" class="modal-sm"> </td>
<td> </td>
</tr>
<tr>
<td style="width: 217px" class="modal-sm"> </td>
<td> </td>
</tr>
<tr>
<td style="width: 217px" class="modal-sm"> </td>
<td style="width: 756px">
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btn_Save_Click" Width="66px" /></td>
<td> </td>
<td> </td>
</tr>
</table>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<script type="text/javascript">
$(function () {
$(".js-example-placeholder-single").select2({
placeholder: "Select",
allowClear: true
});
});
$(function () {
CalculateDebitCredittotal()
});
function CalculateDebitCredittotal() {
var creditTotal = 0;
var debitTotal = 0;
$("[id*=gvtrans] tr:has(td)").each(function () {
if ($(this).find($("[id*=txtCredit]")).val() != "" && $(this).find($("[id*=txtCredit]")).val() != undefined) {
creditTotal += parseFloat($(this).find($("[id*=txtCredit]")).val());
}
if ($(this).find($("[id*=txtDebit]")).val() != "" && $(this).find($("[id*=txtDebit]")).val() != undefined) {
debitTotal += parseFloat($(this).find($("[id*=txtDebit]")).val());
}
$(this).find($("[id*=lblTotal]")).html("Total:" + debitTotal.toString());
$(this).find($("[id*=lblTotalcr]")).html("Total:" + creditTotal.toString());
});
$("[id*=hfCredit]").val(creditTotal);
$("[id*=hfDebit]").val(debitTotal);
}
</script>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Code
C#
SqlConnection con = new SqlConnection("Data Source=SERVER1\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=WHMS;MultipleActiveResultSets=True;");
DataTable dt = new DataTable();
DataRow dr = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialRow();
}
}
protected void gvtrans_RowCreated(object sender, GridViewRowEventArgs e)
{
}
protected void gvtrans_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txtcrcode = (TextBox)e.Row.FindControl("txtcodedr");
DropDownList ddlContnam = (DropDownList)e.Row.FindControl("ddlfourdr");
SqlCommand cmd = new SqlCommand("select * from tbl_Account_L_Four", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
ddlContnam.DataSource = dt;
ddlContnam.DataTextField = "Level_Four_Name";
ddlContnam.DataValueField = "Level_Four_ID";
ddlContnam.DataBind();
ddlContnam.Items.Insert(0, new ListItem("--Select Debit--", "0"));
}
}
protected void ddlfourdr_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlfourdr = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddlfourdr.NamingContainer;
DropDownList ddlModel = (DropDownList)row.FindControl("ddlfourdr");
TextBox txtcodedr = (TextBox)row.FindControl("txtcode");
string strQuery = "select Level_Four_ID from tbl_Account_L_Four where Level_Four_ID = @Level_Four_ID";
SqlCommand cmd = new SqlCommand();
cmd.Parameters.AddWithValue("@Level_Four_ID", ddlModel.SelectedValue);
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
txtcodedr.Text = sdr[0].ToString();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
protected void txtDebit_TextChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in gvtrans.Rows)
{
TextBox Debits = row.FindControl("txtDebit") as TextBox;
TextBox Credits = row.FindControl("txtCredit") as TextBox;
DropDownList ddl = row.FindControl("ddltype") as DropDownList;
ddl.ClearSelection();
if (Debits.Text != "")
{
ddl.Items.FindByText("Debit").Selected = true;
Credits.Enabled = false;
}
else
{
ddl.Items.FindByText("Credit").Selected = true;
Credits.Enabled = true;
}
}
}
protected void txtCredit_TextChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in gvtrans.Rows)
{
TextBox Debitd = row.FindControl("txtDebit") as TextBox;
TextBox Creditc = row.FindControl("txtCredit") as TextBox;
DropDownList ddlc = row.FindControl("ddltype") as DropDownList;
ddlc.ClearSelection();
if (Creditc.Text != "")
{
ddlc.Items.FindByText("Credit").Selected = true;
Debitd.Enabled = false;
}
else
{
ddlc.Items.FindByText("Debit").Selected = true;
Debitd.Enabled = true;
}
}
}
protected void btn_add_Click(object sender, EventArgs e)
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//extract the TextBox values
TextBox box1 = (TextBox)gvtrans.Rows[rowIndex].Cells[1].FindControl("txtcode");
DropDownList box2 = (DropDownList)gvtrans.Rows[rowIndex].Cells[2].FindControl("ddlfourdr");
TextBox box3 = (TextBox)gvtrans.Rows[rowIndex].Cells[3].FindControl("txtDebit");
TextBox box4 = (TextBox)gvtrans.Rows[rowIndex].Cells[4].FindControl("txtCredit");
DropDownList box5 = (DropDownList)gvtrans.Rows[rowIndex].Cells[5].FindControl("ddlType");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
dtCurrentTable.Rows[i - 1]["txtcode"] = box1.Text;
dtCurrentTable.Rows[i - 1]["ddlfourdr"] = box2.Text;
dtCurrentTable.Rows[i - 1]["txtDebit"] = box3.Text;
dtCurrentTable.Rows[i - 1]["txtCredit"] = box4.Text;
dtCurrentTable.Rows[i - 1]["ddlType"] = box5.SelectedValue;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
gvtrans.DataSource = dtCurrentTable;
gvtrans.DataBind();
foreach (GridViewRow row in gvtrans.Rows)
{
TextBox txtDebit = (TextBox)row.FindControl("txtDebit");
TextBox txtCredit = (TextBox)row.FindControl("txtCredit");
DataTable dt = (DataTable)ViewState["CurrentTable"];
string type = dt.Rows[row.RowIndex]["ddlType"].ToString();
if (!string.IsNullOrEmpty(dt.Rows[row.RowIndex]["txtDebit"].ToString())
|| !string.IsNullOrEmpty(dt.Rows[row.RowIndex]["txtCredit"].ToString()))
{
txtDebit.Enabled = false;
txtCredit.Enabled = false;
if (type == "1")
{
txtCredit.Enabled = true;
}
if (type == "2")
{
txtDebit.Enabled = true;
}
}
}
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}
protected void btn_Save_Click(object sender, EventArgs e)
{
decimal totalDebit = Convert.ToDecimal(hfCredit.Value);
decimal totalCredit = Convert.ToDecimal(hfDebit.Value);
if (totalDebit == totalCredit)
{
foreach (GridViewRow row in gvtrans.Rows)
{
TextBox transId = row.FindControl("txtcode") as TextBox;
DropDownList ddltype = row.FindControl("ddltype") as DropDownList;
TextBox debit = row.FindControl("txtDebit") as TextBox;
TextBox credit = row.FindControl("txtCredit") as TextBox;
DropDownList ddlfourdr = row.FindControl("ddlfourdr") as DropDownList;
string query = @"INSERT INTO Reciept_Disbursement_Detail VALUES (@Id,@Head,@CrDr,@CrDrAction)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@Id", transId.Text);
cmd.Parameters.AddWithValue("@Head", ddlfourdr.SelectedValue);
cmd.Parameters.AddWithValue("@CrDr", !string.IsNullOrEmpty(debit.Text) ? debit.Text : credit.Text);
cmd.Parameters.AddWithValue("@CrDrAction", ddltype.SelectedValue);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
private void SetInitialRow()
{
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("txtcode", typeof(string)));
dt.Columns.Add(new DataColumn("ddlfourdr", typeof(string)));
dt.Columns.Add(new DataColumn("txtDebit", typeof(string)));
dt.Columns.Add(new DataColumn("txtCredit", typeof(string)));
dt.Columns.Add(new DataColumn("ddltype", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["txtcode"] = String.Empty;
dr["txtDebit"] = String.Empty;
dr["txtCredit"] = String.Empty;
dt.Rows.Add(dr);
ViewState["CurrentTable"] = dt;
gvtrans.DataSource = dt;
gvtrans.DataBind();
}
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TextBox box1 = (TextBox)gvtrans.Rows[rowIndex].Cells[1].FindControl("txtcode");
DropDownList box2 = (DropDownList)gvtrans.Rows[rowIndex].Cells[2].FindControl("ddlfourdr");
TextBox box3 = (TextBox)gvtrans.Rows[rowIndex].Cells[3].FindControl("txtDebit");
TextBox box4 = (TextBox)gvtrans.Rows[rowIndex].Cells[4].FindControl("txtCredit");
DropDownList box5 = (DropDownList)gvtrans.Rows[rowIndex].Cells[5].FindControl("ddlType");
box1.Text = dt.Rows[i]["txtcode"].ToString();
box2.Text = dt.Rows[i]["ddlfourdr"].ToString();
box3.Text = dt.Rows[i]["txtDebit"].ToString();
box4.Text = dt.Rows[i]["txtCredit"].ToString();
box5.SelectedValue = dt.Rows[i]["ddlType"].ToString();
rowIndex++;
}
}
}
}
VB.Net
Private con As SqlConnection = New SqlConnection("Data Source=SERVER1\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=WHMS;MultipleActiveResultSets=True;")
Private dt As DataTable = New DataTable()
Private dr As DataRow = Nothing
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
SetInitialRow()
End If
End Sub
Protected Sub gvtrans_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
End Sub
Protected Sub gvtrans_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim txtcrcode As TextBox = CType(e.Row.FindControl("txtcodedr"), TextBox)
Dim ddlContnam As DropDownList = CType(e.Row.FindControl("ddlfourdr"), DropDownList)
Dim cmd As SqlCommand = New SqlCommand("select * from tbl_Account_L_Four", con)
Dim sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
ddlContnam.DataSource = dt
ddlContnam.DataTextField = "Level_Four_Name"
ddlContnam.DataValueField = "Level_Four_ID"
ddlContnam.DataBind()
ddlContnam.Items.Insert(0, New ListItem("--Select Debit--", "0"))
End If
End Sub
Protected Sub ddlfourdr_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim ddlfourdr As DropDownList = CType(sender, DropDownList)
Dim row As GridViewRow = CType(ddlfourdr.NamingContainer, GridViewRow)
Dim ddlModel As DropDownList = CType(row.FindControl("ddlfourdr"), DropDownList)
Dim txtcodedr As TextBox = CType(row.FindControl("txtcode"), TextBox)
Dim strQuery As String = "select Level_Four_ID from tbl_Account_L_Four where Level_Four_ID = @Level_Four_ID"
Dim cmd As SqlCommand = New SqlCommand()
cmd.Parameters.AddWithValue("@Level_Four_ID", ddlModel.SelectedValue)
cmd.CommandType = CommandType.Text
cmd.CommandText = strQuery
cmd.Connection = con
Try
con.Open()
Dim sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
txtcodedr.Text = sdr(0).ToString()
End While
Catch ex As Exception
Throw ex
Finally
con.Close()
con.Dispose()
End Try
End Sub
Protected Sub txtDebit_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
For Each row As GridViewRow In gvtrans.Rows
Dim Debits As TextBox = TryCast(row.FindControl("txtDebit"), TextBox)
Dim Credits As TextBox = TryCast(row.FindControl("txtCredit"), TextBox)
Dim ddl As DropDownList = TryCast(row.FindControl("ddltype"), DropDownList)
ddl.ClearSelection()
If Debits.Text <> "" Then
ddl.Items.FindByText("Debit").Selected = True
Credits.Enabled = False
Else
ddl.Items.FindByText("Credit").Selected = True
Credits.Enabled = True
End If
Next
End Sub
Protected Sub txtCredit_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
For Each row As GridViewRow In gvtrans.Rows
Dim Debitd As TextBox = TryCast(row.FindControl("txtDebit"), TextBox)
Dim Creditc As TextBox = TryCast(row.FindControl("txtCredit"), TextBox)
Dim ddlc As DropDownList = TryCast(row.FindControl("ddltype"), DropDownList)
ddlc.ClearSelection()
If Creditc.Text <> "" Then
ddlc.Items.FindByText("Credit").Selected = True
Debitd.Enabled = False
Else
ddlc.Items.FindByText("Debit").Selected = True
Debitd.Enabled = True
End If
Next
End Sub
Protected Sub btn_add_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim rowIndex As Integer = 0
If ViewState("CurrentTable") IsNot Nothing Then
Dim dtCurrentTable As DataTable = CType(ViewState("CurrentTable"), DataTable)
Dim drCurrentRow As DataRow = Nothing
If dtCurrentTable.Rows.Count > 0 Then
For i As Integer = 1 To dtCurrentTable.Rows.Count
Dim box1 As TextBox = CType(gvtrans.Rows(rowIndex).Cells(1).FindControl("txtcode"), TextBox)
Dim box2 As DropDownList = CType(gvtrans.Rows(rowIndex).Cells(2).FindControl("ddlfourdr"), DropDownList)
Dim box3 As TextBox = CType(gvtrans.Rows(rowIndex).Cells(3).FindControl("txtDebit"), TextBox)
Dim box4 As TextBox = CType(gvtrans.Rows(rowIndex).Cells(4).FindControl("txtCredit"), TextBox)
Dim box5 As DropDownList = CType(gvtrans.Rows(rowIndex).Cells(5).FindControl("ddlType"), DropDownList)
drCurrentRow = dtCurrentTable.NewRow()
drCurrentRow("RowNumber") = i + 1
dtCurrentTable.Rows(i - 1)("txtcode") = box1.Text
dtCurrentTable.Rows(i - 1)("ddlfourdr") = box2.Text
dtCurrentTable.Rows(i - 1)("txtDebit") = box3.Text
dtCurrentTable.Rows(i - 1)("txtCredit") = box4.Text
dtCurrentTable.Rows(i - 1)("ddlType") = box5.SelectedValue
rowIndex += 1
Next
dtCurrentTable.Rows.Add(drCurrentRow)
ViewState("CurrentTable") = dtCurrentTable
gvtrans.DataSource = dtCurrentTable
gvtrans.DataBind()
For Each row As GridViewRow In gvtrans.Rows
Dim txtDebit As TextBox = CType(row.FindControl("txtDebit"), TextBox)
Dim txtCredit As TextBox = CType(row.FindControl("txtCredit"), TextBox)
Dim dt As DataTable = CType(ViewState("CurrentTable"), DataTable)
Dim type As String = dt.Rows(row.RowIndex)("ddlType").ToString()
If Not String.IsNullOrEmpty(dt.Rows(row.RowIndex)("txtDebit").ToString()) _
OrElse Not String.IsNullOrEmpty(dt.Rows(row.RowIndex)("txtCredit").ToString()) Then
txtDebit.Enabled = False
txtCredit.Enabled = False
If type = "1" Then
txtCredit.Enabled = True
End If
If type = "2" Then
txtDebit.Enabled = True
End If
End If
Next
End If
Else
Response.Write("ViewState is null")
End If
SetPreviousData()
End Sub
Protected Sub btn_Save_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim totalDebit As Decimal = Convert.ToDecimal(hfCredit.Value)
Dim totalCredit As Decimal = Convert.ToDecimal(hfDebit.Value)
If totalDebit = totalCredit Then
For Each row As GridViewRow In gvtrans.Rows
Dim transId As TextBox = TryCast(row.FindControl("txtcode"), TextBox)
Dim ddltype As DropDownList = TryCast(row.FindControl("ddltype"), DropDownList)
Dim debit As TextBox = TryCast(row.FindControl("txtDebit"), TextBox)
Dim credit As TextBox = TryCast(row.FindControl("txtCredit"), TextBox)
Dim ddlfourdr As DropDownList = TryCast(row.FindControl("ddlfourdr"), DropDownList)
Dim query As String = "INSERT INTO Reciept_Disbursement_Detail VALUES (@Id,@Head,@CrDr,@CrDrAction)"
Using cmd As SqlCommand = New SqlCommand(query)
cmd.Parameters.AddWithValue("@Id", transId.Text)
cmd.Parameters.AddWithValue("@Head", ddlfourdr.SelectedValue)
cmd.Parameters.AddWithValue("@CrDr", If(Not String.IsNullOrEmpty(debit.Text), debit.Text, credit.Text))
cmd.Parameters.AddWithValue("@CrDrAction", ddltype.SelectedValue)
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
Next
End If
End Sub
Private Sub SetInitialRow()
dt.Columns.Add(New DataColumn("RowNumber", GetType(String)))
dt.Columns.Add(New DataColumn("txtcode", GetType(String)))
dt.Columns.Add(New DataColumn("ddlfourdr", GetType(String)))
dt.Columns.Add(New DataColumn("txtDebit", GetType(String)))
dt.Columns.Add(New DataColumn("txtCredit", GetType(String)))
dt.Columns.Add(New DataColumn("ddltype", GetType(String)))
dr = dt.NewRow()
dr("RowNumber") = 1
dr("txtcode") = String.Empty
dr("txtDebit") = String.Empty
dr("txtCredit") = String.Empty
dt.Rows.Add(dr)
ViewState("CurrentTable") = dt
gvtrans.DataSource = dt
gvtrans.DataBind()
End Sub
Private Sub SetPreviousData()
Dim rowIndex As Integer = 0
If ViewState("CurrentTable") IsNot Nothing Then
Dim dt As DataTable = CType(ViewState("CurrentTable"), DataTable)
If dt.Rows.Count > 0 Then
For i As Integer = 0 To dt.Rows.Count - 1
Dim box1 As TextBox = CType(gvtrans.Rows(rowIndex).Cells(1).FindControl("txtcode"), TextBox)
Dim box2 As DropDownList = CType(gvtrans.Rows(rowIndex).Cells(2).FindControl("ddlfourdr"), DropDownList)
Dim box3 As TextBox = CType(gvtrans.Rows(rowIndex).Cells(3).FindControl("txtDebit"), TextBox)
Dim box4 As TextBox = CType(gvtrans.Rows(rowIndex).Cells(4).FindControl("txtCredit"), TextBox)
Dim box5 As DropDownList = CType(gvtrans.Rows(rowIndex).Cells(5).FindControl("ddlType"), DropDownList)
box1.Text = dt.Rows(i)("txtcode").ToString()
box2.Text = dt.Rows(i)("ddlfourdr").ToString()
box3.Text = dt.Rows(i)("txtDebit").ToString()
box4.Text = dt.Rows(i)("txtCredit").ToString()
box5.SelectedValue = dt.Rows(i)("ddlType").ToString()
rowIndex += 1
Next
End If
End If
End Sub