int rowPosition = 0;
string WorkerName = "";
DataTable dtAllotedManpower = new DataTable();
dtAllotedManpower.Columns.Add("WorkerName");
foreach (GridViewRow row in GridViewTotalManpower.Rows)
{
    if (row.RowType == DataControlRowType.DataRow)
    {
        DataRow drAllotedManpower = dtAllotedManpower.NewRow();
        CheckBox chkChild = (CheckBox)GridViewTotalManpower.Rows[rowPosition].FindControl("chkChild");
        if (chkChild.Checked == true)
        {
            WorkerName = Convert.ToString(GridViewTotalManpower.DataKeys[rowPosition]["WorkerName"].ToString()) + "," + WorkerName;

        }
        rowPosition++;
    }
    hidfWorker.Value = WorkerName;


我编写了以下代码。我的隐藏字段值像这样来

“哈里斯,里玛,”

但是我想要值“ HARSH,RIMA”(在最后一个单词后没有',')。如何构建该代码? 。最后一个词后没有“逗号”。

最佳答案

将它们添加到集合中,然后使用string.Join

var list = new List<string>();

foreach (GridViewRow row in GridViewTotalManpower.Rows) {
    // ...other code here...
    list.Add(Convert.ToString(GridViewTotalManpower.DataKeys[rowPosition]["WorkerName"].ToString()));
}

 hidfWorker.Value = string.Join(", ", list);

关于c# - 如何在不使用最终逗号的情况下逗号分隔一组字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24052987/

10-10 21:25