我有一个表单(customersForm)显示带有客户信息的datagridview。第二种形式(viewForm)允许用户查看,编辑,删除和更新所选的datagridview行。当我点击更新,或删除我喜欢customerForm上的datagridview来刷新显示更新的数据。如何通过单击按钮来执行此操作?
这是viewForms完整代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
namespace Lewis_Warby_Airbrushing
{
public partial class viewForm : Form
{
DataRowView Data = null;
public viewForm(DataRowView dr)
{
InitializeComponent();
Data = dr;
}
private void closeBTN_Click(object sender, EventArgs e)
{
this.Close();
}
private void viewForm_Load(object sender, EventArgs e)
{
refTxt.Text = Data["Reference"].ToString().Trim();
firstTxt.Text = Data["First Name"].ToString().Trim();
surenameTxt.Text = Data["Surename"].ToString().Trim();
address1Txt.Text = Data["Address Line 1"].ToString().Trim();
address2Txt.Text = Data["Address Line 2"].ToString().Trim();
countyTxt.Text = Data["County"].ToString().Trim();
postTxt.Text = Data["Post Code"].ToString().Trim();
contactTxt.Text = Data["Contact Number"].ToString().Trim();
emailTxt.Text = Data["Email Address"].ToString().Trim();
}
private void deleteBTN_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Customer information will be perminantly deteled. Do you with to continue? ", "Confirm Delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
string constring = @"Data Source=|DataDirectory|\LWADataBase.sdf";
string Query = "delete from customersTBL where Reference ='" + this.refTxt.Text + "';";
SqlCeConnection conDataBase = new SqlCeConnection(constring);
SqlCeCommand cmdDataBase = new SqlCeCommand(Query, conDataBase);
SqlCeDataReader myReader;
try
{
conDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
MessageBox.Show("Customer information has been deleted", "Deleted Sucessfully");
while (myReader.Read())
{
}
MessageBox.Show("Please exit the Customers window and re-open to update the table");
this.Close();
//displays a system error message if a problem is found
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void editBTN_Click(object sender, EventArgs e)
{
bool notEditable = true;
if (editBTN.Text == "Update")
{
int UserID = Convert.ToInt32(refTxt.Text);
UpdateDataBase( UserID );
editBTN.Text = "Edit";
deleteBTN.Visible = true;
notEditable = true;
}
else
{
deleteBTN.Visible = false;
editBTN.Text = "Update";
deleteBTN.Visible = false;
notEditable = false;
}
firstTxt.ReadOnly = notEditable;
surenameTxt.ReadOnly = notEditable;
address1Txt.ReadOnly = notEditable;
address2Txt.ReadOnly = notEditable;
countyTxt.ReadOnly = notEditable;
contactTxt.ReadOnly = notEditable;
emailTxt.ReadOnly = notEditable;
postTxt.ReadOnly = notEditable;
}
private void UpdateDataBase(int customerID)
{
if (MessageBox.Show("Customer information will be updated. This change cannot be undone. Are you sure you want to continue? ", "Confirm Edit", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
string constring = @"Data Source=|DataDirectory|\LWADataBase.sdf";
string Query = @"update customersTBL set [First Name] = @fname,
surename = @sur, [Address Line 1] = @addr1,
[Address Line 2] = @addr2, County = @county,
[Post Code] = @pcode, [Email Address] = @mail, [Contact Number] = @ctNo
WHERE Reference = @id";
using (SqlCeConnection conDataBase = new SqlCeConnection(constring))
using (SqlCeCommand cmdDataBase = new SqlCeCommand(Query, conDataBase))
{
try
{
conDataBase.Open();
cmdDataBase.Parameters.AddWithValue("@fname", this.firstTxt.Text);
cmdDataBase.Parameters.AddWithValue("@sur", this.surenameTxt.Text);
cmdDataBase.Parameters.AddWithValue("@addr1", this.address1Txt.Text);
cmdDataBase.Parameters.AddWithValue("@addr2", this.address2Txt.Text);
cmdDataBase.Parameters.AddWithValue("@county", this.countyTxt.Text);
cmdDataBase.Parameters.AddWithValue("@pcode", this.postTxt.Text);
cmdDataBase.Parameters.AddWithValue("@mail", this.emailTxt.Text);
cmdDataBase.Parameters.AddWithValue("@ctNo", this.contactTxt.Text);
cmdDataBase.Parameters.AddWithValue("@id", customerID);
int rowsUpdated = cmdDataBase.ExecuteNonQuery();
if (rowsUpdated == 0)
MessageBox.Show("No customer found to update");
MessageBox.Show("Customer information sucessfully updated", "Update Sucessfull");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
customerForm的完整代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
namespace Lewis_Warby_Airbrushing
{
public partial class customerForm : Form
{
public customerForm()
{
InitializeComponent();
}
public void customerForm_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'lWADataBaseDataSet.customersTBL' table. You can move, or remove it, as needed.
timer1.Start();
this.lWADataBaseDataSet.EnforceConstraints = false;
this.customersTBLTableAdapter.Fill(this.lWADataBaseDataSet.customersTBL);
}
private void viewBTN_Click(object sender, EventArgs e)
{
int selectedRowIndex = customersTBLDataGridView.SelectedCells[0].RowIndex;
DataGridViewRow selectedRow = customersTBLDataGridView.Rows[selectedRowIndex];
viewForm frm2 = new viewForm((DataRowView)selectedRow.DataBoundItem);
frm2.ShowDialog();
}
addForm addForm = new addForm();
private void addBTN_Click(object sender, EventArgs e)
{
if (addForm == null || addForm.IsDisposed == true)
addForm = new addForm();
addForm.ShowDialog();
this.customersTBLTableAdapter.Fill(this.lWADataBaseDataSet.customersTBL);
}
int count = 0;
private void timer1_Tick(object sender, EventArgs e)
{
count = customersTBLBindingSource.Count;
statusLBL.Text = "You currently have "+count.ToString()+" customer(s) stored in this database";
}
searchForm searchForm = new searchForm();
private void searchBTN_Click(object sender, EventArgs e)
{
if (searchForm == null || searchForm.IsDisposed == true);
searchForm = new searchForm();
searchForm.ShowDialog();
}
}
}
答案 0 :(得分:1)
您的问题中没有指定很多内容,但让我们假设这种情况:
显示customerView时,您调用数据库以使用客户详细信息填充DataGridView。在某些时候,您可以通过dbl单击DGV行,或者选择一行并单击一个按钮,然后显示viewForm,传入当前所选行中的数据。同样,您没有指定如何完成此操作,但我们假设您传入了某种数据传输对象。
完成编辑后,单击按钮,将更改保存到数据库,然后关闭viewForm。
根据您的意见,这是您现在应用程序中的常规工作流程。
在这种情况下,您可以简单地重新获取数据,就像在viewForm.ShowDialog()方法返回时首次显示customerView时所做的那样。
除非有其他我无法得到的东西,否则可以这样轻松完成:
//customerForm
private void button1_Click(object sender, EventArgs e)
{
ViewFormClass viewForm = new ViewFormClass();
viewForm.SetCustomerData(dataObject);
viewForm.ShowDialog(); // will stop here waiting for viewForm to close
this.FetchCustomerData();
}
其中FetchCustomerData()
与打开customerView时调用的方法相同。在那里,您通常会获取数据并将其绑定到控件。
干杯
编辑:
根据您自己的代码,通过简单的修改:
private void viewBTN_Click(object sender, EventArgs e)
{
int selectedRowIndex = customersTBLDataGridView.SelectedCells[0].RowIndex;
DataGridViewRow selectedRow = customersTBLDataGridView.Rows[selectedRowIndex];
viewForm frm2 = new viewForm((DataRowView)selectedRow.DataBoundItem);
frm2.ShowDialog();
this.customersTBLTableAdapter.Fill(this.lWADataBaseDataSet.customersTBL);
}