Sql server 如何从子窗体C更新父窗体上的datagridview#

Sql server 如何从子窗体C更新父窗体上的datagridview#,sql-server,winforms,c#-4.0,devexpress,xtragrid,Sql Server,Winforms,C# 4.0,Devexpress,Xtragrid,我是一个新的C#程序员,我开始使用C#已经有一个月了,到目前为止我可以说还不错,但我现在正在处理一个简单的情况,但我仍然无法让它工作,有这样一种情况: 我有两个表单,一个是父表单,一个是子表单,父表单包含一个Xtragrid控件和一个按钮,打开第二个表单并在第二个表单中加载带有值的文本框,第二个表单有一个按钮在发生任何更改时更新值。但我仍然无法让它工作,我有以下错误: 必须在@ID处声明标量变量 我知道问题的原因,但我就是解决不了。我已经做了一些研究来解决问题,但我还是没能成功 最后一行 da.

我是一个新的C#程序员,我开始使用C#已经有一个月了,到目前为止我可以说还不错,但我现在正在处理一个简单的情况,但我仍然无法让它工作,有这样一种情况:

我有两个表单,一个是父表单,一个是子表单,父表单包含一个Xtragrid控件和一个按钮,打开第二个表单并在第二个表单中加载带有值的文本框,第二个表单有一个按钮在发生任何更改时更新值。但我仍然无法让它工作,我有以下错误:

必须在@ID处声明标量变量

我知道问题的原因,但我就是解决不了。我已经做了一些研究来解决问题,但我还是没能成功 最后一行

