0

Im totaly new to coding but im trying to learn:) In the application Im working on I have a button that adds a new row

Edit(true);
dbDocSet.DocData.AddDocDataRow(dbDocSet.DocData.NewDocDataRow());
docDataBindingSource.MoveLast();

and then I save from textboxes with another button

Edit(false);
docDataBindingSource.EndEdit();
docDataTableAdapter.Update(dbDocSet.DocData);
dataGridView1.Refresh();

I can also edit a row

Edit(true);

How can I edit a row but after edit save it to a new row instead of overwriting the one im editing?

Or, maybe I shold change it to work like this:

Instead of - Add new row with newbutton - Fill in textboxes - Save with savebutton

Do like this: - Fill in textboxes - Save to new row with savebutton

Edit: - Populate textboxes by selecting a row - Make changes in textboxes - Save to same row with changebuttonenter image description here

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Rectangle resolutionRect = System.Windows.Forms.Screen.FromControl(this).Bounds;
        if (this.Width >= resolutionRect.Width || this.Height >= resolutionRect.Height)
        {
            this.WindowState = FormWindowState.Maximized;
        }
        this.docDataTableAdapter.Fill(this.dbDocSet.DocData);
        Edit(false);
    }

    private void Edit(bool value)
    {
        textBox1.Enabled = value;
        textBox2.Enabled = value;
        textBox3.Enabled = value;

And then more textBox.Enable = value (143 st)

private void button1_Click(object sender, EventArgs e)
    { //-----Nytt dokument-----
        try
        {
            Edit(true);
            dbDocSet.DocData.AddDocDataRow(dbDocSet.DocData.NewDocDataRow());
            docDataBindingSource.MoveLast();
            textBox1.Focus();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            dbDocSet.DocData.RejectChanges();
        }

        for (int i = 0; i < dataGridView1.RowCount - 1; i++)

        {
            if (dataGridView1.Rows[i].Cells[0].Value.ToString() == "" || dataGridView1.Rows[i].Cells[1].Value.ToString() == "")

            {

                dataGridView1.Rows.RemoveAt(i);
                i--;
            }
        }
    }

    private void button3_Click(object sender, EventArgs e)
    { //-----Öppna upp för att kunna ändra-----
      Edit(true);
      textBox1.Focus();
    }

    private void button4_Click(object sender, EventArgs e)
    { //-----Avbryt ifyllnad dokument-----
        Edit(false);
        docDataBindingSource.ResetBindings(false);
    }

    private void button2_Click(object sender, EventArgs e)
    { //-----Spara dokument-----
        if (string.IsNullOrWhiteSpace(textBox1.Text))
        {
            MessageBox.Show("Dokumenttyp måste anges !", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            textBox1.Focus(); 
        }
        else
        if (string.IsNullOrWhiteSpace(textBox2.Text))
        {
            MessageBox.Show("Dokumentnamn måste anges !", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            textBox2.Focus();
        }
        else
        if (string.IsNullOrWhiteSpace(textBox3.Text))
        {
            MessageBox.Show("Revision för dokumentet måste anges !", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            textBox3.Focus();
        }
        else
        try
        {
            Edit(false);
            docDataBindingSource.EndEdit();
            docDataTableAdapter.Update(dbDocSet.DocData);
            dataGridView1.Refresh();
            textBox1.Focus();
            MessageBox.Show("Dokument sparat med lyckat resultat !", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            dbDocSet.DocData.RejectChanges();
        }
    }

    private void dataGridView1_KeyDown_1(object sender, KeyEventArgs e)
    { //-----Ta bort valt dokument-----
        if (e.KeyCode == Keys.Delete)
            foreach (DataGridViewCell oneCell in dataGridView1.SelectedCells)
            {
                if (oneCell.Selected)
                    if (MessageBox.Show("Är du säker på att du vill ta bort dokumentet ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        dataGridView1.Rows.RemoveAt(oneCell.RowIndex);
            }
    }
Tobbe
  • 13
  • 1
  • 3

1 Answers1

0

Try using dataGridView1_CellValidating - LINK. It occurs when a cell loses input focus, enabling content validation. So when validating if you want to add new row instead of editing current one, just get current values from row and use it to add new row, and at the end of validating use e.Cancel to cancel row edit.

If you want to copy data from some row to textboxes, make changes in textboxes and then with button add new row you can use dataGridView1_CellClick - LINK. So basically when user click cell, you get that row index of that cell, and then with that you can access every cell of that row. With that you populate textboxes, make some changes, and on save button you just add new row.

If your question is also how to add new row to datagridview you have answer here: LINK

Community
  • 1
  • 1
DoLoop
  • 125
  • 1
  • 2
  • 10
  • Now i do not understand you. Could you please screenshot your form so i know how it looks like. I thought that buttons function is to add new row to datagridview – DoLoop Mar 02 '17 at 19:00
  • @TobiasJohansson I think i am catching up what you want to do but you are doing it wrong. `Ny` button only need to enable textbox, while at `Spara` you save what you wrote in textboxes AND ADD NEW ROW. And the other to i do not see the point. – DoLoop Mar 02 '17 at 21:49
  • @TobiasJohansson i do not know if it is by rules to give it here, but if you want message me via mail `aristiccitsira@gmail.com` and i will help you. – DoLoop Mar 04 '17 at 17:31