2012-08-15 59 views
0

我想在Xtragrid控件devexpress中動態創建一個未綁定列;有這樣的情況:我有一個網格綁定與來自Payment Datatable的數據,字段是「Payment_ID,Customer_ID」等等,我想要做的是,而不是有這個表中的外國Customer_ID,我希望在其中包含與Customer_ID相關的Customer_Name,以避免混淆。有人幫忙謝謝(使用winforms C#)。下面的代碼使用;填充網格如何創建一個與網格內的外鍵相關的未綁定列

  using System; 
      using System.Collections.Generic; 
      using System.ComponentModel; 
      using System.Drawing; 
      using System.Data; 
      using System.Linq; 
      using System.Text; 
      using System.Windows.Forms; 
      using System.Data.SqlClient; 

      namespace YoungWoman 
      { 
       public partial class Payment2 : UserControl 
        { 
          DataSet ds3 = new DataSet(); 
          SqlDataAdapter dapayment = new SqlDataAdapter(); 
          SqlConnection conne = SqlCoonectionSEtup.GetConnection; 
          BindingSource PaymentBinding = new BindingSource(); 

       public Payment2() 
       { 
        InitializeComponent(); 
       } 

       private void AddPayment_Click(object sender, EventArgs e) 
      { 
       Paymentfrm payent = new Paymentfrm(Utils.Formtype.add, 0); 
       payent.PaymentEvent += new EventHandler(RefreshingGrid); 
       payent.PayentlabelsEvent += new EventHandler(RefresHlabels); 

       payent.ShowDialog(); 

      } 

       private void EditPayment_Click(object sender, EventArgs e) 
       { 
       Paymentfrm pyt = new Paymentfrm(Utils.Formtype.edit, 1); 
       pyt.ShowDialog(); 
       } 
        void RefreshingGrid(object sender, EventArgs e) 
      { 
        ds3.Tables["tblPaymentYW"].Clear(); 
        SqlCommand cmd = new SqlCommand("SELECT * FROM PaymentYW", conne); 

         dapayment.SelectCommand = cmd; 
         dapayment.Fill(ds3, "tblPaymentYW"); 
         DgPaymentYW.DataSource = ds3.Tables["tblPaymentYW"]; 
         DgPaymentYW.RefreshDataSource(); 
      } 
    void RefresHlabels(object sender, EventArgs e) 
    { 
     LoadData(); 
    } 


    private void Payment2_Load(object sender, EventArgs e) 
    { 
     LoadData(); 



    } 

    private void LoadData() 
    { 
     SqlCommand cmd = new SqlCommand("SELECT *FROM PaymentYW ORDER BY PaymentId ", conne); 
     dapayment.SelectCommand = cmd; 
     ds3.Clear(); 
     dapayment.Fill(ds3, "tblPaymentYW"); 
     PaymentBinding.DataSource = ds3.Tables["tblPaymentYW"]; 
     DgPaymentYW.DataSource = PaymentBinding; 

     this.PaymentIdlabelContro.DataBindings.Clear(); 
     this.PaymentIdlabelContro.DataBindings.Add(new Binding("Text", PaymentBinding, "PaymentId")); 
     ReservationIdlabelControl.DataBindings.Clear(); 
     this.ReservationIdlabelControl.DataBindings.Add(new Binding("Text", PaymentBinding, "ReservationId")); 
     LesseeIdlabelControl.DataBindings.Clear(); 
     this.LesseeIdlabelControl.DataBindings.Add(new Binding("Text", PaymentBinding, "LesseeId")); 

     DatelabelControl.DataBindings.Clear(); 

     this.DatelabelControl.DataBindings.Add(new Binding("Text", PaymentBinding, "PaymentDate")); 
     AmountlabelControl.DataBindings.Clear(); 
     this.AmountlabelControl.DataBindings.Add(new Binding("Text", PaymentBinding, "AmountPaid")); 
     BalancelabelControl.DataBindings.Clear(); 
     this.BalancelabelControl.DataBindings.Add(new Binding("Text", PaymentBinding, "Balance")); 
     gridView1.Columns[1].Visible = false; 
     gridView1.Columns[6].Visible = false; 


     DevExpress.Utils.FormatInfo fi = new DevExpress.Utils.FormatInfo(); 
     fi.FormatType = DevExpress.Utils.FormatType.Numeric; 
     fi.FormatString = "c2"; 
     gridView1.Columns[5].DisplayFormat.Assign(fi); 
     gridView1.Columns[4].DisplayFormat.Assign(fi); 
     Rowcount(); 
     Priveleges(); 


    } 

    void Rowcount() 
    { 
     RecordlabelControl.Text = " Records : " + (gridView1.RowCount); 

    } 
    void Priveleges() 
    { 
     if (SqlCoonectionSEtup.Priveleges == "User") 
     { 
      AddPayment.Enabled = false; 
      EditPayment.Enabled = false; 
      CancelBtn.Enabled = false; 
     } 
     else 
     { 

      AddPayment.Enabled = true; 
      EditPayment.Enabled = true; 
      CancelBtn.Enabled = true; 

     } 
    } 

} 

}

+0

「動態創建未綁定列」是什麼意思? – Paparazzi 2012-08-15 15:28:47

+0

作者:「動態創建一個未綁定的列」我的意思是通過代碼創建一個來自另一個表的列,這個表的主鍵是一個特定表中的外鍵。 – 2012-08-15 16:54:20

+0

Jerry的方法應該可行。我仍然稱它爲一個綁定列。 – Paparazzi 2012-08-15 17:44:30

回答

1

在SELECT語句,你可以加入PaymentYW表的CUSTOMER_ID領域的客戶表?然後,所有客戶字段都將可用,並且您可以在DataGrid中包含Customer_Name列。

+0

好的會嘗試使用這種方法 – 2012-08-15 16:56:47

0

如果我的理解正確,您希望顯示相應的customer_name而不是customner_id。您可以通過使用RepositoryItemLookUpEdit來實現此行爲。

加上一個repositoryItemLookUpEdit作爲列編輯器與「CUSTOMER_ID」從客戶表

獲取客戶信息映射到的列,並將其分配給repositoryItemLookUpEdit數據源。

"Select Customer_ID,Customer_Name from Customers" - 喜歡這個,你可以得到你的客戶的詳細信息。

爲id(Customer_ID)和名稱(Customer_Name)添加兩列到repositoryItemLookUpEdit,併爲它們適當地指定字段名稱。將「Customer_ID」字段指定爲值成員,並將「Customer_name」字段指定爲顯示成員至repositoryItemLookUpEdit。這將滿足您的要求。

對於更好的演示文稿,您可以使repositoryItemLookUpEdit中的id列可見性爲false。