vendredi 1 mars 2019

dataGridView column referencing and nested if statements with CellContentDoubleClick event c#

Hi so i see a lot of posts re: cell referencing in a DGV and nested if statements. The post have got me soo far i am very nearly there or maybe not... i am hoping for some help and open to suggestions.

very simply i need to be able to double click on a certain cells in dgv, on double clicking the cell, if that cell is in certain column it applies a 1 or 0 to its relating column.

So, doubling clicking a cell in column 2 applies a figure one or zero depending to the same row but column 5.

so,column 2 relates to 5
      6 relates to 9
      10 relates to 13
      14 relates to 17
      18 relates to 21
      22 relates to 25

here is what i have so far..i have messed about alot and have taken it so far however i cannot get it to work independently. so, if i double click all columns change not the relating column. at present this doesn’t work. any help would be welcomed.

        private void dataGridViewAcorn_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
    {

        commandBuilder = new SqlCommandBuilder(dataAdapter);
        dataAdapter.UpdateCommand = commandBuilder.GetUpdateCommand();//get the update command



        string HW_A = dataGridViewAcorn.CurrentRow.Cells[5].Value.ToString();
        string Crwn_A = dataGridViewAcorn.CurrentRow.Cells[9].Value.ToString();
        string HB_A = dataGridViewAcorn.CurrentRow.Cells[13].Value.ToString();
        string OV_A = dataGridViewAcorn.CurrentRow.Cells[17].Value.ToString();
        string Acorn_A = dataGridViewAcorn.CurrentRow.Cells[21].Value.ToString();
        string Brn_A = dataGridViewAcorn.CurrentRow.Cells[25].Value.ToString();

        int columnIndex = dataGridViewAcorn.CurrentCell.ColumnIndex;


        if (columnIndex == 2)
        {
            if (HW_A == ("1"))

            {
                dataGridViewAcorn.CurrentRow.Cells[5].Value = 0;

            }
            else HW_A = ("0");

            {
                dataGridViewAcorn.CurrentRow.Cells[5].Value = 1;

            }


        }



        /*
        System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
        messageBoxCS.AppendFormat("{0} = {1}", "ColumnIndex", e.ColumnIndex);
        messageBoxCS.AppendLine();
        messageBoxCS.AppendFormat("{0} = {1}", "RowIndex", e.RowIndex);
        messageBoxCS.AppendLine();
        MessageBox.Show(messageBoxCS.ToString(), "CellConentDoubleClick Event");

        */

        dataGridViewAcorn.DataSource = bindingSourceAcorn;
        bindingSourceAcorn.EndEdit();// updates table in memory 
        dataAdapter.Update(table);//actually the data base 
                                  //MessageBox.Show("Update Successful!");




    }

Aucun commentaire:

Enregistrer un commentaire