使用c#在ASP.NET中DropDownList中的MultiColumns [英] MultiColumns in a DropDownList in ASP.NET using c#

查看:62
本文介绍了使用c#在ASP.NET中DropDownList中的MultiColumns的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在DropDownList菜单中显示2列。为了实现这一点,当我在SSMS中执行以下SQL查询时,







  SELECT  convert  varchar ,Cost_ID)+ ' ,' + Item_Description) FROM  Cost_ID; 





它带来了所需的输出。但是当我在Microsoft Visual Studio 2012数据源中添加它时,即



  <   asp:SqlDataSource     ID   =  SqlDataSource12    runat   =  server    ConnectionString   = 数据源= MEHDI-PC\SQLEXPRESS;初始目录= PIMS;集成安全性=真    ProviderName   =  System.Data.SqlClient    SelectCommand   =  SELECT(转换(varchar,CostID)+','+ Item_Description)FROM Cost_ID >  <   / asp :SqlDataSource  >  





it运行程序时出现以下错误:



 DataBinding:System.Data.DataRowView不包含名为Cost_ID的属性。





我知道哪里出错了?



谢谢。

解决方案

不确定我是否理解你,你正试图在数据库的下拉列表中显示多个列。是吗?

如果然后看看如何在下面完成:



 SqlCommand cmd; 
SqlConnection con;
SqlDataAdapter da;
DataSet ds;

private void button1_Click( object sender,EventArgs e)
{
con = new SqlConnection( your_database_connection_sting);
cmd = new SqlCommand( select *来自emp,con);
da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);

for int i = 0 ; i < ds.Tables [ 0 ]。Rows.Count; i ++ )
{
comboBox1.Items.Add(ds.Tables [ 0 ]。行[i] [ 0 ] + + ds.Tables [ 0 ]。行[i] [ 1 ] + + ds.Tables [ 0 ]。行[i] [ 2 ]);
}
}





输出:

  1021  shuvro  4875  
1022 ridoy 3675 ​​
1023 trena 5575


I am trying to show 2 columns in a DropDownList Menu. To accomplish that,

when I execute the following SQL query in SSMS,

SELECT (convert(varchar,Cost_ID) +' , '+ Item_Description) FROM Cost_ID;



It brings the desired output. But when I add it in Microsoft Visual Studio 2012 Data-Source, i.e.,

<asp:SqlDataSource ID="SqlDataSource12" runat="server" ConnectionString="Data Source=MEHDI-PC\SQLEXPRESS;Initial Catalog=PIMS;Integrated Security=True" ProviderName="System.Data.SqlClient" SelectCommand="SELECT (convert(varchar,CostID) +'   ,   '+ Item_Description) FROM Cost_ID"></asp:SqlDataSource>



it gives the following error when I run the program:

DataBinding: System.Data.DataRowView does not contain a property with the name Cost_ID.



Any idea where I am going wrong?

Thank You.

解决方案

Not sure if i understand you right,you are trying to show multiple columns in dropdownlist that comes from a database.Is it?
If that then see how it can be done below:

SqlCommand cmd;
SqlConnection con;
SqlDataAdapter da;
DataSet ds;

private void button1_Click(object sender, EventArgs e)
{
    con = new SqlConnection("your_database_connection_sting");
    cmd =new SqlCommand ("select * from emp", con);
    da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);

    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        comboBox1.Items.Add(ds.Tables[0].Rows[i][0] + " " + ds.Tables[0].Rows[i][1] + " " + ds.Tables[0].Rows[i][2]);
    }
}



Output:

1021 shuvro 4875
1022 ridoy 3675
1023 trena 5575


这篇关于使用c#在ASP.NET中DropDownList中的MultiColumns的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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