这是数据网格视图表单,单击添加成员按钮时将显示第二个表单。
private void btnAddMember_Click(object sender, EventArgs e)
{
Cashier_NewMember F_Cashier_NewMember = new Cashier_NewMember();
F_Cashier_NewMember.Show();
}
这是我将插入会员详情的表格。
private void btnAddMember_Click(object sender, EventArgs e)
{
try
{
if (sqlCon.State == ConnectionState.Closed)
sqlCon.Open();
if (btnAddMember.Text == "Add Member")
{
if (string.IsNullOrWhiteSpace(txtFirstName.Text) || string.IsNullOrWhiteSpace(txtLastName.Text) || string.IsNullOrWhiteSpace(txtMobileNo.Text) || string.IsNullOrWhiteSpace(txtHomeNo.Text) || string.IsNullOrWhiteSpace(txtAddress.Text) || string.IsNullOrWhiteSpace(txtPostcode.Text) || string.IsNullOrWhiteSpace(txtCity.Text) || string.IsNullOrWhiteSpace(txtCountry.Text))
MessageBox.Show("All fields are required!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
else if (string.IsNullOrWhiteSpace(txtMemberID.Text))
MessageBox.Show("Please generate a Member ID!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
else
{
SqlCommand sqlCmd = new SqlCommand("AddOrUpdateMembership", sqlCon);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.AddWithValue("@mode", "Add");
sqlCmd.Parameters.AddWithValue("@Member_ID", txtMemberID.Text);
sqlCmd.Parameters.AddWithValue("@Member_Since_Date", dtpMemberSince.Value.Date);
sqlCmd.Parameters.AddWithValue("@Title", cmbTitle.GetItemText(cmbTitle.SelectedItem));
sqlCmd.Parameters.AddWithValue("@First_Name", txtFirstName.Text.Trim());
sqlCmd.Parameters.AddWithValue("@Last_Name", txtLastName.Text.Trim());
sqlCmd.Parameters.AddWithValue("@DOB", dtpDOB.Value.Date);
sqlCmd.Parameters.AddWithValue("@Gender", cmbGender.GetItemText(cmbGender.SelectedItem));
sqlCmd.Parameters.AddWithValue("@Mobile_No", cmbMobileNo.GetItemText(cmbMobileNo.SelectedItem) + txtMobileNo.Text.Trim());
sqlCmd.Parameters.AddWithValue("@Home_No", cmbHomeNo.GetItemText(cmbHomeNo.SelectedItem) + txtHomeNo.Text.Trim());
sqlCmd.Parameters.AddWithValue("@House_Address", txtAddress.Text.Trim());
sqlCmd.Parameters.AddWithValue("@Post_Code", txtPostcode.Text.Trim());
sqlCmd.Parameters.AddWithValue("@City", txtCity.Text.Trim());
sqlCmd.Parameters.AddWithValue("@City_State", txtState.Text.Trim());
sqlCmd.Parameters.AddWithValue("@Country", txtCountry.Text.Trim());
sqlCmd.ExecuteNonQuery();
}
这是我用来填充数据网格视图的代码
public void FillDataGridView()
{
Cashier_NewMember F_Cashier_NewMember = new Cashier_NewMember();
if (sqlCon.State == ConnectionState.Closed)
sqlCon.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter("ViewOrSearchMembership", sqlCon);
sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
sqlDa.SelectCommand.Parameters.AddWithValue("@Member_ID", F_Cashier_NewMember.txtMemberID.Text);
DataTable dtbl = new DataTable();
sqlDa.Fill(dtbl);
dgvMembershipDetails.DataSource = dtbl;
sqlCon.Close();
}
这是我用来搜索和填充数据网格视图的存储过程代码。
CREATE PROCEDURE [dbo].[ViewOrSearchMembership]
@Member_ID VARCHAR(10)
AS
SELECT *
FROM Member
WHERE Member_ID LIKE @Member_ID + '%'
我尝试在任何地方添加 FillDataGridView(),但数据网格视图仍未更新。
答案 0 :(得分:2)
在分配数据源dgvMembershipDetails.update()
后尝试此操作答案 1 :(得分:1)
dgvMembershipDetails.Databind()应在分配数据源行后放置。
答案 2 :(得分:1)
确保你的dgvMembershipDetails修饰符是公开的,然后在填充之前尝试清除datagridview
public void FillDataGridView(){
Cashier_NewMember F_Cashier_NewMember = new Cashier_NewMember();
if (sqlCon.State == ConnectionState.Closed)
sqlCon.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter("ViewOrSearchMembership", sqlCon);
sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
sqlDa.SelectCommand.Parameters.AddWithValue("@Member_ID", F_Cashier_NewMember.txtMemberID.Text);
DataTable dtbl = new DataTable();
sqlDa.Fill(dtbl);
dgvMembershipDetails.DataSource =null;
dgvMembershipDetails.DataSource = dtbl;
sqlCon.Close();
}