细胞价值改变事件

时间:2012-08-20 12:54:17

标签: c# datagridview cells

当我向我的单元格提交值时,这可以正常工作,但是当我向另一个单元格提交另一个值时,它会更改这些单元格的值。我不希望它这样做,每个单元格应该有自己独特的价值,我该怎么做呢:

       private void retrieveData()
    {
        //returns values, creates and populates dgv
        try
        {
            string company, GetPartCodeV, logname, area;
            dgvDetials.DataSource = null; //resets dgv source to nothing so when refreshing no old data is contained
            dgvDetials.ColumnCount = 0; //removes all columns from dgv so none are repeated when refreshed


            logname = dtArea.Rows[cbArea.SelectedIndex]["logname"].ToString();

            dtArea3 = SQLMethods.GetAreaValues(logname);
            company = dtArea3.Rows[0]["company"].ToString();
            GetPartCodeV = dtArea3.Rows[0]["def_part_type"].ToString();

            DataTable dtGetPartDetails = SQLMethods.PartNo(company, GetPartCodeV); //gets production figures based on selected values

              dgvDetials.DataSource = dtGetPartDetails; //sets dgv source
          //  DataGridViewComboBoxColumn machCol = new DataGridViewComboBoxColumn(); //creates new combobox column for machine
          //  machCol.Name = "machine";
          //  machCol.DataSource = new string[] { "", "1", "2", "3", "4" }; //sets source for combobox column
          //  dgvDetials.Columns.Add(machCol); //adds machine column

            DataGridViewColumn qtyInsp = new DataGridViewColumn(); //creates new textbox column for quantity
            DataGridViewCell qtyCell = new DataGridViewTextBoxCell();
            qtyInsp.CellTemplate = qtyCell;
            qtyInsp.Name = "qty_insp";
            dgvDetials.Columns.Add(qtyInsp); //inserts new column into dgv

            DataGridViewColumn colScrapCount = new DataGridViewColumn(); //creates new textbow column for scrap count
            DataGridViewCell cellScrapCount = new DataGridViewTextBoxCell();
            colScrapCount.CellTemplate = cellScrapCount;
            colScrapCount.Name = "scrapCount";
            dgvDetials.Columns.Add(colScrapCount); //inserts new column

            DataGridViewColumn colReworkCount = new DataGridViewColumn(); //creates new textbox column for rework count
            DataGridViewCell cellReworkCount = new DataGridViewTextBoxCell();
            colReworkCount.CellTemplate = cellReworkCount;
            colReworkCount.Name = "reworkCount";
            dgvDetials.Columns.Add(colReworkCount); //inserts new column into dgv
            dtArea2 = SQLMethods.GetAreaCode(username,logname);
            area = dtArea2.Rows[0]["area_code"].ToString();

            DataGridViewColumn qtyrecorded = new DataGridViewColumn(); //creates new textbox column for quantity
            DataGridViewCell qtyRecCell = new DataGridViewTextBoxCell();
            qtyrecorded.CellTemplate = qtyRecCell;
            qtyrecorded.Name = "QtyRecorded";
            dgvDetials.Columns.Add(qtyrecorded); //inserts new column into dgv


            //populate scrap, rework and quantity counts
            foreach (DataGridViewRow row in dgvDetials.Rows) //loop through each of the results
            {
                string scrapDate, scrapShift, PartCode;
                scrapDate = dtpInspectionDate.Value.ToString("MM/dd/yyyy");
                scrapShift = cbShift.Text;

                PartCode = row.Cells[0].Value.ToString();

                //define each of the counts
                int qtyInspCount = SQLMethods.CountQtyInspTotal(scrapDate, scrapShift, area, PartCode);
                int scrapCount = SQLMethods.CountScrapReworkTotal(scrapDate, scrapShift, area, PartCode, "S");
                int reworkCount = SQLMethods.CountScrapReworkTotal(scrapDate, scrapShift, area, PartCode, "R");

                //populate each of the counts cells
                row.Cells[2].Value = 0;
                row.Cells[5].Value = qtyInspCount;
                row.Cells[3].Value = scrapCount;
                row.Cells[4].Value = reworkCount;
            }

            //format dgv
            //dgvDetials.Columns[0].ReadOnly = true; //sets cell to read only so cannot be edited
            //dgvDetials.Columns[0].HeaderText = "Die Number"; //column header
            //dgvDetials.Columns[0].Width = 80; //column width
            dgvDetials.Columns[1].ReadOnly = true;
            dgvDetials.Columns[1].HeaderText = "Die Description";
            dgvDetials.Columns[1].Width = 150;
            dgvDetials.Columns[0].ReadOnly = true;
            dgvDetials.Columns[0].HeaderText = "Part Code";
            dgvDetials.Columns[0].Width = 90;
       //   dgvDetials.Columns[2].HeaderText = "Machine";
       //   dgvDetials.Columns[2].Width = 80;
            dgvDetials.Columns[2].HeaderText = "Quantity Inspected";
            dgvDetials.Columns[2].Width = 90;
            dgvDetials.Columns[3].ReadOnly = true;
            dgvDetials.Columns[3].HeaderText = "Scrap";
            dgvDetials.Columns[3].Width = 80;
            dgvDetials.Columns[4].ReadOnly = true;
            dgvDetials.Columns[4].HeaderText = "Rework";
            dgvDetials.Columns[4].Width = 80;
            dgvDetials.Columns[5].ReadOnly = true;
            dgvDetials.Columns[5].HeaderText = "Quantity Inspected Recorded";
            dgvDetials.Columns[5].Width = 90;
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error retirieving data and setting up grid view. Processed with error: " + ex.Message);
        }
    }    //inserts figures into database
string area, inspDate, inspShift, partNo, dieCode, comment, machine;
int qtyInsp;

try
{
  foreach (DataGridViewRow row in dgvDetials.Rows) //loop through each of the rows in the dgv
  {
    int intQtyInsp;
    if (Convert.ToString(row.Cells[3].Value) == "") //if the machine column is empty
    {
      intQtyInsp = 0; //quantity inspected is 0. Allows for current quantity to be displayed while not reinserting
    }
    else if (row.Cells[4].Value.ToString() == "") //if quantity inspected is empty
    {
      intQtyInsp = 0; //quantity inspected is 0. Prevents null value errors.
    }
    else
    {
      intQtyInsp = Int32.Parse(row.Cells[4].Value.ToString()); //sets quantity inspected to value entered
    }

    if (intQtyInsp == 0) //if quantity inspected is 0. Ignore row.
    {
    }
    else //else gather details and insert row as production.
    {
      area = dtArea.Rows[cbArea.SelectedIndex]["area_code"].ToString();
      inspDate = dtpInspectionDate.Value.ToString("MM/dd/yyyy");
      inspShift = cbShift.Text;
      partNo = row.Cells[2].Value.ToString();
      dieCode = row.Cells[0].Value.ToString();
      qtyInsp = intQtyInsp;
      comment = "";
      machine = row.Cells[3].Value.ToString();

      SQLMethods.insertProduction(area, inspDate, inspShift, partNo, dieCode, qtyInsp, comment, machine);
    }
  }

  retrieveData(); //reset values
}
catch (Exception ex)
{
  MessageBox.Show("Error instering production values. Processed with error: " + ex.Message);
}

0 个答案:

没有答案