2012-01-12 25 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         
       } 
相關問題