Devexpress SearchLookUpEdit从数据库中设置值

时间:2012-08-30 09:05:49

标签: c# devexpress

我在我的Form加载上有这个代码,用于填充我的数据表上的数据,然后在我的SearchLookupEdit上填充:

FillGrids(oData, "POLIST",
    "SELECT [PORecord].[POID],[ItemCode],[PONumber],"
  + "[SiteNo]+' '+[Place]+' '+[Name]+' '+[ScopeOfWork]"
  + "+' '+CAST([DeliveryDate] AS VARCHAR(30))"
  + "+' '+CAST([PRNumber]AS VARCHAR(10))+' '+[Requisitioner] AS Name,"
  + "[UnitPrice],[Quantity],[Unit],Completed,Status"
  + " FROM [Globaltek-Final].[Project].[PORecord]"
  + " INNER JOIN Project.POStatus ON Project.PORecord.POID = Project.POStatus.POID",
  CommandType.Text);

cmbPOID.Properties.DataSource = oData.Tables["POLIST"];
cmbPOID.Properties.DisplayMember = "PONumber";
cmbPOID.Properties.ValueMember = "POID";

然后通过这个事件:

private void cmbPOID_EditValueChanged(object sender, EventArgs e)
{
    //cmbPOID.ShowPopup();

    System.Data.DataRow row = gvPOID.GetDataRow(gvPOID.FocusedRowHandle);
    try
    {
        oPOID = row[0].ToString();
        txtAmount.Text = row["UnitPrice"].ToString();
    }
    catch (NullReferenceException ex)
    {
        MessageBox.Show(ex.Message);
    }
}

我可以更改控件的值,然后将其添加到我的数据库中。

编辑:要加载我存储在数据库中的数据,我使用以下代码:

 using (SqlCommand xComm = new SqlCommand())
 {
     xComm.Connection = xConn;
     xComm.CommandText =
        "SELECT [ProjectID] ,[Year] ,[NTPID] ,Project.MainProjectRecord.[POID],"
      + "[SiteID]  ,[SiteName]  ,[SiteDetailsID] ,[EquipmentID]  ,[RepGlobaltekID],"
      + "[CompletedID] ,[OnGoingID]  ,Project.PORecord.POID"
      + " FROM [Globaltek-Final].[Project].[MainProjectRecord]"
      + " INNER JOIN Project.PORecord ON Project.MainProjectRecord.POID = Project.PORecord.POID"
      + " WHERE ProjectID = @recordID";
     xComm.CommandType = CommandType.Text;
     xComm.Parameters.AddWithValue("@recordID", RecordID);

     SqlDataReader xReader = null;
     try
     {
         xConn.Open();
         xReader = xComm.ExecuteReader();
         while (xReader.Read())
         {
             txtSiteID.Text = xReader["SiteID"].ToString();
             txtSiteName.Text = xReader["SiteName"].ToString();
             cmbPOID.EditValue = xReader["POID"].ToString();
             cmbPOID.Properties.GetDisplayTextByKeyValue(xReader["POID"].ToString());
             //cmbPOID.EditValue = xReader["POID"].ToString();
         }
         xReader.Close();
     }
     .....

我的问题是如何设置或加载我从数据库返回到SearchLookupEdit控件的值?

2 个答案:

答案 0 :(得分:0)

获取记录的POID值后,只需在表单EditValue之前设置ShowDialog属性,以备编辑项目时使用:

using(var dlg = new ProjectForm())
{
    dlg.searchLookUp.EditValue = poid;

    dlg.ShowDialog(); //The rest of your code
}

在表单的加载中,您应该设置searchLookup的DataSource属性。 ValueMember和DisplayValue属性可以通过设计器设置,即使我将它们设置在代码中

searchLookup.DataSource = myFunctionToGetData();
searchLookup.Properties.DisplayMember = "PONumber";
searchLookup.Properties.ValueMember= "POID";

答案 1 :(得分:0)

要在使用数据表将其绑定到数据集中时在searchlookupedit上显示值或设置值,它应该与searchlookupedit.properties.value成员匹配您传递到editvalue的值,如下所示..

int value= 12;
       searchLookUpEdit1.Properties.ValueMember = "POID";
       searchLookUpEdit1.Properties.DisplayMember = "PONumber";
       searchLookUpEdit1.EditValue = ValueDisplay(); //it should match the value member

 private object ValueDisplay()
    {
        return value;
    }

对于我的问题,我首先填充数据表并将其填充到searchloookupedit中。设置searchlookupedit.editvalue时它是空白的,因为在我的数据表列上它是名称“PoID”,而在我的值成员上它是“POID”。这应该修复,并且必须使用相同的名称,以便editvalue生效。我改变了“POID” - > “POID”。 :d