前台部分内容
<div style="margin-left:auto; margin-right:auto; 1024px;"> <div> <asp:Button ID="lbtnAddRow" runat="server" Text="添加行" OnClick="lbtnAddRow_Click" CssClass="btn"/> <asp:Button ID="btnDeleteRow" runat="server" OnClick="btnDeleteRow_Click" OnClientClick="return confirm('确定要删除选中行吗?');" Text="删除选中行" CssClass="btn"/> <span style="font-weight:bold; font-size:22px;">教育背景</span> </div> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" Width="100%"> <Columns> <asp:BoundField DataField="ID" HeaderText="ID"> <ItemStyle CssClass="hide" /> <HeaderStyle CssClass="hide" /> </asp:BoundField> <asp:TemplateField> <HeaderTemplate> <input id="chkAll" type="checkbox" onclick="SelectAll(this)" /> </HeaderTemplate> <ItemTemplate> <input id="chkRow" type="checkbox" onclick="checkRow(this);" runat="server" /> </ItemTemplate> <ItemStyle Width="30px" HorizontalAlign="Center" /> </asp:TemplateField> <asp:TemplateField HeaderText="起止年月"> <ItemTemplate> <asp:TextBox ID="JybjStarTime" runat="server" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" Text='<%# Bind("起年月") %>'></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator_JybjStarTime" runat="server" ErrorMessage="不能为空" ControlToValidate="JybjStarTime" Display="Dynamic"></asp:RequiredFieldValidator>到 <asp:TextBox ID="JybjFinishTime" runat="server" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" Text='<%# Bind("止年月") %>'></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator_JybjFinishTime" runat="server" ErrorMessage="不能为空" ControlToValidate="JybjFinishTime" Display="Dynamic"></asp:RequiredFieldValidator> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="学校名称"> <ItemTemplate> <asp:TextBox ID="SchoolName" runat="server" Text='<%# Bind("学校名称") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="专业"> <ItemTemplate> <asp:TextBox ID="Major" runat="server" Text='<%# Bind("专业") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="学历/学位"> <ItemTemplate> <asp:TextBox ID="Education" runat="server" Text='<%# Bind("学历") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </div>
pageload事件里面的绑定 ,有数据的话读数据库绑定就行。注释部分去掉的话就会自带一行空白行
protected void bind() { DataTable table = new DataTable(); table.Columns.Add(new DataColumn("ID")); table.Columns.Add(new DataColumn("起年月")); table.Columns.Add(new DataColumn("止年月")); table.Columns.Add(new DataColumn("学校名称")); table.Columns.Add(new DataColumn("专业")); table.Columns.Add(new DataColumn("学历")); //DataRow row = table.NewRow(); //row["ID"] = Guid.NewGuid().ToString(); //table.Rows.Add(row); GridView1.DataSource = table; GridView1.DataBind(); }
教育背景相关操作 代码
#region ===教育背景=== protected void lbtnAddRow_Click(object sender, EventArgs e) { DataTable table = GetGridViewData1(); DataRow newRow = table.NewRow(); newRow["ID"] = Guid.NewGuid().ToString(); table.Rows.Add(newRow); GridView1.DataSource = table; GridView1.DataBind(); } private DataTable GetGridViewData1() { DataTable table = new DataTable(); table.Columns.Add(new DataColumn("ID")); table.Columns.Add(new DataColumn("起年月")); table.Columns.Add(new DataColumn("止年月")); table.Columns.Add(new DataColumn("学校名称")); table.Columns.Add(new DataColumn("专业")); table.Columns.Add(new DataColumn("学历")); foreach (GridViewRow row in GridView1.Rows) { DataRow sourseRow = table.NewRow(); sourseRow["ID"] = row.Cells[0].Text; sourseRow["起年月"] = ((TextBox)row.Cells[2].FindControl("JybjStarTime")).Text; sourseRow["止年月"] = ((TextBox)row.Cells[2].FindControl("JybjFinishTime")).Text; sourseRow["学校名称"] = ((TextBox)row.Cells[3].FindControl("SchoolName")).Text; sourseRow["专业"] = ((TextBox)row.Cells[4].FindControl("Major")).Text; sourseRow["学历"] = ((TextBox)row.Cells[5].FindControl("Education")).Text; table.Rows.Add(sourseRow); } return table; } protected void btnDeleteRow_Click(object sender, EventArgs e) { DataTable table = GetGridViewData1(); foreach (GridViewRow row in GridView1.Rows) { if (((HtmlInputCheckBox)row.Cells[1].FindControl("chkRow")).Checked) { foreach (DataRow dtRow in table.Rows) { if (dtRow["ID"].ToString() == row.Cells[0].Text) { table.Rows.Remove(dtRow); break; } } } } GridView1.DataSource = table; GridView1.DataBind(); } #endregion
保存的话调用一下GetGridViewData1()方法,会返回data table 然后 调用table存数据库就行
DataTable table = GetGridViewData1(); if (table.Rows.Count > 0) { for (int i = 0; i < table.Rows.Count; i++) { string sql = "insert into 教育背景 values('" + table.Rows[i]["ID"].ToString() + "','" + table.Rows[i]["起年月"].ToString() + "','" + table.Rows[i]["止年月"].ToString() + "','" + table.Rows[i]["学校名称"].ToString() + "','" + table.Rows[i]["专业"].ToString() + "','" + table.Rows[i]["学历"].ToString() + "','" + ID + "')"; db.db.ExecuteSql(sql); } }