C#在组合框中查看显示成员 [英] C# viewing display member in combobox
本文介绍了C#在组合框中查看显示成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是c#数据库编程的新手,我正在设计一个具有组合框的简单表单,这些表单是绑定的,当我通过记录导航时,组合框的显示成员有时不会显示,而是显示值成员任何想法为什么它正在发生。
我的尝试:
这是我写的代码:
private void Frm_R_Load(object sender,EventArgs e)
{
#region表格加载
this.land_NewTableAdapter.Fill (reentitiesDataset.Land_New);
FillCombos(T_Region,id_RegionComboBox);
FillCombos(T_district,id_DistrictComboBox);
#endregion
}
private void FillCombos(String TableName,ComboBox ComboName)
{
using(SqlConnection conn = new SqlConnection(string.Format( ConnectionString.DatabaseConnection.GetSQLConnString())))
{
try
{
conn.Open();
SqlCommand cmdCombo = new SqlCommand(FillCombos,conn);
cmdCombo.CommandType = CommandType.StoredProcedure;
cmdCombo.Parameters.Add(@ TableName,SqlDbType.VarChar).Value = TableName;
SqlDataAdapter DaCombo = new SqlDataAdapter(cmdCombo);
DataSet DsCombo = new DataSet();
DaCombo.Fill(DsCombo);
ComboName.DataSource = DsCombo.Tables [0];
ComboName.DisplayMember =Descrp;
ComboName.ValueMember =Id;
}
catch(exception ex)
{
MessageBox.Show(ex.Message,,MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
}
这是程序:
PROCEDURE [dbo]。[FillCombos](@ TableName as varchar (100))
AS
BEGIN
声明@SQL为varchar(1000)
SET NOCOUNT ON;
设置@SQL ='从'+ @TableName +'中选择Id,Descrp 2'
exec sp_sqlexec @sql
END
表的结构:
CREATE TABLE [dbo]。[T_District]([Id] [tinyint] IDENTITY(1,1)NOT NULL,[varrp](50)NOT NULL,CONSTRAINT [PK_T_District] PRIMARY KEY CLUSTERED([创建] ASC)WITH(ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON)ON [PRIMARY])ON [PRIMARY] CREATE TABLE [dbo]。[T_Region]([Id] [bigint] IDENTITY(1,1)NOT NULL,[Desccript] ] [varchar](255)NOT NULL,[D2] [varchar](255)NULL,CONSTRAINT [PK_T_Region] PRIMARY KEY CLUSTERED([Id] ASC)WITH(ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON)ON [PRIMARY])ON [主要]
解决方案
我不确定你的代码是如何工作的但我会使用绑定和WPF(如果那是你的选项) - 有关于此的文章:
一步一步与组合框的WPF数据绑定 [ ^ ]
......无论如何你都会遇到像UWP这样的东西......
I am new to c# database programming, and am designing a simple form that has comboboxes, which are binded and when I navigate thru records the display member of the combobox do not show sometimes and displays instead the value member any idea why it is happening.
What I have tried:
This is the code am writing: private void Frm_R_Load(object sender, EventArgs e) { #region Form load this.land_NewTableAdapter.Fill(reentitiesDataset.Land_New); FillCombos("T_Region", id_RegionComboBox); FillCombos("T_district", id_DistrictComboBox); #endregion } private void FillCombos(String TableName, ComboBox ComboName) { using (SqlConnection conn = new SqlConnection(string.Format(ConnectionString.DatabaseConnection.GetSQLConnString()))) { try { conn.Open(); SqlCommand cmdCombo = new SqlCommand("FillCombos", conn); cmdCombo.CommandType = CommandType.StoredProcedure; cmdCombo.Parameters.Add("@TableName", SqlDbType.VarChar).Value = TableName; SqlDataAdapter DaCombo = new SqlDataAdapter(cmdCombo); DataSet DsCombo = new DataSet(); DaCombo.Fill(DsCombo); ComboName.DataSource = DsCombo.Tables[0]; ComboName.DisplayMember = "Descrp"; ComboName.ValueMember = "Id"; } catch (Exception ex) { MessageBox.Show(ex.Message, " ", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } And this is the procedure: PROCEDURE [dbo].[FillCombos](@TableName as varchar(100)) AS BEGIN Declare @SQL as varchar(1000) SET NOCOUNT ON; Set @SQL = 'Select Id,Descrp from ' + @TableName + ' order by 2 ' exec sp_sqlexec @sql END Structure of the tables: CREATE TABLE [dbo].[T_District]( [Id] [tinyint] IDENTITY(1,1) NOT NULL, [Descrp] [varchar](50) NOT NULL, CONSTRAINT [PK_T_District] PRIMARY KEY CLUSTERED ( [Descrp] ASC )WITH (ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] CREATE TABLE [dbo].[T_Region]( [Id] [bigint] IDENTITY(1,1) NOT NULL, [Descrp] [varchar](255) NOT NULL, [D2] [varchar](255) NULL, CONSTRAINT [PK_T_Region] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
解决方案
I am not sure how your code works but I would use binding and WPF (if thats an option for you) - there are tones of articles around this:
Step by Step WPF Data Binding with Comboboxes[^]
... and you'll be running into it with things like UWP anyway...
这篇关于C#在组合框中查看显示成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文