在一个文本框C#中选择Gridview的多行

时间:2018-07-13 10:51:59

标签: c# asp.net gridview

选择一行后,我需要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;
    }
}

这是select选项当前功能的一个示例: enter image description here

有人可以帮助我如何继续选择文本框的行吗?

2 个答案:

答案 0 :(得分:0)

我只是在做这项工作...所以可能需要花点时间。记下需要寻址的特殊字符。

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("&nbsp;", "").Replace("&#39;","'").Replace("&amp;","&");

        sbRetVal.AppendLine(sLine);
    }
    return sbRetVal.ToString();
}

答案 1 :(得分:0)

选择所有行:您可以选择从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