Solved

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

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 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