我是c#数据库编程的新手,我正在设计一个具有组合框的简单表单,这些表单是绑定的,当我通过记录导航时,组合框的显示成员有时不显示,而是显示值成员任何想法为什么它发生了。 这是我写的代码:
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);
}
}
}
这是程序:
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
表格的结构:
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] –