Solved

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

Posted on 2011-09-08
5
356 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Replacing HTML tags in Textarea/Textbox 5 29
.net VBA word safemode 1 24
Round up to 100% in .NET 10 45
Asp.net mvc foreach 3 6
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now