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);
}
}