Hi tex,
Check this example. Now please take its reference and correct your code.
HTML
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$.ajax({
type: "POST",
url: "Default.aspx/GetCustomers",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
}
});
});
var row;
function OnSuccess(response) {
var xmlDoc = $.parseXML(response.d);
var xml = $(xmlDoc);
var trans = xml.find("trans");
if (row == null) {
row = $("[id*=gvTransaction] tr:last-child").clone(true);
}
$("[id*=gvTransaction] tr").not($("[id*=gvTransaction] tr:first-child")).remove();
if (trans.length > 0) {
$.each(trans, function () {
$("td", row).eq(0).html($(this).find("t_id").text());
$("td", row).eq(1).html($(this).find("t_debit").text());
$("td", row).eq(2).html($(this).find("t_debit_dinar").text());
$("td", row).eq(3).html($(this).find("t_debit_fils").text());
$("td", row).eq(4).html($(this).find("t_credit").text());
$("td", row).eq(5).html($(this).find("t_credit_dinar").text());
$("td", row).eq(6).html($(this).find("t_credit_fils").text());
$("td", row).find('[id*=ch_transfer]').attr('checked', $(this).find("t_transfer").text() == "1" ? true : false);
$("[id*=gvTransaction]").append(row);
row = $("[id*=gvTransaction] tr:last-child").eq(0).clone(true);
});
}
}
</script>
<asp:GridView ID="gvTransaction" runat="server" ShowHeaderWhenEmpty="true" AutoGenerateColumns="false"
ShowHeader="true">
<Columns>
<asp:TemplateField HeaderText="No." ItemStyle-BackColor="Transparent" ControlStyle-BackColor="Transparent"
ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lbSeq" Value='<%# Eval("t_id") %>' runat="server" ItemStyle-Width="1%" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Debit" ItemStyle-BackColor="Transparent" ControlStyle-BackColor="Transparent"
ItemStyle-Width="29%">
<ItemTemplate>
<asp:Label ID="lb_debit" runat="server" Text='<%# Eval("t_debit") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="JD" ItemStyle-BackColor="Transparent" ControlStyle-BackColor="Transparent"
ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:Label ID="lbdebit_dinar" runat="server" Text='<%# Eval("t_debit_dinar") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fils" ItemStyle-BackColor="Transparent" ControlStyle-BackColor="Transparent"
ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:Label ID="lbdebit_pister" runat="server" Text='<%# Eval("t_debit_fils") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Credit" ItemStyle-BackColor="Transparent" ControlStyle-BackColor="Transparent"
ItemStyle-Width="29%">
<ItemTemplate>
<asp:Label ID="lb_credit" runat="server" Text='<%# Eval("t_credit") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="JD" ItemStyle-BackColor="Transparent" ControlStyle-BackColor="Transparent"
ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:Label ID="lbcredit_dinar" runat="server" Text='<%# Eval("t_credit_dinar") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fils" ItemStyle-BackColor="Transparent" ControlStyle-BackColor="Transparent"
ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:Label ID="lbcredit_pister" runat="server" Text='<%# Eval("t_credit_fils") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Transfer" ItemStyle-BackColor="Transparent" ControlStyle-BackColor="Transparent"
ItemStyle-Width="1%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="ch_transfer" runat="server" onclick="return false;" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindDummyRow();
}
}
private void BindDummyRow()
{
System.Data.DataTable dummy = new System.Data.DataTable();
dummy.Columns.Add("t_id");
dummy.Columns.Add("t_debit");
dummy.Columns.Add("t_debit_dinar");
dummy.Columns.Add("t_debit_fils");
dummy.Columns.Add("t_credit");
dummy.Columns.Add("t_credit_dinar");
dummy.Columns.Add("t_credit_fils");
dummy.Columns.Add("t_transfer");
dummy.Rows.Add();
gvTransaction.DataSource = dummy;
gvTransaction.DataBind();
}
[System.Web.Services.WebMethod]
public static string GetCustomers()
{
return GetData().GetXml();
}
private static System.Data.DataSet GetData()
{
System.Data.DataSet ds = new System.Data.DataSet();
System.Data.DataTable dt = new System.Data.DataTable();
dt.TableName = "trans";
dt.Columns.Add("t_id");
dt.Columns.Add("t_debit");
dt.Columns.Add("t_debit_dinar");
dt.Columns.Add("t_debit_fils");
dt.Columns.Add("t_credit");
dt.Columns.Add("t_credit_dinar");
dt.Columns.Add("t_credit_fils");
dt.Columns.Add("t_transfer");
dt.Rows.Add(1, 1000, 1, 0, 2000, 1, 0, 1);
dt.Rows.Add(2, 2000, 0, 1, 4000, 1, 0, 0);
dt.Rows.Add(3, 3000, 1, 0, 6000, 0, 1, 0);
dt.Rows.Add(4, 4000, 1, 1, 8000, 1, 1, 1);
ds.Tables.Add(dt);
return ds;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.BindDummyRow()
End If
End Sub
Private Sub BindDummyRow()
Dim dummy As System.Data.DataTable = New System.Data.DataTable()
dummy.Columns.Add("t_id")
dummy.Columns.Add("t_debit")
dummy.Columns.Add("t_debit_dinar")
dummy.Columns.Add("t_debit_fils")
dummy.Columns.Add("t_credit")
dummy.Columns.Add("t_credit_dinar")
dummy.Columns.Add("t_credit_fils")
dummy.Columns.Add("t_transfer")
dummy.Rows.Add()
gvTransaction.DataSource = dummy
gvTransaction.DataBind()
End Sub
<System.Web.Services.WebMethod()>
Public Shared Function GetCustomers() As String
Return GetData().GetXml()
End Function
Private Shared Function GetData() As System.Data.DataSet
Dim ds As System.Data.DataSet = New System.Data.DataSet()
Dim dt As System.Data.DataTable = New System.Data.DataTable()
dt.TableName = "trans"
dt.Columns.Add("t_id")
dt.Columns.Add("t_debit")
dt.Columns.Add("t_debit_dinar")
dt.Columns.Add("t_debit_fils")
dt.Columns.Add("t_credit")
dt.Columns.Add("t_credit_dinar")
dt.Columns.Add("t_credit_fils")
dt.Columns.Add("t_transfer")
dt.Rows.Add(1, 1000, 1, 0, 2000, 1, 0, 1)
dt.Rows.Add(2, 2000, 0, 1, 4000, 1, 0, 0)
dt.Rows.Add(3, 3000, 1, 0, 6000, 0, 1, 0)
dt.Rows.Add(4, 4000, 1, 1, 8000, 1, 1, 1)
ds.Tables.Add(dt)
Return ds
End Function
Screenshot