1

我在表单加载中有此代码,用于在我的数据表中填充我的数据,然后填充到我的 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 控件的值?

4

2 回答 2

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";
于 2012-08-30T13:48:42.187 回答
0

要在使用数据表将 searchlookupedit 绑定到数据集时在 searchlookupedit 上显示值或设置值,它应该将 searchlookupedit.properties.valuemember 与您传递给 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;
    }

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

于 2012-08-31T04:01:35.737 回答