一旦我更新了表格行并关闭了表格,我就会因刷新dataGridView而感到困惑和困惑。
我有2个表格。 MainForm
和EditAdminForm
。主窗体包含dataGridView,其中列出了Admin表。 EditAdminForm
由来自dataGridView的选定数据行填充。一旦更新,我关闭表格,但桌子不新鲜。
我尝试过一个简单的dataGridView.Refresh();
,但这不起作用。
MainForm
private void EditAdminBtn_Click(object sender, EventArgs e)
{
EditAdminForm Admin = new EditAdminForm();
Admin.idTxt.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
Admin.usernameTxt.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
Admin.firstnameTxt.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
Admin.surnameTxt.Text = this.dataGridView1.CurrentRow.Cells[3].Value.ToString();
Admin.emailTxt.Text = this.dataGridView1.CurrentRow.Cells[4].Value.ToString();
Admin.statusCombo.Text = this.dataGridView1.CurrentRow.Cells[6].Value.ToString();
Admin.ShowDialog();
}
EditAdminForm
private void SaveBtn_Click(object sender, EventArgs e)
{
//SQL Connection and SQL for updating admin information
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0; AttachDbFilename=C:\Users\Donald\Documents\Visual Studio 2013\Projects\DesktopApplication\DesktopApplication\Student_CB.mdf ;Integrated Security=True");
SqlDataAdapter sda3 = new SqlDataAdapter("UPDATE Admin set Admin_Username='" + this.usernameTxt.Text + "' , Admin_FName='" + this.firstnameTxt.Text + "' , Admin_SName='" + this.surnameTxt.Text + "' , Admin_Email='" + this.emailTxt.Text + "', Admin_Status='" + this.statusCombo.Text + "' WHERE Admin_ID='" + this.idTxt.Text + "'", con);
DataTable dt3 = new DataTable();
sda3.Fill(dt3);
MessageBox.Show("Information Successfully Updated!");
dt3.Clear();
this.Close();
}
加载表格数据
public void MainForm_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'student_CBDataSetStudent.Student' table. You can move, or remove it, as needed.
this.studentTableAdapter.Fill(this.student_CBDataSetStudent.Student);
// TODO: This line of code loads data into the 'student_CBDataSetAdmin.Admin' table. You can move, or remove it, as needed.
this.adminTableAdapter.Fill(this.student_CBDataSetAdmin.Admin);
}
答案 0 :(得分:0)
在您的编辑管理员表格中,您可以编辑"数据库"中的内容,而不是在您的表格中。 这意味着你必须在完成后重新加载表格。
将处理程序附加到"已关闭"主表单中的事件以及刷新说明。这将在表单关闭时自动触发更新:
private void EditAdminBtn_Click(object sender, EventArgs e)
{
EditAdminForm Admin = new EditAdminForm();
Admin.Closed += delegate(object sender, EventArgs e){
this.studentTableAdapter.Fill(this.student_CBDataSetStudent.Student);
this.adminTableAdapter.Fill(this.student_CBDataSetAdmin.Admin);
};
Admin.idTxt.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
Admin.usernameTxt.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
Admin.firstnameTxt.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
Admin.surnameTxt.Text = this.dataGridView1.CurrentRow.Cells[3].Value.ToString();
Admin.emailTxt.Text = this.dataGridView1.CurrentRow.Cells[4].Value.ToString();
Admin.statusCombo.Text = this.dataGridView1.CurrentRow.Cells[6].Value.ToString();
Admin.ShowDialog();
}
答案 1 :(得分:0)
有很多方法可以做到这一点,包括在EditAdminForm
上使用数据绑定,但最简单的方法可能就是更新SaveBtn_Click
方法中的行。从技术上讲,重新查询和重新填充DataGridView更准确,尽管并非总是必要的。如果更新成功,那么您可以直接通过在打开EditAdminForm
时存储行来更新行中的数据:
private void EditAdminBtn_Click(object sender, EventArgs e)
{
var admin = new EditAdminForm();
admin.CurrentRow = dataGridView1.CurrentRow;
admin.ShowDialog();
}
// EditAdminForm
private DataGridRow _currentRow;
public DataGridRow CurrentRow
{
get { return _currentRow; }
set
{
_currentRow = value;
if (value == null)
{
// TODO: Clear form
}
else
{
usernameTxt.Text = value.CurrentRow.Cells[1].Value.ToString();
firstnameTxt.Text = value.CurrentRow.Cells[2].Value.ToString();
surnameTxt.Text = value.CurrentRow.Cells[3].Value.ToString();
emailTxt.Text = value.Cells[4].Value.ToString();
statusCombo.Text = value.Cells[6].Value.ToString();
}
}
}
private void UpdateCurrentRow()
{
if (_currentRow == null) return;
_currentRow.Cells[0].Value = idTxt.Text;
_currentRow.Cells[1].Value = usernameTxt.Text;
_currentRow.Cells[2].Value = firstnameTxt.Text;
_currentRow.Cells[3].Value = surnameTxt.Text;
_currentRow.Cells[4].Value = emailTxt.Text;
_currentRow.Cells[6].Value = statusCombo.Text;
}
private void SaveBtn_Click(object sender, EventArgs e)
{
// Save connection, same as before...
// then update the row
UpdateCurrentRow();
this.Close();
}