如何从表中包含3个字段,用cql server2008在c#windows窗体中显示组合框的成员属性 [英] how to include 3 fields from table to display member property of combobox in c# windows forms with sql server2008

查看:68
本文介绍了如何从表中包含3个字段,用cql server2008在c#windows窗体中显示组合框的成员属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我的名字在过去的3天里我已经打破了我的头脑如何从表中包含3个字段来显示c#server形式的c#server窗体中的组合框件?

我有一个名为: frmDialyzer 的表单,其中包含一个名为: cboPatientID 的组合框。

下面给出了关于如何从名为 patient_id patient_name 的表填充我的组合框( cboPatientID )的c#代码在sql server2008中:

Hi my name is vishal for past 3 days i have been breaking my head on how to include 3 fields from table to display member property of combobox in c# windows forms with sql server2008?
I have a form named:frmDialyzer and in it is a combobox named:cboPatientID.
Given below is c# code on how i populate my combobox(cboPatientID) from tables named:patient_id and patient_name in sql server2008.:

namespace DRRS_CSharp
{
    public partial class frmDialyzer : Form
    {
int pUserID;
public frmDialyzer()
        {
            InitializeComponent();
string Sql = "Select p.patient_id as patient_id,(n.patient_first_name+' '+n.patient_last_name) as Name from patient_id p,patient_name n where n.patient_id=p.patient_id and n.status=1 and not exists(Select * from dialyser where dialyser.deleted_status=0 and dialyser.closed_status=0 and dialyser.patient_id=p.patient_id)";
            DataTable dt = new DataTable();
            SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            SqlCommand cmd = new SqlCommand(Sql, conn);
            dt.Load(cmd.ExecuteReader());
            cboPatientID.DataSource = dt;
            cboPatientID.ValueMember = "patient_id";
            cboPatientID.DisplayMember = "Name";
            cboPatientID.SelectedValue = 0;



因为我正在使用 SelectedValue frmDialyzer 中选择组合框( cboPatientID )中的项目时, cboPatientID 的属性。我还在 frmDialyzer 中启用了 cboPatientID 的DropDownStyle属性到 DropDownList

下面给出了sql server2008中表的结构:

表名: patient_id

ColumnName DataType AllowNulls

patient_id(主键)Int No(自动增量)

patient_sex nvarchar (15)是

patient_dob日期是

row_upd_date datetime是



表名: patient_name

ColumnName DataType AllowNulls

patient_id int是

patient_first_name nvarchar(50)是

patient_middle_name nvarchar(50)是

patient_las t_name nvarchar(50)是

状态位是

row_upd_date datetime是



如下所示c#代码 frmDialyzer


Since i am using SelectedValue property of cboPatientID in terms of selecting item in combobox(cboPatientID) in frmDialyzer. I have also enabled DropDownStyle property of cboPatientID to DropDownList in frmDialyzer.
Given below is structure of tables in sql server2008:
table name:patient_id
ColumnName DataType AllowNulls
patient_id(primary key) Int No(since auto-increment)
patient_sex nvarchar(15) Yes
patient_dob date Yes
row_upd_date datetime Yes

table name:patient_name
ColumnName DataType AllowNulls
patient_id int Yes
patient_first_name nvarchar(50) Yes
patient_middle_name nvarchar(50) Yes
patient_last_name nvarchar(50) Yes
status bit Yes
row_upd_date datetime Yes

As you can see in below c# code of frmDialyzer:

namespace DRRS_CSharp
{
    public partial class frmDialyzer : Form
    {
int pUserID;
 public frmDialyzer()
        {
            InitializeComponent();
string Sql = "Select p.patient_id as patient_id,(n.patient_first_name+' '+n.patient_last_name) as Name from patient_id p,patient_name n where n.patient_id=p.patient_id and n.status=1 and not exists(Select * from dialyser where dialyser.deleted_status=0 and dialyser.closed_status=0 and dialyser.patient_id=p.patient_id)";
            DataTable dt = new DataTable();
            SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            SqlCommand cmd = new SqlCommand(Sql, conn);
            dt.Load(cmd.ExecuteReader());
            cboPatientID.DataSource = dt;
            cboPatientID.ValueMember = "patient_id";
            cboPatientID.DisplayMember = "Name";
            cboPatientID.SelectedValue = 0;
}



在上面的代码中,我能够在 DisplayMember 患者的名字和姓氏 > cboPatientID 的属性。

假设我想显示 patient_id 以及患者名字和姓氏 cboPatientID DisplayMember 属性?可能吗?!如果需要,我是否需要进行任何有关 patient_id 的转换,然后将其与患者 名称一起显示在 DisplayMember中 cboPatientID 的属性。

任何人都可以帮助我了解如何 Patient_id 患者首先并且姓氏 cboPatientID DisplayMember 属性中的名称?!

任何人都可以请帮帮我?!任何帮助/指导解决这个问题将不胜感激!


In above code i am able to display patient first and last name as Name in DisplayMember property of cboPatientID.
Suppose i want to display patient_id along with patient first and last name as Name in DisplayMember property of cboPatientID? Is it possible?! If so should i need to do any conversions regarding patient_id and then display it along with patient first and last name as Name in DisplayMember property of cboPatientID.
Can anyone help me please on how to display patient_id with patient first and last name as Name in DisplayMember property of cboPatientID?!
Can anyone help me please?! Any help/guidance in solving of this problem would be greatly appreciated!

推荐答案

你必须在连接之前将patient_id的int类型转换为varchar,修改:

You have to convert the int type of patient_id to varchar before concatenation, modify:
(n.patient_first_name+' '+n.patient_last_name) 



to:


to:

(convert(varchar, n.patient_id) + ' ' + n.patient_first_name+' '+n.patient_last_name) 


这篇关于如何从表中包含3个字段,用cql server2008在c#windows窗体中显示组合框的成员属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