如何编辑选定行但另存为新行(textBox、DataGridView、SQL)

Howto edit selected row but save as new row (textBox, DataGridView, SQL)

我对编码完全陌生,但我正在努力学习:) 在我正在处理的应用程序中,我有一个添加新行的按钮

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

然后我用另一个按钮从文本框中保存

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

我也可以编辑一行

Edit(true);

如何编辑一行,但在编辑后将其保存到新行而不是覆盖正在编辑的行?

或者,也许我应该将其更改为这样工作:

而不是 - 使用新按钮添加新行 - 填写文本框 - 使用保存按钮保存

这样做: - 填写文本框 - 使用保存按钮保存到新行

编辑: - 通过选择一行来填充文本框 - 在文本框中进行更改 - 使用 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;

然后更多 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);
            }
    }

尝试使用 dataGridView1_CellValidating - LINK。它在单元格失去输入焦点时发生,从而启用内容验证。因此,当验证是否要添加新行而不是编辑当前行时,只需从行中获取当前值并使用它来添加新行,并在验证结束时使用 e.Cancel 取消行编辑。

如果您想将某行的数据复制到文本框中,请在文本框中进行更改,然后使用按钮添加新行,您可以使用 dataGridView1_CellClick - LINK。所以基本上当用户单击单元格时,你会得到 row index of that cell,然后你可以访问该行的每个单元格。使用它填充文本框,进行一些更改,然后在保存按钮上添加新行。

如果您的问题也是如何向 datagridview 添加新行,您可以在此处找到答案:LINK