[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2011-09-08
5
Medium Priority
?
369 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

649 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