2012-07-15 51 views
0

我是一個新的C#程序員,它已經開始使用C#的一個月,到目前爲止我可以說很好,但即時處理一個簡單的情況,但我還沒有得到它的工作,有這種情況:如何更新父窗體上的DataGridView從一個子窗體C#

我有2個窗體父和子,父包含一個Xtragrid控件和一個按鈕,打開第二個窗體並加載第二種形式的文本框帶有值,第二種帶有一個按鈕,用於在發生任何更改時更新值。但我仍然沒有得到它的工作,我有以下錯誤:

必須聲明標量變量在@ID

我理解probleme的原因,但我只是不能修復它,香港專業教育學院做了一些研究,我自己整理出來,但我仍然didnt管理,使其工作 最後一行

da.updatecommand.parameters.addwithvalues("[email protected]",ds.tables["tblLesseeYW"].Rows[LesseeYW.Position][0];

我已經做到了,但它不工作(LesseeYW這是我結合源碼Ë對象,但在目前情況下它不退出,也沒有這是可以理解 數據集:有代碼需要幫助pleaese

// 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 [email protected] ,Gender = @gender , Country [email protected],LesseePostalCode = @Postal,City = @city , Province = @province,MobileNo = @Mobile,HomePhoneNo = @phone,TutorName = @tutor,[email protected],AddressTutor = @line1,[email protected],TutorPhoneNo = @Tphone,TutorEmail [email protected] 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); 

       } 

      } 

回答

0

首先你應該計劃的目標。不要將你的類的所有變量聲明爲公共靜態。你應該讓它們保密。要獲得訪問權限,你必須聲明屬性。

像這樣:

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,名稱,城市,圖片等。在您的表單中,您可以從您的課程中創建一個對象。你可以命名這個對象Leese。

private Leese leese = new Leese(); 
leese.id = ??? 
leese.name = ??? 

更具可讀性。

da.updatecommand.parameters.addwithvalues("[email protected]",ds.tables["tblLesseeYW"].Rows[LesseeYW.Position][0]; 

你寫了「ID @」,但它應該是「@ID」。如果你建立你的類正如我在上面descripe您可以使用屬性:

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

我希望幫助你。

+0

非常感謝,我告訴過你,我還是新的,我會嘗試編程更客觀,我真的很感激。 – 2012-07-16 21:22:19

相關問題