locked
Adjust Textbox amount in gridview row ,according to Amount RRS feed

  • Question

  • User-367318540 posted

    my requirement ,when i input cheque amount in (500) and Cheque No (123), then i press button of adjust amount,
    500 amount auto adjust below gridview ,if Amount and Cheque amount is equal then Status will be Received other wise Pending.

    Below is image link

    https://ibb.co/8zBRjtY

    <div>
                Cheque Amount<asp:TextBox ID="chqamount" runat="server"></asp:TextBox> Cheque No :<asp:TextBox ID="chequno" runat="server"></asp:TextBox> 
                <asp:Button ID="btn_Adj" runat="server" Text="Adjust_Amount" />
                <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" Text='0'></asp:TextBox>
                                    </ItemTemplate>
                 </asp:TemplateField>
                           <asp:TemplateField HeaderText=" Cheque_Amount">
                                    <ItemTemplate>
                                        <asp:TextBox ID="C_Amt" runat="server" Text='0'></asp:TextBox>
                                    </ItemTemplate>
                 </asp:TemplateField>
                            <asp:TemplateField HeaderText="Status">
                                    <ItemTemplate>
                                        <asp:Dropdownlist ID="C_Amt" runat="server" >
                                         
        <asp:ListItem Text="Pending" Value="1"></asp:ListItem>
                                                <asp:ListItem Text="Received" Value="2" />
                                        </asp:Dropdownlist>
    
                                    </ItemTemplate>
                 </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>

    Thursday, July 9, 2020 7:21 AM

