2015-06-09 307 views
2

我在DataGridView中列有逗號5個值一樣 abc,xyz,asdf,qwer,mni拆分逗號分隔字符串5

如何分割成字符串,並顯示在文本框中

abc 
xyz 
asdf 
qwer 
mni 
+2

所以,在總共三個問題:我如何從一個DataGridView列數據(你是說細胞)?如何在TextBox中設置文本?如何替換字符串中的逗號?沒有代碼,只顯示很少的努力... Winforms? – spender

+0

@Brain:爲了在5個不同的文本框中顯示它,你可以使用'Split()'。看到我的答案。 –

回答

0

你不需要在這裏拆分,只需更換逗號在字符串中,string.Replace

str = str.Replace(",", " "); 

編輯

string []arr = str.Split('c'); 
txt1.Text = arr[0]; 
txt2.Text = arr[1]; 
txt3.Text = arr[2]; 
txt4.Text = arr[3]; 
txt5.Text = arr[4]; 
+1

不應該是'.Replace(「,」,「」);'? – npinti

+0

謝謝@npinti,已經更新 – Adil

+0

我有5個文本框來顯示這5個字符串 – Ghost

0

先用空格替換逗號:

str = str.Replace(',', ''); 

然後將其添加回文本框:

textbox.Text = str; 
0

OP說,他有5個文本框來顯示的話。所以你可以使用String.Split();

例子:

string str="abc,xyz,asdf,qwer,mni"; 

textbox1.Text = str.Split(',')[0]; 
textbox2.Text = str.Split(',')[1]; 
textbox3.Text = str.Split(',')[2]; 
textbox4.Text = str.Split(',')[3]; 
textbox5.Text = str.Split(',')[4]; 

OR

您可以使用數組:

string[] strarray = str.Split(','); 
textbox1.Text = strarray[0]; 
textbox2.Text = strarray[1]; 
textbox3.Text = strarray[2]; 
textbox4.Text = strarray[3]; 
textbox5.Text = strarray[4];