Hi akhter,
Check this example. Now please take its reference and correct your code.
HTML
<div>
Cheque Amount<asp:TextBox ID="chqamount" runat="server" Text="500"></asp:TextBox>
<br />
Cheque No :<asp:TextBox ID="chequno" runat="server" Text="123"></asp:TextBox>
<asp:Button ID="btn_Adj" runat="server" Text="Adjust_Amount" OnClick="AdjustAmount" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText=" ID">
<ItemTemplate>
<asp:Label ID="I_ID" runat="server" Text='<%#Bind("I_ID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=" Amount">
<ItemTemplate>
<asp:Label ID="I_Amt" runat="server" Text='<%#Bind("I_Amt")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=" Cheque_NO">
<ItemTemplate>
<asp:TextBox ID="C_NO" runat="server" ReadOnly="true" Text='0'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=" Cheque_Amount">
<ItemTemplate>
<asp:TextBox ID="C_Amt" runat="server" ReadOnly="true" Text='0'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:DropDownList ID="ddlStatus" runat="server">
<asp:ListItem Text="Pending" Value="1" Selected="True"></asp:ListItem>
<asp:ListItem Text="Received" Value="2" />
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.AddRange(new System.Data.DataColumn[2] {
new System.Data.DataColumn("I_ID", typeof(int)),
new System.Data.DataColumn("I_Amt", typeof(int)) });
dt.Rows.Add(21, 339);
dt.Rows.Add(22, 622);
dt.Rows.Add(23, 226);
dt.Rows.Add(26, 339);
dt.Rows.Add(27, 339);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void AdjustAmount(object sender, EventArgs e)
{
int chequeAmount = Convert.ToInt32(chqamount.Text);
foreach (GridViewRow row in GridView1.Rows)
{
int id = Convert.ToInt32((row.FindControl("I_ID") as Label).Text);
int amount = Convert.ToInt32((row.FindControl("I_Amt") as Label).Text);
TextBox txtCheque = row.FindControl("C_NO") as TextBox;
TextBox txtAmount = row.FindControl("C_Amt") as TextBox;
DropDownList ddl = row.FindControl("ddlStatus") as DropDownList;
if (chequeAmount >= amount)
{
txtCheque.Text = chequno.Text;
txtAmount.Text = amount.ToString();
chequeAmount = chequeAmount - amount;
}
else if (chequeAmount < amount && chequeAmount > 0)
{
txtCheque.Text = chequno.Text;
txtAmount.Text = chequeAmount.ToString();
chequeAmount = 0;
}
else
{
txtCheque.Text = "0";
txtAmount.Text = "0";
}
ddl.ClearSelection();
if (amount == Convert.ToInt32(txtAmount.Text))
{
ddl.Items.FindByText("Received").Selected = true;
}
else
{
ddl.Items.FindByText("Pending").Selected = true;
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dt As System.Data.DataTable = New System.Data.DataTable()
dt.Columns.AddRange(New System.Data.DataColumn(1) {
New System.Data.DataColumn("I_ID", GetType(Integer)),
New System.Data.DataColumn("I_Amt", GetType(Integer))})
dt.Rows.Add(21, 339)
dt.Rows.Add(22, 622)
dt.Rows.Add(23, 226)
dt.Rows.Add(26, 339)
dt.Rows.Add(27, 339)
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Protected Sub AdjustAmount(ByVal sender As Object, ByVal e As EventArgs)
Dim chequeAmount As Integer = Convert.ToInt32(chqamount.Text)
For Each row As GridViewRow In GridView1.Rows
Dim id As Integer = Convert.ToInt32((TryCast(row.FindControl("I_ID"), Label)).Text)
Dim amount As Integer = Convert.ToInt32((TryCast(row.FindControl("I_Amt"), Label)).Text)
Dim txtCheque As TextBox = TryCast(row.FindControl("C_NO"), TextBox)
Dim txtAmount As TextBox = TryCast(row.FindControl("C_Amt"), TextBox)
Dim ddl As DropDownList = TryCast(row.FindControl("ddlStatus"), DropDownList)
If chequeAmount >= amount Then
txtCheque.Text = chequno.Text
txtAmount.Text = amount.ToString()
chequeAmount = chequeAmount - amount
ElseIf chequeAmount < amount AndAlso chequeAmount > 0 Then
txtCheque.Text = chequno.Text
txtAmount.Text = chequeAmount.ToString()
chequeAmount = 0
Else
txtCheque.Text = "0"
txtAmount.Text = "0"
End If
ddl.ClearSelection()
If amount = Convert.ToInt32(txtAmount.Text) Then
ddl.Items.FindByText("Received").Selected = True
Else
ddl.Items.FindByText("Pending").Selected = True
End If
Next
End Sub
Screenshot