Answers

  • User-1330468790 posted

    Hi akhterr,

      

    From my understanding, you want a function that could automatically fill the cheque amout into each row of below table until the amount reach the receivables (I_Amt).

      

    I constructed a demo but added some change to your codes.

    You could refer to below codes.

    .aspx Page:

     <div>
                <div>
                    Cheque No :<asp:TextBox ID="chequno" runat="server"></asp:TextBox>
                    Cheque Amount<asp:TextBox ID="chqamount" runat="server"></asp:TextBox>
                    <asp:Button ID="btn_Adj" runat="server" Text="Adjust_Amount" OnClick="btn_Adj_Click" />
                    <br />
                    <br />
    
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
    
                        <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" Text='<%#Bind("Cheq_No")%>'></asp:TextBox>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText=" Cheque_Amount">
                                <ItemTemplate>
                                    <asp:TextBox ID="C_Amt" runat="server" Text='<%#Bind("Display_Cheq_Amt")%>'></asp:TextBox>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Status">
                                <ItemTemplate>
                                    <asp:DropDownList ID="C_Status" runat="server">
                                        <asp:ListItem Text="Pending" Value="1"></asp:ListItem>
                                        <asp:ListItem Text="Received" Value="2"></asp:ListItem> 
                                    </asp:DropDownList>
    
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </div>
    
            </div>

    Code behind:

    // Simulation of the data
            private static DataTable _gridviewDT;
            public static DataTable GridviewDT
            {
                get
                {
                    if (_gridviewDT is null)
                    {
                        _gridviewDT = new DataTable();
    
                        _gridviewDT.Columns.Add("I_ID", typeof(int));
                        _gridviewDT.Columns.Add("I_Amt", typeof(int));
                        _gridviewDT.Columns.Add("Cheq_Amt", typeof(int));
                        _gridviewDT.Columns.Add("Cheq_No", typeof(string));
                        _gridviewDT.Columns.Add("Display_Cheq_Amt", typeof(string));
                        _gridviewDT.Columns.Add("Status", typeof(int));
    
                        _gridviewDT.Rows.Add(21, 339, 0, null, null, 1);
                        _gridviewDT.Rows.Add(22, 622, 0, null, null, 1);
                        _gridviewDT.Rows.Add(23, 226, 0, null, null, 1);
                        _gridviewDT.Rows.Add(26, 339, 0, null, null, 1);
                        _gridviewDT.Rows.Add(27, 339, 0, null, null, 1);
    
    
                    }
    
                    return _gridviewDT;
                }
                set
                {
                    _gridviewDT = value;
                }
            }
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindGridView();
                }
            }
    
            private void BindGridView()
            {
                GridView1.DataSource = GridviewDT;
                GridView1.DataBind();
            }
    
         
    
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                
                if(e.Row.RowType == DataControlRowType.DataRow)
                {
                    GridViewRow row = e.Row;
                    DropDownList ddl =(DropDownList)row.FindControl("C_Status");
                    DataRowView rowView = (DataRowView)e.Row.DataItem;
    
                    ddl.SelectedValue = rowView["Status"].ToString();
                }
    
                
    
            }
    
            protected void btn_Adj_Click(object sender, EventArgs e)
            {
                string new_cheq_no = chequno.Text;
                int cheq_amt_balance = Convert.ToInt32(chqamount.Text);
    
                while(cheq_amt_balance > 0)
                {
                    IEnumerable<DataRow> drs = GridviewDT.Select("Status=1");
                    if(drs.Count() != 0)
                    {
                        DataRow dr = drs.First();
    
                        int cheq_amt = Convert.ToInt32(dr["Cheq_Amt"]);
                        int i_amt = Convert.ToInt32(dr["I_Amt"]);
                        string display_cheq_amt = dr["Display_Cheq_Amt"].ToString();
                        string cheq_no = dr["Cheq_No"].ToString();
    
    
                        int add_cheq_amt = 0;
                        if (cheq_amt_balance >= (i_amt - cheq_amt))
                        {
                            add_cheq_amt = i_amt - cheq_amt;
                            // Here should update database for Status
                            dr["Status"] = 2;
                        }
                        else
                        {
                            add_cheq_amt = cheq_amt_balance;
                        }
    
                        // Here should update database for Status
                        dr["Cheq_Amt"] = cheq_amt + add_cheq_amt;
                        dr["Display_Cheq_Amt"] = display_cheq_amt + "[" + add_cheq_amt.ToString() + "]";
                        dr["Cheq_No"] = cheq_no + "[" + new_cheq_no + "]";
    
    
                        cheq_amt_balance -= add_cheq_amt;
                    } else
                    {
                        cheq_amt_balance = 0 ;
                    }
                    
                }
    
                BindGridView();
            }

    Demo:

     

    Hope this can help you.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 9, 2020 9:51 AM

