Solved

How to subtract 1 from a orginal count in an ASP.NET gridview

Posted on 2011-09-08
5
360 Views
Last Modified: 2013-12-17
I have a gridview that contains a count (whic is Quantity) were i have a button that adds a row under the orginal row and i need the sub row's count (Quantity) to subtract one from the orgianl row Quantity.
EX: Before button click
Orgianl row = 3
After click
Orginal row = 2
Subrow = 1
Code:
ASP.NET
 <asp:ButtonField  CommandName="btn_AddRow" Text="Add New Row" ButtonType="Button" />

Open in new window


Code C#:
 // FUNCTION : Adds a new subrow
        protected void gvParent_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("btn_AddRow", StringComparison.OrdinalIgnoreCase))
            {
                // Get the row that was clicked (index 0. Meaning that 0 is 1, 1 is 2 and so on)
                // Objects can be null, Int32s cannot not.
                // Int16 = 2 bytes long (short)
                // Int32 = 4 bytes long (int)
                // Int64 = 8 bytes long (long)
                int i = Convert.ToInt32(e.CommandArgument);

                // create a DataTable based off the view state
                DataTable dataTable = (DataTable)ViewState["gvParent"];

                for (int part = 0; part < dataTable.Rows.Count; part++)
                {
                    int oldQuantity = Convert.ToInt32(dataTable.Rows[i]["Quantity"]);
                    string partNumber = dataTable.Rows[i]["ProductDescription"].ToString();
                    string description = dataTable.Rows[i]["Description"].ToString();
                    string dateOrdered = dataTable.Rows[i]["GTRI_DateSubmittedtoPurchasing"].ToString();
                    string estShipDate = dataTable.Rows[i]["Gtri_EstShipDate"].ToString();
                    string actualShipDate = dataTable.Rows[i]["Gtri_ActualShipDate"].ToString();
                    string trackingNumb = dataTable.Rows[i]["GTRI_TrackingNumbers"].ToString();
                    string serialNumb = dataTable.Rows[i]["Gtri_SerialNumber"].ToString();

                    int oldQuantitySubtract = Convert.ToInt32(dataTable.Rows[part]["Quantity"]);
                    string curentPartNumbers = dataTable.Rows[part]["ProductDescription"].ToString();
                    string currentDescription = dataTable.Rows[part]["Description"].ToString();
                    string currentDateOrdered = dataTable.Rows[part]["GTRI_DateSubmittedtoPurchasing"].ToString();
                    string currentEstShipDate = dataTable.Rows[part]["Gtri_EstShipDate"].ToString();
                    string currentActualShipDate = dataTable.Rows[part]["Gtri_ActualShipDate"].ToString();
                    string currentTrackingNumb = dataTable.Rows[part]["GTRI_TrackingNumbers"].ToString();
                    string currentSerialNumb = dataTable.Rows[part]["Gtri_SerialNumber"].ToString();

                    if (partNumber.Equals(curentPartNumbers, StringComparison.OrdinalIgnoreCase) && oldQuantitySubtract > 1)
                    {
                        dataTable.Rows[part]["Quantity"] = oldQuantitySubtract - 1;

                        // Instert a new row at a specific index
                        DataRow dtAdd = dataTable.NewRow();

                        for (int k = 0; k < dataTable.Columns.Count; k++)
                        
                            dtAdd[k] = dataTable.Rows[part][k];
                            dataTable.Rows.InsertAt(dtAdd, i + 1);
                           
                            break;

                        //dataTable.Rows.Add(dtAdd);
                    }
                }
                // Rebind the data
                gvParent.DataSource = dataTable;
                gvParent.DataBind();
            }
        }

Open in new window

0
Comment
Question by:newjeep19
  • 3
5 Comments
 
LVL 13

Expert Comment

by:Rick
ID: 36506377
dataTable.Rows.Count - 1
0
 

Author Comment

by:newjeep19
ID: 36506463
This dataTable.Rows[part]["Quantity"] = oldQuantitySubtract - 1;
dose subtract from the quantity when i run degug and insert a breakpoint.
my issue is two things......first, when i click on the add row button the "subrow" is the same quantity as the orginal/parent row.
EX: After Add Sub Row button is clicked
Quantity(orgianl/parent row) = 3
Quantity(sub row) = 3

