我正在使用C#中的.NET Webforms,现在我正尝试在按钮单击时在gridview中添加和减去一行。我在页脚模板中有一个添加和删除按钮,我的添加按钮工作正常,但删除了最后一行,同时保留了其他可能输入的数据,这是我拥有的问题。基本上我可以删除最后一行,但是我手工填写多行数据,当我删除最后一行时它都被删除了。我想保留输入的所有数据,只需从gridview中减去最后一行索引。这是我的gridview:
<!-- ADD COURSE GRIDVIEW -->
<asp:GridView ID="Course_Gridview" runat="server" ShowFooter="True" AutoGenerateColumns="false" class="table table-striped" GridLines="None" Visible="false">
<Columns>
<asp:BoundField DataField="RowNumberCourse" HeaderText="Course #" />
<asp:TemplateField HeaderText="Course">
<ItemTemplate>
<asp:DropDownList ID="CourseList" runat="server" DataSourceID="CourseListODS" DataTextField="SubjectName"
DataValueField="CourseID" AppendDataBoundItems="true" CssClass="form-control course-ddl-fix">
<asp:ListItem Value="0">---------[Select]---------</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ErrorMessage="Please select a Program Length value from the list." Display="Dynamic" ControlToValidate="CourseList"
InitialValue="0" Text="*" CssClass="require-fix"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mark">
<ItemTemplate>
<asp:TextBox ID="EnterMark" runat="server" CssClass="form-control pgm-length-fix"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"
ErrorMessage="Please enter a Mark when inserting a course." Text="*" ControlToValidate="EnterMark"
Display="Dynamic" CssClass="require-fix"></asp:RequiredFieldValidator>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add Another Course..." OnClick="ButtonAdd_Click" CssClass="listview-buttons" CausesValidation="False"/>
<asp:LinkButton ID="ButtonSubtract" runat="server" CssClass="btn btn-default btn-sm delete-fix"
OnClick="ButtonSubtract_Click" CausesValidation="false">
<i aria-hidden="true" class="glyphicon glyphicon-remove"></i> Remove...</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
这是减去最后一行的方法:
protected void ButtonSubtract_Click(object sender, EventArgs e)
{
if (ViewState["CurrentTableCourse"] != null)
{
//create new datatable, cast datatable of viewstate
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTableCourse"];
DataRow drCurrentRow = null;
int rowIndex = 0;
if (dtCurrentTable.Rows.Count > 1)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//extract the values
DropDownList courseList = (DropDownList)Course_Gridview.Rows[rowIndex].Cells[1].FindControl("CourseList");
TextBox marks = (TextBox)Course_Gridview.Rows[rowIndex].Cells[2].FindControl("EnterMark");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumberCourse"] = i;
dtCurrentTable.Rows[i - 1]["Column1Course"] = courseList.Text;
dtCurrentTable.Rows[i - 1]["Column2Course"] = marks.Text;
rowIndex++;
}
dtCurrentTable.Rows[rowIndex - 1].Delete();
ViewState["CurrentTableCourse"] = dtCurrentTable;
Course_Gridview.DataSource = dtCurrentTable;
Course_Gridview.DataBind();
}
}
}
除了最后删除的值之外,我如何保留输入的所有值?
答案 0 :(得分:0)
感谢您的回复,但我得到了它。我需要添加以下方法才能相应地工作:
private void SetPreviousCourseData()
{
int rowIndex = 0;
if (ViewState["CurrentTableCourse"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTableCourse"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DropDownList courseList = (DropDownList)Course_Gridview.Rows[rowIndex].Cells[1].FindControl("CourseList");
TextBox marks = (TextBox)Course_Gridview.Rows[rowIndex].Cells[2].FindControl("EnterMark");
courseList.Text = dt.Rows[i]["Column1Course"].ToString();
marks.Text = dt.Rows[i]["Column2Course"].ToString();
rowIndex++;
}
}
}
}
如果(!Page.IsPostBack):
,请调用此方法private void SetInitialCourse()
{
//Create DataTable
DataTable dt = new DataTable();
DataRow dr = null;
//Add initail values to DataTable
dt.Columns.Add(new DataColumn("RowNumberCourse", typeof(string)));
dt.Columns.Add(new DataColumn("Column1Course", typeof(string)));
dt.Columns.Add(new DataColumn("Column2Course", typeof(string)));
//dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dr = dt.NewRow();
dr["RowNumberCourse"] = 1;
dr["Column1Course"] = string.Empty;
dr["Column2Course"] = string.Empty;
//dr["Column3"] = string.Empty;
dt.Rows.Add(dr);
dr = dt.NewRow();
//Store the DataTable in ViewState
ViewState["CurrentTableCourse"] = dt;
Course_Gridview.DataSource = dt;
Course_Gridview.DataBind();
}