Solved

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

691 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