我是编码的新手,但我试图学习:)在应用程序中,我有一个添加新行
的按钮Edit(true);
dbDocSet.DocData.AddDocDataRow(dbDocSet.DocData.NewDocDataRow());
docDataBindingSource.MoveLast();
,然后我从文本框中保存了另一个按钮
Edit(false);
docDataBindingSource.EndEdit();
docDataTableAdapter.Update(dbDocSet.DocData);
dataGridView1.Refresh();
我也可以编辑一行
Edit(true);
如何编辑一行,但是将其保存到新行之后,而不是覆盖IM编辑?
或者,也许我将其更改为这样的工作:
而不是 - 与newbutton一起添加新行 - 填写文本框 - 保存使用SaveButton
确实喜欢: - 填写文本框 - 保存到新的行中,使用savebutton
编辑: - 通过选择一行来填充文本框 - 更改文本框 - 使用changebuttonenter图像描述在此处保存到同一行
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
-链接。当一个单元格失去输入焦点时,它会发生,从而实现内容验证。因此,当验证是否要添加新行而不是编辑当前的一行时,只需从行中获取当前值并使用它添加新行,并且在验证结束时使用e.Cancel
来取消行编辑。
如果要将数据从某些行复制到文本框,请在文本框中进行更改,然后使用按钮添加新行,可以使用dataGridView1_CellClick
-link。因此,基本上,当用户单击单元单元时,您可以获得row index of that cell
,然后可以访问该行的每个单元格。随着您的填充文本框,进行一些更改,然后在保存按钮上,您只需添加新行即可。
如果您的问题也是如何将新行添加到DataGridView,您在此处回答:link