本文介绍了使用组合框显示方式时,它显示列标题而不是值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我所做的,


我想做的是当用户离开我想要的combodropdonbox时
将客户详情"加载到其他文本框中,我想使用值member
由于数据库重复. "CustomerID"将不允许重复
至于客户名称可能.


在此先感谢您的任何帮助或建议!


试试
{
{
My_Job_Card_Manager.Entity_Framework.EntityContext context =新的My_Job_Card_Manager.Entity_Framework.EntityContext();
列表< my_job_card_manager.entity_framework.customer> CustomerList = context.Customers.ToList();
{
var AllRows = context.Customers.ToList();
DataTable CustomersDT =新的DataTable("Customers");

#region将必需的列添加到DataTable
////////////////////////////////////////////////////////////////////////向DataTable添加必需的列
///////////////////////////
//////////////


CustomersDT.Columns.Add("CustomerID",typeof(int));
CustomersDT.Columns.Add("CustomerDateAdded",typeof(string));
CustomersDT.Columns.Add("CustomerName",typeof(string));
CustomersDT.Columns.Add("CustomerAccountNumber",typeof(string));
CustomersDT.Columns.Add("CustomerAddress1",typeof(string));
CustomersDT.Columns.Add("CustomerAddress2",typeof(string));
CustomersDT.Columns.Add("CustomerAddress3",typeof(string));
CustomersDT.Columns.Add("CustomerTel",typeof(string));
CustomersDT.Columns.Add("CustomerFax",typeof(string));
CustomersDT.Columns.Add("CustomerEmail",typeof(string));
客户DT.Rows.Add(-1,");


//////////////
///////////////////////////
////////////////////////////////////////////////////////////////////////向DataTable添加必需的列
#endregion将必需的列添加到DataTable


#region将必需的列添加到DataTable
///////////////////////////////////////////////////////////////////////创建循环以将所有用户加载到DataTable
///////////////////////////
//////////////


foreach(AllRows.ToList()中的My_Job_Card_Manager.Entity_Framework.Customer Customercontext)
{
CustomersDT.Rows.Add(Customercontext.CustomerID,///数据库客户ID
Customercontext.CustomerDateAdded,///CustomerDateAdded
Customercontext.CustomerName,///CustomerName
Customercontext.CustomerAccountNumber,///CustomerAccountNumber
Customercontext.CustomerAddress1,///CustomerAddress1
Customercontext.CustomerAddress2,///CustomerAddress2
Customercontext.CustomerAddress3,///CustomerAddress3
Customercontext.CustomerTel,///CustomerTel
Customercontext.CustomerFax,///CustomerFax
Customercontext.CustomerEmail); ///CustomerEmail
}


//////////////
///////////////////////////
///////////////////////////////////////////////////////////////////////创建循环以将所有用户加载到DataTable
#endregion创建循环以将所有用户加载到DataTable

//如果(CustomersDT.Rows.Count< = 1)
//{
//MessageBox.Show(GlobalInfo.Messages.MessageBoxMessageNoUsersInDatabase,GlobalInfo.Messages.MessageBoxCaptionCriticalError,MessageBoxButtons.OK,MessageBoxIcon.Error);
//}

comboBox_Customer_Name.DataSource = CustomersDT;
comboBox_Customer_Name.ValueMember ="CustomerID";
comboBox_Customer_Name.DisplayMember ="CustomerName";

}
}





This is what i have done,


What i wish to do is when the user leaves the combodropdonbox i want to
load "Customer Detials" into other text boxes and i want to use the value member
because of duplications in database. the "CustomerID" Will not be allowed Duplicates
as for the customer name might.


Thanks In advance for any help or suggestions!


try
{
{
My_Job_Card_Manager.Entity_Framework.EntityContext context = new My_Job_Card_Manager.Entity_Framework.EntityContext();
List<my_job_card_manager.entity_framework.customer> CustomerList = context.Customers.ToList();
{
var AllRows = context.Customers.ToList();
DataTable CustomersDT = new DataTable("Customers");

#region Add Required Columns To DataTable
////////////////////////////////////////////////////////////////////// Add Required Columns To DataTable
//////////////////////////////////////////
////////////////


CustomersDT.Columns.Add("CustomerID", typeof(int));
CustomersDT.Columns.Add("CustomerDateAdded", typeof(string));
CustomersDT.Columns.Add("CustomerName", typeof(string));
CustomersDT.Columns.Add("CustomerAccountNumber", typeof(string));
CustomersDT.Columns.Add("CustomerAddress1", typeof(string));
CustomersDT.Columns.Add("CustomerAddress2", typeof(string));
CustomersDT.Columns.Add("CustomerAddress3", typeof(string));
CustomersDT.Columns.Add("CustomerTel", typeof(string));
CustomersDT.Columns.Add("CustomerFax", typeof(string));
CustomersDT.Columns.Add("CustomerEmail", typeof(string));
CustomersDT.Rows.Add(-1, " ");


////////////////
//////////////////////////////////////////
////////////////////////////////////////////////////////////////////// Add Required Columns To DataTable
#endregion Add Required Columns To DataTable


#region Add Required Columns To DataTable
////////////////////////////////////////////////////////////////////// Create Loop To Load All Users In To DataTable
//////////////////////////////////////////
////////////////


foreach (My_Job_Card_Manager.Entity_Framework.Customer Customercontext in AllRows.ToList())
{
CustomersDT.Rows.Add(Customercontext.CustomerID, /// Database Customer ID
Customercontext.CustomerDateAdded, /// CustomerDateAdded
Customercontext.CustomerName, /// CustomerName
Customercontext.CustomerAccountNumber, /// CustomerAccountNumber
Customercontext.CustomerAddress1, /// CustomerAddress1
Customercontext.CustomerAddress2, /// CustomerAddress2
Customercontext.CustomerAddress3, /// CustomerAddress3
Customercontext.CustomerTel, /// CustomerTel
Customercontext.CustomerFax, /// CustomerFax
Customercontext.CustomerEmail); /// CustomerEmail
}


////////////////
//////////////////////////////////////////
////////////////////////////////////////////////////////////////////// Create Loop To Load All Users In To DataTable
#endregion Create Loop To Load All Users In To DataTable

// if (CustomersDT.Rows.Count <= 1)
// {
// MessageBox.Show(GlobalInfo.Messages.MessageBoxMessageNoUsersInDatabase, GlobalInfo.Messages.MessageBoxCaptionCriticalError, MessageBoxButtons.OK, MessageBoxIcon.Error);
// }

comboBox_Customer_Name.DataSource = CustomersDT;
comboBox_Customer_Name.ValueMember = "CustomerID";
comboBox_Customer_Name.DisplayMember = "CustomerName";

}
}





private void comboBox_Customer_Name_Leave(object sender, EventArgs e)
{

    Entity_Framework.EntityContext CustomerDetailLoad = new Entity_Framework.EntityContext();

    try
    {
        int ReqCustomerID = Int32.Parse(comboBox_Customer_Name.ValueMember.ToString());
        var SelectedCust = (from u in CustomerDetailLoad.Customers select u).FirstOrDefault(r => r.CustomerID == ReqCustomerID);

        if (SelectedCust != null)
        {

            Text_Customer_Address_1.Text = SelectedCust.CustomerAddress1;
            Text_Customer_Address_2.Text = SelectedCust.CustomerAddress2;
            Text_Customer_Address_3.Text = SelectedCust.CustomerAddress3;
            Text_Customer_E_mail.Text = SelectedCust.CustomerEmail;
            Text_Customer_Tel.Text = SelectedCust.CustomerTel;
            Text_Customer_Fax.Text = SelectedCust.CustomerFax;
            Text_Customer_Account_Number.Text = SelectedCust.CustomerAccountNumber;

        }




   }

    catch(EntityException er)
        {


        }

推荐答案

int ReqCustomerID = Int32.Parse(comboBox_Customer_Name.ValueMember.ToString());


这是不正确的.您想要做的就是获取所选商品的CustomerID(我想,您的问题不清楚).您实际上正在做的是检索ValueMember属性,该属性已设置为静态字符串"CustomerID" ,并尝试将其解析为数字(这将不可避免地失败,并出现NumberFormatException ).

如果已在组合上设置ValueMember,则应该是从SelectedValue中获得的.所以我认为您要做的就是


This is incorrect. What you''re trying to do is obtain the CustomerID (I assume, your question isn''t that clear) for the selected item. What you''re actually doing is retrieving the ValueMember property, which you''ve set to the static string "CustomerID", and trying to parse that as a number (which will inevitably fail with a NumberFormatException).

If you''ve set the ValueMember on the combo, that should be what you get from SelectedValue. So I think all you need to do is

int ReqCustomerID = (int)comboBox_Customer_Name.SelectedValue;


这篇关于使用组合框显示方式时,它显示列标题而不是值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 01:57