在我有一個關於如何(動態添加和從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);
對於將第一行我沒有問題,當我選擇從下拉列表中我可以記錄在田間和我手工填寫的其他領域有(文件編號,rev,標題)。
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++;
}
}
}
}
您的數據中有多少條記錄?在AddnewRow()中設置 – Raptor