2012-01-12 23 views
0

This is my database table. Now I want to combine Branch_Id, Acc_no and Acc_Type like 000178963503 and 000211111102 and display it in a dropdownlist如何将数据库表字段值和负载组合到一个下拉列表中?

这是我的数据库表。现在我想Branch_Id,ACC_NO和Acc_Type,使它看起来像000178963503或000211111102相结合,并在下拉列表显示它 我的代码是

string[] account_number; 
string account; 
for (int j = 0; j < dtAcc.Rows.Count;j++) 
        { 
         account = Convert.ToString(dtAcc.Rows[j][2])+Convert.ToString(dtAcc.Rows[j][5]) + Convert.ToString(dtAcc.Rows[j][3]); ///Account Number 
         account_number =account.Split(' ');            
        } 
Drp_AccType.DataSource = account_number; 
        Drp_AccType.DataBind(); 

感谢

回答

2

我不知道你有什么用account_number =account.Split(' ');实现

//here is the list which will be source of combo box 
List<String> accounts = new List<String>(); 
//go through each row 
for (int j = 0; j < dtAcc.Rows.Count;j++) 
{ 
    //combine branch, account no and type 
    String account = Convert.ToString(dtAcc.Rows[j]["Branch_Id"]) + Convert.ToString(dtAcc.Rows[j]["Acc_no"]) + Convert.ToString(dtAcc.Rows[j]["Acc_Type"]); ///Account Number 
    //add account/row record to list 
    accounts.Add(account);            
} 
//bind combo box 
Drp_AccType.DataSource = accounts; 
Drp_AccType.DataBind(); 
+0

...感谢它的工作 – sun 2012-01-12 07:47:17

0

查询从数据库中选择

例如,当你可以Concat的字符串:选择Branch_ ID + ACC_NO + Acc_Type从表名作为COL1

代码

所有的
DataSet ds=GetDataSet("select Branch_Id + Acc_no + Acc_Type from tablename as col1"); 
Drp_AccType.DataSource = ds; 
Drp_AccType.DataTextField="col1" 
Drp_AccType.DataValueField="col1" 
        Drp_AccType.DataBind(); 
0

首先,你不能直接分配给“ACCOUNT_NUMBER”,因为它的一个数组,你将不得不使用索引即。

account_number[j] = account; 

你也需要初始化数组的项目将举行,因为量如果您尝试访问不是数组,你会得到一个“访问冲突错误”

我会在一个项目而是使用这种方法,那么你不需要使用数组;

string account; 
Drp_AccType.Items.Clear(); //This removes previous items because you are not using  
//DataBind method 
for (int j = 0; j < dtAcc.Rows.Count;j++) 
       { 
        account = Convert.ToString(dtAcc.Rows[j][2])+Convert.ToString(dtAcc.Rows[j][5]) + Convert.ToString(dtAcc.Rows[j][3]); ///Account Number 
        Drp_AccType.Items.Add(account); //Add directly to 
//dropdown box         
       } 
相关问题