da.updatecommand.parameters.addwithvalues(“ID@”,ds.tables[“tblLesseeYW”]。Rows[LesseeYW.Position][0];

我已经这样做了,但它不起作用(LesseeYW是我的绑定源对象,但它在当前上下文中不存在,也不存在可以理解的数据集) :有代码需要帮助

// This the class ive created to retrieve all Columns from the SQl server data base
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YoungWoman
 {
   public static class GetRowData
{

   public static int LesseeId;
   public static byte LesseePic;
   public static string LesseeName;
   public static string LesseeLastName;
   public static string PassportNo;
   public static string IDNo;
   public static DateTime BirthDate;
   public static string Gender;
   public static string Country;
   public static string City;
   public static string Province;
   public static string LesseePostalCode;
   public static string MobileNo;
   public static string HomePhoneNo;
   public static string TutorName;
   public static string TutorLastName;
   public static string AddressTutor;
   public static string AddressLessee;
   public static string TutorPhoneNo;
   public static string TutorEmail;




     }
   }


  // the parent form 


     namespace YoungWoman
   {

     public partial class Lessee2 : UserControl
    {
      DataSet ds = new DataSet();
       DataView dv ;
      SqlDataAdapter daLessee = new SqlDataAdapter();
      SqlDataAdapter daReservation = new SqlDataAdapter();
      BindingSource LesseeYW = new BindingSource();

       BindingSource ReservationCenterYW = new BindingSource();

       SqlConnection conne = SqlCoonectionSEtup.GetConnection;

// the button that opens the Child Form

    private void EditLesseeFrm_Click(object sender, EventArgs e)
    {
        Lesseefrm Lessee = new Lesseefrm(Utils.Formtype.edit, 1);
        Lessee.LesseeEventHandler += new EventHandler(RefreshLesseeGrid);

        GetRowData.LesseeId = Convert.ToInt32(gridView1.GetRowCellValue    (gridView1.FocusedRowHandle, "LesseeId"));
        GetRowData.LesseeName = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "LesseeName"));
        GetRowData.LesseeLastName = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"LesseeLastName"));
        GetRowData.PassportNo = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"PassportNo"));
        GetRowData.Gender = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Gender"));
        GetRowData.Province = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Province"));
        GetRowData.BirthDate = Convert.ToDateTime(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "BirthDate"));
        GetRowData.City = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "City"));
        GetRowData.Country = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Country"));
        GetRowData.MobileNo = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MobileNo"));
        GetRowData.HomePhoneNo = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "HomePhoneNo"));
        GetRowData.IDNo = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "IDNo"));
        GetRowData.AddressLessee = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "AddressLessee"));
        GetRowData.AddressTutor = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "AddressTutor"));
        GetRowData.LesseePostalCode = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "LesseePostalCode"));
        GetRowData.TutorName = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TutorName"));
        GetRowData.TutorLastName = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TutorLastName"));
        GetRowData.TutorPhoneNo = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TutorPhoneNo"));
        GetRowData.TutorEmail = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TutorEmail"));


        Lessee.ShowDialog();
    }



     ( // Child LOad_form if form type == Edit )


          if (formtype == Formtype.edit && Lesseeid > 0)
            {



                LesseeIdtextEdit.Enabled = false;
                ClearBtnlayoutControlItem26.Visibility =    DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                SaveBtn.Text = "&Edit";
                SaveBtn.Image = Resources.brush_16;
                this.Text = string.Format(" Edit Lessee Information - YW Residence ");

                LesseeIdtextEdit.Text = Convert.ToInt32(GetRowData.LesseeId).ToString();
                txtName.Text = GetRowData.LesseeName;
                txtLAstname.Text = GetRowData.LesseeLastName;
                txtPassport.Text = GetRowData.PassportNo;
                txtID.Text = GetRowData.IDNo;
                GendercomboBoxEdit.SelectedItem = GetRowData.Gender;
                DobdateEdit.DateTime = GetRowData.BirthDate;
                CountrycomboBoxEdit.SelectedItem = GetRowData.Country;
                txtProvince.Text = GetRowData.Province;
                txtCity.Text = GetRowData.City;
                txtPostalCode.Text = GetRowData.LesseePostalCode;
                LesseememoEdit1.Text = GetRowData.AddressLessee;
                txtMobile.Text = GetRowData.MobileNo;
                txtHomePhone.Text = GetRowData.HomePhoneNo;
                txtTutorName.Text = GetRowData.TutorName;
                txttutorLastname.Text = GetRowData.TutorLastName;
                tutorAddresstxt.Text = GetRowData.AddressTutor;
                txtTutorMobile.Text = GetRowData.TutorPhoneNo;
                txtEmail.Text = GetRowData.TutorEmail;



            }



     public event System.EventHandler LesseeEventHandler;

     private void SaveBtn_Click(object sender, EventArgs e)

       if (formtype == Formtype.edit && Lesseeid > 0)
            {

                MemoryStream ms = new MemoryStream();
                PicBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] Pic_arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(Pic_arr, 0, Pic_arr.Length);

                try
                {

                    da.UpdateCommand = new SqlCommand(" UPDATE LesseeYW SET LesseePic = @image , LesseeName = @Name, LesseeLastName = @Last , PassportNo = @pass,IDNo = @Number, BirthDate =@birth ,Gender = @gender , Country =@country,LesseePostalCode = @Postal,City = @city , Province = @province,MobileNo = @Mobile,HomePhoneNo = @phone,TutorName = @tutor,TutorLastName=@Tlast,AddressTutor = @line1,AddressLessee=@line2,TutorPhoneNo = @Tphone,TutorEmail =@Temail WHERE LesseeId = @ID ", conne);
                    da.UpdateCommand.Parameters.AddWithValue("@image", Pic_arr);
                    da.UpdateCommand.Parameters.AddWithValue("@Name", txtName.Text);
                    da.UpdateCommand.Parameters.AddWithValue("@Last", txtLAstname.Text);
                    da.UpdateCommand.Parameters.AddWithValue("@pass", txtPassport.Text);
                    da.UpdateCommand.Parameters.AddWithValue("@Number", txtID.Text);
                    da.UpdateCommand.Parameters.AddWithValue("@birth", DobdateEdit.DateTime);
                    da.UpdateCommand.Parameters.AddWithValue("@gender", GendercomboBoxEdit.SelectedItem.ToString());
                    da.UpdateCommand.Parameters.AddWithValue("@country", CountrycomboBoxEdit.SelectedItem.ToString());
                    da.UpdateCommand.Parameters.AddWithValue("@Postal", txtPostalCode.Text);
                    da.UpdateCommand.Parameters.AddWithValue("@city", txtCity.Text);
                    da.UpdateCommand.Parameters.AddWithValue("@province", txtProvince.Text);
                    da.UpdateCommand.Parameters.AddWithValue("@Mobile", txtMobile.Text);
                    da.UpdateCommand.Parameters.AddWithValue("@phone", txtHomePhone.Text);
                    da.UpdateCommand.Parameters.AddWithValue("@tutor", txtTutorName.Text);
                    da.UpdateCommand.Parameters.AddWithValue("@Tlast", txttutorLastname.Text);
                    da.UpdateCommand.Parameters.AddWithValue("@line1", tutorAddresstxt.Text);
                    da.UpdateCommand.Parameters.AddWithValue("@line2", LesseememoEdit1.Text);
                    da.UpdateCommand.Parameters.AddWithValue("@Tphone", txtTutorMobile.Text);
                    da.UpdateCommand.Parameters.AddWithValue("@Temail", txtEmail.Text);
                   //da.UpdateCommand.Parameters.AddWithValue("@ID"




                    da.UpdateCommand.ExecuteNonQuery();
                    conne.Close();
                    MessageBox.Show("Lessee Details Updated ", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();

                }

                catch (Exception Ex)
                {

                    MessageBox.Show(Ex.Message);

                }

            }

首先,你应该编写更客观的程序。不要将类中的所有变量都声明为公共静态变量。你应该将它们设置为私有变量。要获得访问权限,你必须声明属性

像这样:

private string name;

public string Name
{
  get {return name;}
  set {name = value;}
}
如果你有一个ID,你可以很容易地使它成为只读的,没有人可以修改它,但每个人都可以阅读

private int leeseId;

public int LeeseID
{
  get{return leeseId;}
}
所以你可以使用“getter/setter”来提供你的类属性。此外,不是所有的变量都应该命名为leese…只需将它命名为id、name、city、pic等。在你的表单中,你可以从你的类中创建一个对象。你可以将这个对象命名为leese

private Leese leese = new Leese();
leese.id = ???
leese.name = ???
更具可读性

da.updatecommand.parameters.addwithvalues("ID@",ds.tables["tblLesseeYW"].Rows[LesseeYW.Position][0];
您编写了“ID@”,但它应该是“@ID”。如果您按照我在顶部描述的方式构建类,则可以使用属性:

da.updatecommand.parameters.addwithvalues("@ID", leese.Id);

我希望这能对你有所帮助。

非常感谢,因为我告诉过你我还是新手,我会努力让计划更客观,我真的很感激。