Exemplo n.º 1
0
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int      id;
            Status   state;
            string   title;
            WorkType type;
            TimeSpan duration;
            decimal  rate;
            int      newId;

            if (dataGridView1.CurrentRow != null)
            {
                rowId = dataGridView1.CurrentRow.Index;
            }

            if (e.ColumnIndex == 6 && newRow) //save icon button is clicked
            {
                //save to list
                dataGridView1.Rows[e.RowIndex].Cells[0].Value = Status.Active;
                title    = dataGridView1.Rows[e.RowIndex].Cells[1].Value == null ? String.Empty : dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                type     = (WorkType)Enum.Parse(typeof(WorkType), dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString());
                duration = (TimeSpan)dataGridView1.Rows[e.RowIndex].Cells[3].Value;
                rate     = decimal.Parse(dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString());
                newId    = manager.Add(title, rate, duration, type);
                dataGridView1.Rows[e.RowIndex].Cells[8].Value = newId;

                newRow = false;

                dataGridView1.Rows[e.RowIndex].Cells[6].Value = Image.FromFile(Environment.CurrentDirectory + "/images/edit.png");
                dataGridView1.Rows[e.RowIndex].Cells[7].Value = Image.FromFile(Environment.CurrentDirectory + "/images/delete.png");
            }
            else if (e.ColumnIndex == 7 && newRow) //save icon button is clicked
            {
                //remove from datagrideview
                dataGridView1.Rows.RemoveAt(e.RowIndex);
            }
            else if (e.ColumnIndex == 6 && e.RowIndex >= 0 && newRow != true)
            {
                //enable edit
                id       = int.Parse(dataGridView1.Rows[e.RowIndex].Cells[8].Value.ToString());
                state    = (Status)Enum.Parse(typeof(Status), dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
                title    = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                type     = (WorkType)Enum.Parse(typeof(WorkType), dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString());
                duration = (TimeSpan)dataGridView1.Rows[e.RowIndex].Cells[3].Value;
                rate     = decimal.Parse(dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString());

                manager.Edit(id, state, title, rate, duration, type);
            }
            else if (e.ColumnIndex == 7 && e.RowIndex >= 0 && newRow != true)
            {
                DialogResult result = MessageBox.Show("Do you want to delete this record?", "Confirmation", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    //delete from the list
                    id = int.Parse(dataGridView1.Rows[e.RowIndex].Cells[8].Value.ToString());
                    if (manager.Delete(id))
                    {
                        dataGridView1.Rows.RemoveAt(e.RowIndex);
                    }
                }
            }
        }