Select 一个文本框中的多行Gridview C#

Select multiple rows of Gridview in one textbox C#

我需要 GridView 的多行在 select 编辑行后显示在单个 TextBox 中。我能够 select 一行并将其显示在 TextBox 中,这是它的代码:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        txtConfirm.Text = "Course: " + GridView1.SelectedRow.Cells[0].Text;
    }
}

这是 select 选项当前作用的示例:

任何人都可以帮助我如何将 selecting 行保存到 TextBox 中吗?

我正在研究这个...因此可能需要一些调整。记下需要寻址的特殊字符。

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();
}

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