2

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

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

对于添加第一行我没有问题,当我从下拉列表中选择文档时,我可以在字段中使用(文档编号,rev,title),对于其他字段,我可以手动填写。

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

请帮助我如何使用这个变量并解决我的问题。 在此处输入图像描述

 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++;
            }
        }
    }
}
4

1 回答 1

1

将您的 Docprpp 创建/初始化移动到 AddNewRow() 中,并且不要使用静态。

于 2013-04-12T03:55:12.023 回答