如何根据记录在gridview中创建复选框 [英] How to create checkbox in gridview depending on the record
问题描述
我知道datagridview的基础知识。
我可以在gridview中显示表格的内容。
现在我的问题是如何为gridview中的所有记录创建一个复选框。
然后如何根据复选框点击选择记录。
请帮我摆脱这个问题
I know the basics of datagridview.
I can display the contents of a table in gridview.
Now my question is that how to create a checkbox for all the records in the gridview.
Then how to select the the record based on the checkbox click.
Please do help me to get rid of this problem
推荐答案
研究这个例子并适应你的需要:
aspx页面:
Study this example and adapt to your need:
the aspx page:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default31.aspx.cs" Inherits="Default31" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</form>
</body>
</html>
背后的代码:
the code behind:
using System;
using System.Web.UI.WebControls;
using System.Data;
public partial class Default31 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == true)
return;
DataTable dtTemp = new DataTable();
dtTemp.Columns.Add("Student ID");
dtTemp.Columns.Add("First Name");
dtTemp.Columns.Add("Last Name");
dtTemp.Rows.Add("1", "Peter", "Leow");
dtTemp.Rows.Add("2", "James", "Song");
dtTemp.Rows.Add("3", "Mick", "Sam");
dtTemp.Rows.Add("4", "MAry", "Cool");
GridView1.DataSource = dtTemp;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chkBox = (CheckBox)row.FindControl("CheckBox1");
if (chkBox != null && chkBox.Checked)
{
TextBox1.Text = row.Cells[2].Text.ToString();
}
}
}
}
在Gridview中添加复选框
这是asp.net项目的格式并拖动gridview控件
Add Checkbox in Gridview
Here is the format for asp.net project and drag the gridview control
<asp:gridview id="GridVwHeaderChckbox" runat="server" autogeneratecolumns="False" xmlns:asp="#unknown">
Font-Names="Verdana" PageSize="5" Width="55%" BorderColor="#CCCCCC" BorderStyle="Solid"
BorderWidth="1px">
<alternatingrowstyle backcolor="#BFE4FF" />
<pagerstyle bordercolor="#CCCCCC" borderstyle="Solid" borderwidth="1px" />
<headerstyle height="30px" backcolor="#6DC2FF" font-size="15px" bordercolor="#CCCCCC">
BorderStyle="Solid" BorderWidth="1px" />
<rowstyle height="20px" font-size="13px" bordercolor="#CCCCCC" borderstyle="Solid">
BorderWidth="1px" />
<columns>
<asp:boundfield datafield="Emp_Name" headertext="Employee Name" headerstyle-width="150px" />
<asp:boundfield datafield="Emp_job" headertext="Job title" headerstyle-width="150px" />
<asp:boundfield datafield="Emp_Dep" headertext="Department" headerstyle-width="150px" />
<asp:templatefield itemstyle-width="40px">
<headertemplate>
<asp:checkbox id="chkboxSelectAll" runat="server" onclick="CheckAllEmp(this);" />
</headertemplate>
<itemstyle horizontalalign="Center" verticalalign="Middle" />
<itemtemplate>
<asp:checkbox id="chkEmp" runat="server"></asp:checkbox>
</itemtemplate>
</asp:templatefield>
</columns>
</rowstyle></headerstyle></asp:gridview>
< br $>
选择并取消选中复选框
Select and deselect checkbox
<asp:templatefield itemstyle-width="40px" xmlns:asp="#unknown">
<headertemplate>
<asp:checkbox id="chkboxSelectAll" runat="server" autopostback="true" oncheckedchanged="chkboxSelectAll_CheckedChanged" />
</headertemplate>
<itemstyle horizontalalign="Center" verticalalign="Middle" />
<itemtemplate>
<asp:checkbox id="chkEmp" runat="server"></asp:checkbox>
</itemtemplate>
</asp:templatefield>
将autopostback设置为true并在复选框中创建OnCheckedChanged事件
set autopostback as true and create OnCheckedChanged event in checkbox
protected void chkboxSelectAll_CheckedChanged(object sender, EventArgs e)
{
CheckBox ChkBoxHeader = (CheckBox)GridVwHeaderChckboxSrvr.HeaderRow.FindControl("chkboxSelectAll");
foreach (GridViewRow row in GridVwHeaderChckboxSrvr.Rows)
{
CheckBox ChkBoxRows = (CheckBox)row.FindControl("chkEmp");
if (ChkBoxHeader.Checked == true)
{
ChkBoxRows.Checked = true;
}
else
{
ChkBoxRows.Checked = false;
}
}
}
参考:http: //www.aspsnippets.com/Articles/GridView-with-CheckBox-Get-Selected-Rows-in-ASPNet.aspx
问候,
Praveen Nelge
Reference : http://www.aspsnippets.com/Articles/GridView-with-CheckBox-Get-Selected-Rows-in-ASPNet.aspx
Regards,
Praveen Nelge
这篇关于如何根据记录在gridview中创建复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!