一个网格视图复选框选择数据添加另一个网格视图
问题描述:
这是html代码...............
This is html code..............
<asp:GridView ID="grdEmployee" runat="server" Width="753px" onrowdatabound="grdEmployee_RowDataBound" AutoGenerateColumns="False"
AllowPaging="True" Height="154px"
onpageindexchanging="grdEmployee_PageIndexChanging" PageSize="6">
<columns>
<asp:TemplateField>
<itemtemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</itemtemplate>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" onclick="javascript:SelectheaderCheckboxes(this)" />
</HeaderTemplate>
<itemstyle width="20px" />
<asp:BoundField DataField="EmployeeName" HeaderText="EmployeeName" ItemStyle-Width="20px" >
<itemstyle width="200px" />
<asp:BoundField DataField="Designation" HeaderText="Designation" ItemStyle-Width="20px" >
<itemstyle width="200px" />
</columns>
<pagerstyle backcolor="#999999" forecolor="Black" horizontalalign="Center" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<alternatingrowstyle backcolor="#DCDCDC" />
这是Csharp Code ........
This is Csharp Code........
protected void btnAdd_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("EmployeeName",typeof(string));
dt.Columns.Add("Designation",typeof(string));
foreach (GridViewRow gvRow in grdEmployee.Rows)
{
//int RowNo = 0;
//RowNo = RowNo + 1;
CheckBox checkbox = (CheckBox)gvRow.Cells[0].FindControl("CheckBox1");
if (checkbox.Checked)
{
DataRow row = dt.NewRow();
row["EmployeeName"] = gvRow.Cells[1].Text;
row["Designation"] = gvRow.Cells[2].Text;
dt.Rows.Add(row);
}
}
grdAddEmployee.DataSource = dt;
grdAddEmployee.DataBind();
}
它不能正常工作.复选框始终选择false.请帮助我快速
it is not working properly.checkbox select always false.pls help me quick
答
如果有帮助,请尝试此操作.
Hi,
Try this if could help.
//instantiate new ArrayList to hold our checked items
ArrayList checkedItems = new ArrayList();
CheckBox chk;
string chkBoxIndex = string.Empty;
//loop through each row in the GridView
foreach (GridViewRow row in grdEmployee.Rows)
{
//get the index of the current CheckBox
chkBoxIndex = (string)grdEmployee.DataKeys[row.RowIndex].Value.ToString();
chk = (CheckBox)row.FindControl("CheckBox1");
if (!(Session["CheckedItems"] == null))
{
checkedItems = (ArrayList)Session["CheckedItems"];
}
if (chk.Checked)
{
if (!(checkedItems.Contains(chkBoxIndex)))
{
//add to the list
checkedItems.Add(chkBoxIndex);
}
else
{
//remove from list since it's unchecked
checkedItems.Remove(chkBoxIndex);
}
}
}
var counts = checkedItems.Count;
// Now Check the contains of the checkedItems
// Should contains the Primary Index key...
如果可以的话请投票...
问候
Please vote if could help...
Regards,