如何创建未绑定列与网格内的外键相关

时间:2012-08-15 15:12:44

标签: c# winforms devexpress xtragrid

我想在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;

        }
    }

}

}

2 个答案:

答案 0 :(得分:1)

在Select语句中,您是否可以将customerYW表与customer_ID字段上的Customer表一起加入?然后,所有客户字段都可用,您可以在DataGrid中包含Customer_Name列。

答案 1 :(得分:0)

如果我的理解是正确的,您希望显示等效的customer_name来代替customner_id。您可以使用 RepositoryItemLookUpEdit 来实现此行为。

首先将repositoryItemLookUpEdit作为列编辑器添加到映射为“Customer_ID”的列

从客户表中获取客户信息并将其分配给repositoryItemLookUpEdit数据源。

"Select Customer_ID,Customer_Name from Customers" - 就像这样,您可以获取客户详细信息。

为repository(Customer_ID)和名称(Customer_Name)添加两列到repositoryItemLookUpEdit并适当地为它们指定字段名称。将“customer_ID”字段指定为值成员,将“Customer_name”字段指定为显示成员到repositoryItemLookUpEdit。这将满足您的要求。

要获得更好的演示文稿,您可以在repositoryItemLookUpEdit中使id列可见性为false。