What i need to happpen
EX After Add Sub Row button is clicked
Quantity(orginal/parent row) =2
Quantity(sub row) = 1

next issue is.......I only whant the orginal/parent row to have the "Add Sub Row" button and not the sub row. Which dose at this point. I don't know how to code those two things to work. I am new to C# and ASP.NET and need help.....please.
Thanks
0
 
LVL 10

Expert Comment

by:Irzana
ID: 36507911
can you post your gridview markup ? aspx code
How do you differentiate sub row and parent row?
0
 

Accepted Solution

by:
newjeep19 earned 0 total points
ID: 36510783
Thank you for your response but I figured out the answer. Here is my code that solved the issue.
 // FUNCTION : Adds a new subrow
        protected void gvParent_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("btn_AddRow", StringComparison.OrdinalIgnoreCase))
            {
                // Get the row that was clicked (index 0. Meaning that 0 is 1, 1 is 2 and so on)
                // Objects can be null, Int32s cannot not.
                // Int16 = 2 bytes long (short)
                // Int32 = 4 bytes long (int)
                // Int64 = 8 bytes long (long)
                int i = Convert.ToInt32(e.CommandArgument);

                // create a DataTable based off the view state
                DataTable dataTable = (DataTable)ViewState["gvParent"];

                for (int part = 0; part < dataTable.Rows.Count; part++)
                {
                    int oldQuantity = Convert.ToInt32(dataTable.Rows[i]["Quantity"]);
                    string partNumber = dataTable.Rows[i]["ProductDescription"].ToString();
                    string description = dataTable.Rows[i]["Description"].ToString();
                    string dateOrdered = dataTable.Rows[i]["GTRI_DateSubmittedtoPurchasing"].ToString();
                    string estShipDate = dataTable.Rows[i]["Gtri_EstShipDate"].ToString();
                    string actualShipDate = dataTable.Rows[i]["Gtri_ActualShipDate"].ToString();
                    string trackingNumb = dataTable.Rows[i]["GTRI_TrackingNumbers"].ToString();
                    string serialNumb = dataTable.Rows[i]["Gtri_SerialNumber"].ToString();

                    int oldQuantitySubtract = Convert.ToInt32(dataTable.Rows[part]["Quantity"]);
                    string curentPartNumbers = dataTable.Rows[part]["ProductDescription"].ToString();
                    string currentDescription = dataTable.Rows[part]["Description"].ToString();
                    string currentDateOrdered = dataTable.Rows[part]["GTRI_DateSubmittedtoPurchasing"].ToString();
                    string currentEstShipDate = dataTable.Rows[part]["Gtri_EstShipDate"].ToString();
                    string currentActualShipDate = dataTable.Rows[part]["Gtri_ActualShipDate"].ToString();
                    string currentTrackingNumb = dataTable.Rows[part]["GTRI_TrackingNumbers"].ToString();
                    string currentSerialNumb = dataTable.Rows[part]["Gtri_SerialNumber"].ToString();

                    if (partNumber.Equals(curentPartNumbers, StringComparison.OrdinalIgnoreCase) && oldQuantitySubtract > 1)
                    {
                        dataTable.Rows[part]["Quantity"] = oldQuantitySubtract - 1;

                        // Instert a new row at a specific index
                        DataRow dtAdd = dataTable.NewRow();
                  
                        for (int k = 0; k < dataTable.Columns.Count; k++)
                            dtAdd[k] = dataTable.Rows[part][k];
                        dtAdd["Quantity"] = 1;
                        dataTable.Rows.InsertAt(dtAdd, i + 1);
                        break;
                    }
                }

                // Rebind the data
                gvParent.DataSource = dataTable;
                gvParent.DataBind();
            }
        }

Open in new window

0
 

Author Closing Comment

by:newjeep19
ID: 36534681
I figured out the answer. I am sharing my solution.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help deploying my first MVC.Net app with a SQL Server backend 3 25
hbo knew my windows software 4 60
RegEx with optional part 4 42
JSON Deserialize issue 6 26
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question