网格视图中的重复行

时间:2013-04-12 03:50:18

标签: c# asp.net

在我有关于如何(从ASP.NET网格视图中动态添加和删除行)的问题之前,我通过使用文章(http://www.codeproject.com/Articles/467788/Dynamically-adding-and-deleting-rows-from-ASP-NET)来接收我的答案。 在本文中,我们按类型(手)填写字段时添加行。 现在我想从下拉列表中选择值后完成一些行的字段,所以我使用了

  public static List<string> Docprpp = new List<string>(3);

对于添加第一行我没有问题,当我从下拉列表中选择文档时,我可以在字段中找到(文档号,转换,标题)以及我手工填写的其他字段。

dtCurrentTable.Rows[i - 1]["Col1"] = Docprpp[0];
                dtCurrentTable.Rows[i - 1]["Col2"] = Docprpp[1];
                dtCurrentTable.Rows[i - 1]["Col3"] = Docprpp[2];
                dtCurrentTable.Rows[i - 1]["Col4"] = TextBoxsh.Text;
                dtCurrentTable.Rows[i - 1]["Col5"] = DrpStatus.SelectedValue;
                dtCurrentTable.Rows[i - 1]["Col6"] = DrpClass.SelectedValue;
                rowIndex++;
            }
            dtCurrentTable.Rows.Add(drCurrentRow);
            ViewState["CurrentTable"] = dtCurrentTable;

            grvdocumentDetails.DataSource = dtCurrentTable;
            grvdocumentDetails.DataBind();

        }
    }
    else
    {
        Response.Write("ViewState is null");
    }

    Docprpp.Clear();
    SetPreviousData();

}

但是我喜欢添加第二行或更多行并使用下拉列表中的不同值,但它根据图片处理所有行(旧的,新的)相同(文档号,转换,标题)。

 (public static List<string> Docprpp = new List<string>(3);)

请帮助我如何使用此变量并解决我的问题。 enter image description here

 protected void ddlProjectDocument_SelectedIndexChanged(object sender, EventArgs e)
{
    using (_DataContext = new EDMSDataContext())
    {
        int x = Convert.ToInt32(ddlProjectDocument.SelectedValue);
        var subject = from y in _DataContext.tblDocuments
                      where y.DocId == x
                      select y.TITLE;

        var docno = from z in _DataContext.tblDocuments
                    where z.DocId == x
                    select z.DocumentNo;




        Docprpp.Add(docno.SingleOrDefault().ToString());



        var MaxRev = _DataContext.tblTransmittalls.Where(rev => rev.DocID == x).Max(rev => rev.REV);
        int newRev = (MaxRev == null) ? 0 : Convert.ToInt32(MaxRev) + 1;

        Docprpp.Add(newRev.ToString());

        var MaxReview = _DataContext.Project_Documents.Where(rev => rev.DocId == x && rev.Rev == Convert.ToInt32(MaxRev)).Max(rev => rev.Review);
        int newReview = (MaxReview == null) ? newReview = 1 : Convert.ToInt32(MaxReview) + 1;

        Docprpp.Add(subject.SingleOrDefault().ToString());

    }
}

 private void AddNewRow()
{
    int rowIndex = 0;

    if (ViewState["CurrentTable"] != null)
    {
        DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
        DataRow drCurrentRow = null;
        if (dtCurrentTable.Rows.Count > 0)
        {
            for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
            {
                TextBox TextBoxsh = (TextBox)grvdocumentDetails.Rows[rowIndex].Cells[4].FindControl("txtsh");
                DropDownList DrpStatus = (DropDownList)grvdocumentDetails.Rows[rowIndex].Cells[5].FindControl("drpStatus");
                DropDownList DrpClass = (DropDownList)grvdocumentDetails.Rows[rowIndex].Cells[6].FindControl("drpClass");
                drCurrentRow = dtCurrentTable.NewRow();
                drCurrentRow["RowNumber"] = i + 1;

                dtCurrentTable.Rows[i - 1]["Col1"] = Docprpp[0];
                dtCurrentTable.Rows[i - 1]["Col2"] = Docprpp[1];
                dtCurrentTable.Rows[i - 1]["Col3"] = Docprpp[2];
                dtCurrentTable.Rows[i - 1]["Col4"] = TextBoxsh.Text;
                dtCurrentTable.Rows[i - 1]["Col5"] = DrpStatus.SelectedValue;
                dtCurrentTable.Rows[i - 1]["Col6"] = DrpClass.SelectedValue;
                rowIndex++;
            }
            dtCurrentTable.Rows.Add(drCurrentRow);
            ViewState["CurrentTable"] = dtCurrentTable;

            grvdocumentDetails.DataSource = dtCurrentTable;
            grvdocumentDetails.DataBind();

        }
    }
    else
    {
        Response.Write("ViewState is null");
    }

    Docprpp.Clear();
    SetPreviousData();

}

 private void SetPreviousData()
{
    int rowIndex = 0;
    if (ViewState["CurrentTable"] != null)
    {
        DataTable dt = (DataTable)ViewState["CurrentTable"];
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Label TextBoxDoc = (Label)grvdocumentDetails.Rows[rowIndex].Cells[1].FindControl("txtDoc");
                Label TextBoxRev = (Label)grvdocumentDetails.Rows[rowIndex].Cells[2].FindControl("txtRev");
                Label TextBoxtitle = (Label)grvdocumentDetails.Rows[rowIndex].Cells[3].FindControl("txttitle");
                TextBox TextBoxsh = (TextBox)grvdocumentDetails.Rows[rowIndex].Cells[4].FindControl("txtsh");
                DropDownList DrpStatus = (DropDownList)grvdocumentDetails.Rows[rowIndex].Cells[5].FindControl("drpStatus");
                DropDownList DrpClass = (DropDownList)grvdocumentDetails.Rows[rowIndex].Cells[6].FindControl("drpClass");
                // drCurrentRow["RowNumber"] = i + 1;

                grvdocumentDetails.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                TextBoxDoc.Text = dt.Rows[i]["Col1"].ToString();
                TextBoxRev.Text = dt.Rows[i]["Col2"].ToString();
                TextBoxtitle.Text = dt.Rows[i]["Col3"].ToString();
                TextBoxsh.Text = dt.Rows[i]["Col4"].ToString();
                DrpStatus.SelectedValue = dt.Rows[i]["Col5"].ToString();
                DrpClass.SelectedValue = dt.Rows[i]["Col6"].ToString();

                rowIndex++;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

将Docprpp创建/初始化移动到AddNewRow(),不要使用static。