我有30個按鈕,沒有默認的文本值。我想要做的是從數據庫中獲取有序的數據:將文本值分配給.net中數據庫的按鈕?
這裏是數據庫格式:
ID value cat
1 Button name a
2 Another name a
3 something a
. . a
. . a
. . a
30 Last button a
1 Button name b
2 Another name b
3 something b
. . b
. . b
. . b
18 Last button b
我現在已經創建了一個函數搶ID和類別使用OLEDB爲MS選擇特定的名稱ACCESS數據庫。
這裏是一個函數:
private string getItem(int i, string a) {
//database connection
// database query and table slection where ID = i and cat = a
read the data in the selected column
return the string form of the column
}
現在,每當一種形式的負荷,我已經指派以下功能:
button1.Show();
...
...
button30.Show();
button1.Text = getItem(1, a);
button2.Text = getItem(2, a);
..
...
...
button30.text = getItem(30, a);
再有一個單獨的按鈕來改變,這將改變類別
button1.Text = getItem(1, b);
button2.Text = getItem(2, b);
.
.
.
button18.Text = getItem(18, b);
// because I have only 18 items I hide all the remaining buttons.
button19.Hide();
...
button30.Hide();
它運作良好和輕鬆,但問題是,它:在按鈕上的文字值太慢了。 每當表單加載時,需要一段時間才能將所有數據填充到按鈕,而且當我單擊按鈕更改類別時,也會發生這種情況。
有什麼方法在陣列或類似的東西要做到這一點? 上面的代碼需要多次訪問數據庫導致處理速度緩慢。
的另一個問題是,如果我有僅18或數據庫中的表格更小的30餘項,預製造的按鈕將與在它的默認值是可見的。
在上面的代碼我克服它利用buttonx.Hide(),而x是不具有任何值的所有值..
這些都是手工工作和所需的質量作弄。
我會感激知道解決這個問題的一些好方法。
不是有30個人訪問數據庫,以填補30個按鈕,不能你剛纔有一個很大的訪問和填寫數據的按鈕。我認爲這應該會給你帶來很大的提振。 – 2011-02-09 07:14:13