asp grid 增加和删除行数据

<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; Height: 170px">
<tr>
<td>
<table>
<tr>
<td>请选择参加考人员:</td>
</tr>
<tr>
<td>
<div style="height: 200px; overflow-y: scroll">
<asp:GridView ID="EmployeeGrid" runat="server" AutoGenerateColumns="False" Style="height: 170px" AllowPaging="True" PageSize="1000">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="WorkNumber" HeaderText="工号" SortExpression="WorkNumber" />
<asp:BoundField DataField="EmployeeName" HeaderText="姓名" />
<asp:BoundField DataField="ChineseName" HeaderText="岗位" />
<asp:BoundField DataField="JobName" HeaderText="部门" />
</Columns>
</asp:GridView>
</div>
</td>
<td style="width: 120px">
<asp:Button Text=" ==> " runat="server" ID="addWorker" OnClick="addWorker_Click" BorderStyle="None" /><br />
<br />
<asp:Button Text=" <== " runat="server" ID="deleteWorker" BorderStyle="None" OnClick="deleteWorker_Click" /><br />
<br />
<a id="removeAll" href="javascript:void(0);" class="easyui-linkbutton" data-options="width:80">移除全部</a>
</td>
<td>
<div style="height:200px;overflow-y:scroll;border:1px solid #808080;width:471px">
已选人员:
<asp:GridView ID="selectedGrid" runat="server" AutoGenerateColumns="False" style="width:450px;" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="WorkNumber" HeaderText="工号" SortExpression="WorkNumber" />
<asp:BoundField DataField="EmployeeName" HeaderText="姓名" />
<asp:BoundField DataField="ChineseName" HeaderText="岗位" />
<asp:BoundField DataField="JobName" HeaderText="部门" />
</Columns>
</asp:GridView></div>
</td>
</tr>
</table>
</td>
</tr>
</table>

 

 

 

protected void addWorker_Click(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("WorkNumber"));
table.Columns.Add(new DataColumn("EmployeeName"));
table.Columns.Add(new DataColumn("ChineseName"));
table.Columns.Add(new DataColumn("JobName"));
foreach (GridViewRow row in selectedGrid.Rows)
{
DataRow sourseRow = table.NewRow();
sourseRow["WorkNumber"] = row.Cells[1].Text;
sourseRow["EmployeeName"] = row.Cells[2].Text;
sourseRow["ChineseName"] = row.Cells[3].Text;
sourseRow["JobName"] = row.Cells[4].Text;
table.Rows.Add(sourseRow);
}
int rowCount = this.EmployeeGrid.Rows.Count;
for (int i = 0; i < rowCount; i++)
{
CheckBox tempChk = (CheckBox)EmployeeGrid.Rows[i].FindControl("CheckBox1");
if (tempChk.Checked == true)
{
DataRow sourseRow = table.NewRow();
sourseRow["WorkNumber"] = EmployeeGrid.Rows[i].Cells[1].Text;
sourseRow["EmployeeName"] = EmployeeGrid.Rows[i].Cells[2].Text;
sourseRow["ChineseName"] = EmployeeGrid.Rows[i].Cells[3].Text;
sourseRow["JobName"] = EmployeeGrid.Rows[i].Cells[4].Text;
table.Rows.Add(sourseRow);
((CheckBox)EmployeeGrid.Rows[i].FindControl("CheckBox1")).Checked=false;
}
}
this.selectedGrid.DataSource = table;
this.selectedGrid.DataBind();
}
protected void deleteWorker_Click(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("WorkNumber"));
table.Columns.Add(new DataColumn("EmployeeName"));
table.Columns.Add(new DataColumn("ChineseName"));
table.Columns.Add(new DataColumn("JobName"));
foreach (GridViewRow row in selectedGrid.Rows)
{
DataRow sourseRow = table.NewRow();
sourseRow["WorkNumber"] = row.Cells[1].Text;
sourseRow["EmployeeName"] = row.Cells[2].Text;
sourseRow["ChineseName"] = row.Cells[3].Text;
sourseRow["JobName"] = row.Cells[4].Text;
table.Rows.Add(sourseRow);
}
foreach (GridViewRow row in selectedGrid.Rows)
{
if (((CheckBox)row.Cells[0].FindControl("CheckBox1")).Checked)
{
foreach (DataRow dtRow in table.Rows)
{
if (dtRow["WorkNumber"].ToString() == row.Cells[1].Text)
{
table.Rows.Remove(dtRow);
break;
}
}
}
}
selectedGrid.DataSource = table;
selectedGrid.DataBind();
}

posted @ 2017-02-04 14:20  恋之呓  阅读(322)  评论(0编辑  收藏  举报