[英]Select multiple rows of Gridview in one textbox C#
選擇該行后,我需要GridView的多行顯示在單個TextBox中。 我可以選擇一行並將其顯示在TextBox中,這是它的代碼:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
txtConfirm.Text = "Course: " + GridView1.SelectedRow.Cells[0].Text;
}
}
有人可以幫助我如何繼續選擇文本框的行嗎?
我只是在做這件事...所以可能需要花點時間。 記下需要尋址的特殊字符。
public string GridtoCVS(GridView gvParm)
{
StringBuilder sbRetVal = new StringBuilder();
for (int i = 0; i <= gvParm.Rows.Count - 1; i++)
{
StringBuilder sbLine = new StringBuilder();
for (int j = 0; j <= gvParm.Columns.Count - 1; j++)
{
sbLine.Append(gvParm.Rows[i].Cells[j].Text.Trim() + ",");
}
string sLine = sbLine.ToString().Replace(" ", "").Replace("'","'").Replace("&","&");
sbRetVal.AppendLine(sLine);
}
return sbRetVal.ToString();
}
選擇所有行:您可以選擇從foreach
行中獲取所有行數據,而不是SelectedRow
,如下所示:
String AllRows = String.Empty; // add all rows in this String variable
// Iterate to all rows of your gridview
foreach (GridViewRow row in GridView1.Rows)
AllRows += "Course: " + row.Cells[0].Text + "\n"; Add Current row the String
txtConfirm.Text = AllRows; // set String variable to the TextBox
持久化TextBox數據:您可以像這樣持久化TextBox中的前幾行數據:
String AllRows = txtConfirm.Text; // add TextBox data in this String variable
// Iterate to all rows of your gridview
foreach (GridViewRow row in GridView1.Rows)
AllRows += "Course: " + GridView1.SelectedRow.Cells[0].Text; + "\n";
txtConfirm.Text = AllRows; // set String variable to the TextBox
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.