I have a gridview that contains a count (which is Quantity) where i have a button that adds a row under the original row and i need the sub row's count (Quantity) to subtract one from the orginal row Quantity.
EX: Before button click
Orgianl row = 3
After click
Orginal row = 2
Subrow = 1
Code
ASP.Net
/
// 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();
}