All replies

  • User-1330468790 posted

    Hi akhterr,

      

    From my understanding, you want a function that could automatically fill the cheque amout into each row of below table until the amount reach the receivables (I_Amt).

      

    I constructed a demo but added some change to your codes.

    You could refer to below codes.

    .aspx Page:

     <div>
                <div>
                    Cheque No :<asp:TextBox ID="chequno" runat="server"></asp:TextBox>
                    Cheque Amount<asp:TextBox ID="chqamount" runat="server"></asp:TextBox>
                    <asp:Button ID="btn_Adj" runat="server" Text="Adjust_Amount" OnClick="btn_Adj_Click" />
                    <br />
                    <br />
    
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
    
                        <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" Text='<%#Bind("Cheq_No")%>'></asp:TextBox>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText=" Cheque_Amount">
                                <ItemTemplate>
                                    <asp:TextBox ID="C_Amt" runat="server" Text='<%#Bind("Display_Cheq_Amt")%>'></asp:TextBox>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Status">
                                <ItemTemplate>
                                    <asp:DropDownList ID="C_Status" runat="server">
                                        <asp:ListItem Text="Pending" Value="1"></asp:ListItem>
                                        <asp:ListItem Text="Received" Value="2"></asp:ListItem> 
                                    </asp:DropDownList>
    
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </div>
    
            </div>

    Code behind:

    // Simulation of the data
            private static DataTable _gridviewDT;
            public static DataTable GridviewDT
            {
                get
                {
                    if (_gridviewDT is null)
                    {
                        _gridviewDT = new DataTable();
    
                        _gridviewDT.Columns.Add("I_ID", typeof(int));
                        _gridviewDT.Columns.Add("I_Amt", typeof(int));
                        _gridviewDT.Columns.Add("Cheq_Amt", typeof(int));
                        _gridviewDT.Columns.Add("Cheq_No", typeof(string));
                        _gridviewDT.Columns.Add("Display_Cheq_Amt", typeof(string));
                        _gridviewDT.Columns.Add("Status", typeof(int));
    
                        _gridviewDT.Rows.Add(21, 339, 0, null, null, 1);
                        _gridviewDT.Rows.Add(22, 622, 0, null, null, 1);
                        _gridviewDT.Rows.Add(23, 226, 0, null, null, 1);
                        _gridviewDT.Rows.Add(26, 339, 0, null, null, 1);
                        _gridviewDT.Rows.Add(27, 339, 0, null, null, 1);
    
    
                    }
    
                    return _gridviewDT;
                }
                set
                {
                    _gridviewDT = value;
                }
            }
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindGridView();
                }
            }
    
            private void BindGridView()
            {
                GridView1.DataSource = GridviewDT;
                GridView1.DataBind();
            }
    
         
    
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                
                if(e.Row.RowType == DataControlRowType.DataRow)
                {
                    GridViewRow row = e.Row;
                    DropDownList ddl =(DropDownList)row.FindControl("C_Status");
                    DataRowView rowView = (DataRowView)e.Row.DataItem;
    
                    ddl.SelectedValue = rowView["Status"].ToString();
                }
    
                
    
            }
    
            protected void btn_Adj_Click(object sender, EventArgs e)
            {
                string new_cheq_no = chequno.Text;
                int cheq_amt_balance = Convert.ToInt32(chqamount.Text);
    
                while(cheq_amt_balance > 0)
                {
                    IEnumerable<DataRow> drs = GridviewDT.Select("Status=1");
                    if(drs.Count() != 0)
                    {
                        DataRow dr = drs.First();
    
                        int cheq_amt = Convert.ToInt32(dr["Cheq_Amt"]);
                        int i_amt = Convert.ToInt32(dr["I_Amt"]);
                        string display_cheq_amt = dr["Display_Cheq_Amt"].ToString();
                        string cheq_no = dr["Cheq_No"].ToString();
    
    
                        int add_cheq_amt = 0;
                        if (cheq_amt_balance >= (i_amt - cheq_amt))
                        {
                            add_cheq_amt = i_amt - cheq_amt;
                            // Here should update database for Status
                            dr["Status"] = 2;
                        }
                        else
                        {
                            add_cheq_amt = cheq_amt_balance;
                        }
    
                        // Here should update database for Status
                        dr["Cheq_Amt"] = cheq_amt + add_cheq_amt;
                        dr["Display_Cheq_Amt"] = display_cheq_amt + "[" + add_cheq_amt.ToString() + "]";
                        dr["Cheq_No"] = cheq_no + "[" + new_cheq_no + "]";
    
    
                        cheq_amt_balance -= add_cheq_amt;
                    } else
                    {
                        cheq_amt_balance = 0 ;
                    }
                    
                }
    
                BindGridView();
            }

    Demo:

     

    Hope this can help you.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 9, 2020 9:51 AM
  • User-367318540 posted

    HI Sean.

    here is getting null error

    Object reference not set to an instance of an object.

    while (cheq_amt_balance > 0)
    Line 91:             {
    Line 92:                 IEnumerable<DataRow> drs = _gridviewDT.Select("C_Status=1");
    Line 93:                 if (drs.Count() != 0)
    Line 94:                 {

    Below is code

     private static DataTable _gridviewDT;
            public static DataTable GridviewDT
            {
                get
                {
                    if (_gridviewDT is null)
                    {
    
                        _gridviewDT = new DataTable();
    
                        _gridviewDT.Columns.Add("I_ID", typeof(int));
                        _gridviewDT.Columns.Add("I_Amt", typeof(int));
                        _gridviewDT.Columns.Add("Cheq_Amt", typeof(int));
                        _gridviewDT.Columns.Add("Cheq_No", typeof(string));
                        _gridviewDT.Columns.Add("Display_Cheq_Amt", typeof(string));
                        _gridviewDT.Columns.Add("C_Status", typeof(int));
    
                    }
                    return _gridviewDT;
                }
                set
                {
                    _gridviewDT = value;
                }
            }
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    loadgv();
    
                }
           
            }
    
            private void loadgv()
            {
                SqlCommand cmdbno = new SqlCommand("[SP_Inv_Ret_For_Cheque]", con);
    
                cmdbno.CommandType = CommandType.StoredProcedure;
                //cmdbno.Parameters.AddWithValue("PID", txtpackno.Text);
                SqlDataAdapter adptbno = new SqlDataAdapter(cmdbno);
                DataTable GridviewDT = new DataTable();
                adptbno.Fill(GridviewDT);
                GridView1.DataSource = GridviewDT;
                GridView1.DataBind();
                cmdbno.Dispose();
                con.Close();
            }
    
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    GridViewRow row = e.Row;
                    DropDownList ddl = (DropDownList)row.FindControl("C_Status");
                    DataRowView rowView = (DataRowView)e.Row.DataItem;
    
                    ddl.SelectedValue = rowView["C_Status"].ToString();
                }
    
    
            }
    
            protected void btn_Adj_Click(object sender, EventArgs e)
            {
                string new_cheq_no = chequno.Text;
                int cheq_amt_balance = Convert.ToInt32(chqamount.Text);
    
                while (cheq_amt_balance > 0)
                {
                    IEnumerable<DataRow> drs = _gridviewDT.Select("C_Status=1");
                    if (drs.Count() != 0)
                    {
                        DataRow dr = drs.First();
    
                        int cheq_amt = Convert.ToInt32(dr["Cheq_Amt"]);
                        int i_amt = Convert.ToInt32(dr["I_Amt"]);
                        string display_cheq_amt = dr["Display_Cheq_Amt"].ToString();
                        string cheq_no = dr["Cheq_No"].ToString();
    
    
                        int add_cheq_amt = 0;
                        if (cheq_amt_balance >= (i_amt - cheq_amt))
                        {
                            add_cheq_amt = i_amt - cheq_amt;
                            // Here should update database for Status
                            dr["C_Status"] = 2;
                        }
                        else
                        {
                            add_cheq_amt = cheq_amt_balance;
                        }
    
                        // Here should update database for Status
                        dr["Cheq_Amt"] = cheq_amt + add_cheq_amt;
                        dr["Display_Cheq_Amt"] = display_cheq_amt + "[" + add_cheq_amt.ToString() + "]";
                        dr["Cheq_No"] = cheq_no + "[" + new_cheq_no + "]";
    
    
                        cheq_amt_balance -= add_cheq_amt;
                    }
                    else
                    {
                        cheq_amt_balance = 0;
                    }
    
                }
    
                loadgv();
    
            }
        }
    }

    Thursday, July 9, 2020 4:54 PM
  • User-1330468790 posted

    Hi akhterr,

     

    The simulation of the in-memory database I used is the static property "GridviewDT" not the private one "_gridviewDT". The initialization and modification is completed through the former one "GridviewDT".  Therefore, you have to use "GridviewDT" in your code then the exception will be disappeared.

     

    There are still some places in codes to be improved. You might need to do this after the demo works.

    For example, the input should be checked for the cheque number and cheque amount in case the user inputs are not numbers.

      

    Best regards,

    Sean

     

    Friday, July 10, 2020 1:44 AM
  • User-367318540 posted

    HI Sean,

    giving below error now

    Cannot find column [C_Status].

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Data.EvaluateException: Cannot find column [C_Status].

    Source Error:

    Line 80:             while (cheq_amt_balance > 0)
    Line 81:             {
    Line 82:                 IEnumerable<DataRow> drs = dt.Select("C_Status=1");
    Line 83:                 if (drs.Count() != 0)
    Line 84:                 {
     DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    loadgv();
    
                    dt.Columns.Add("I_ID", typeof(int));
                    dt.Columns.Add("I_Amt", typeof(int));
                    dt.Columns.Add("Cheq_Amt", typeof(int));
                    dt.Columns.Add("Cheq_No", typeof(string));
                    dt.Columns.Add("Display_Cheq_Amt", typeof(string));
                    dt.Columns.Add("C_Status", typeof(int));
                    ViewState["dt"] = dt;
    
                }
           
            }
    
            private void loadgv()
            {
                SqlCommand cmdbno = new SqlCommand("[SP_Inv_Ret_For_Cheque]", con);
    
                cmdbno.CommandType = CommandType.StoredProcedure;
                //cmdbno.Parameters.AddWithValue("PID", txtpackno.Text);
                SqlDataAdapter adptbno = new SqlDataAdapter(cmdbno);
                DataTable dt = new DataTable();
                adptbno.Fill(dt);
                GridView1.DataSource = dt;
                GridView1.DataBind();
                cmdbno.Dispose();
                con.Close();
            }
    
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    GridViewRow row = e.Row;
                    DropDownList ddl = (DropDownList)row.FindControl("C_Status");
                    DataRowView rowView = (DataRowView)e.Row.DataItem;
    
                    ddl.SelectedValue = rowView["C_Status"].ToString();
                }
    
    
            }
    
            protected void btn_Adj_Click(object sender, EventArgs e)
            {
                string new_cheq_no = chequno.Text;
                int cheq_amt_balance = Convert.ToInt32(chqamount.Text);
    
                while (cheq_amt_balance > 0)
                {
                    IEnumerable<DataRow> drs = dt.Select("C_Status=1");
                    if (drs.Count() != 0)
                    {
                        DataRow dr = drs.First();
    
                        int cheq_amt = Convert.ToInt32(dr["Cheq_Amt"]);
                        int i_amt = Convert.ToInt32(dr["I_Amt"]);
                        string display_cheq_amt = dr["Display_Cheq_Amt"].ToString();
                        string cheq_no = dr["Cheq_No"].ToString();
    
    
                        int add_cheq_amt = 0;
                        if (cheq_amt_balance >= (i_amt - cheq_amt))
                        {
                            add_cheq_amt = i_amt - cheq_amt;
                            // Here should update database for Status
                            dr["C_Status"] = 2;
                        }
                        else
                        {
                            add_cheq_amt = cheq_amt_balance;
                        }
    
                        // Here should update database for Status
                        dr["Cheq_Amt"] = cheq_amt + add_cheq_amt;
                        dr["Display_Cheq_Amt"] = display_cheq_amt + "[" + add_cheq_amt.ToString() + "]";
                        dr["Cheq_No"] = cheq_no + "[" + new_cheq_no + "]";
    
    
                        cheq_amt_balance -= add_cheq_amt;
                    }
                    else
                    {
                        cheq_amt_balance = 0;
                    }
    
                }
    
                loadgv();
    
            }
        }
    }

    Friday, July 10, 2020 8:45 AM
  • User-1330468790 posted

    Hi akhterr,

     

    The C_Status is what I am using to save the payment status for each rows. If the status is 1 then the row is "Received", otherwise, "Pending". 

    However, the real implementation should be consistent with your design.

    I think you don't have this column in your database so what is the similar column to mark the result of the cheque payment?

    You could use that column name in your database to map the data in the GridView Control. 

      

    Probably you still have concerns about the codes I provided you with, feel free to let me know. 

    Here is an option that you could post the database schema and we could modify the codes accordingly.

     

    Best regards,

    Sean

    Monday, July 13, 2020 6:57 AM