在Asp.NET中GridView使用
来源:互联网 发布:创建employee数据库 编辑:程序博客网 时间:2024/05/18 14:44
<asp:GridViewID="GridView1" runat="server" AutoGenerateColumns="False"
AllowPaging="True" PageSize="4" DataKeyNames="pid"
Width="332px" onpageindexchanging="GridView1_PageIndexChanging"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBoxID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundFieldDataField="pid" HeaderText="省id"/>
<asp:BoundFieldDataField="pname" HeaderText="省名字"/>
<asp:CommandFieldShowEditButton="True"/>
<asp:CommandFieldShowDeleteButton="True"/>
</Columns>
</asp:GridView>
<asp:ButtonID="Button1" runat="server" Text="删除选择的项"onclick="Button1_Click"/>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace WebApplication1
{
publicpartial class WebForm3 : System.Web.UI.Page
{
privatestring connstr = "data source=.;database=master;uid=sa;pwd=sadamu";
protectedvoid Page_Load(object sender,EventArgs e)
{
if(!this.IsPostBack)
{
BindData();
}
}
privatevoid BindData()
{
string sqlstr = "select * from province";
DataTable dt = GetDataTable(sqlstr);
GridView1.DataSource = dt;
GridView1.DataBind();
}
privateDataTable GetDataTable(string sqlstr)
{
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
SqlCommand cmd = new SqlCommand(sqlstr, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
//分页时触发的事件
protectedvoid GridView1_PageIndexChanging(object sender,GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindData();
}
//删除事件
protectedvoid GridView1_RowDeleting(object sender,GridViewDeleteEventArgs e)
{
int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex][0].ToString());
//下面是删除代码
//DataKeys是指的所有键的值
}
//修改事件
protectedvoid GridView1_RowEditing(object sender,GridViewEditEventArgs e)
{
int ID = Convert.ToInt32(GridView1.DataKeys[e.NewEditIndex][0].ToString());
//然后开始进行修改
}
//删除所有选择的项
protectedvoid Button1_Click(object sender,EventArgs e)
{
for (int i = 0; i <= GridView1.Rows.Count; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (cbox.Checked == true)
{
string sqlstr = "delete from province where pid='" + GridView1.DataKeys[i].Value +"'";
//删除代码省略
}
}
}
}
}
- 在Asp.NET中GridView使用
- 在Asp.net MVC项目中GridView使用
- Asp.net中GridView使用详解
- Asp.net中GridView使用详解
- Asp.net中GridView使用详解(引)
- Asp.net中GridView使用详解
- Asp.net中GridView使用详解
- Asp.net中GridView使用详解
- Asp.net中GridView使用详解
- Asp.net中GridView使用详解
- Asp.net中GridView使用详解
- Asp.net中GridView使用详解(引)
- Asp.net中GridView使用详解
- 【asp.net】GridView中模板的使用
- Asp.net中GridView使用详解(引)
- Asp.net中GridView使用详解
- Asp.net中GridView使用详解(引)
- Asp.net中GridView使用详解
- Parallel Programming with Microsoft Visual C++
- 黑马程序员_字符串和包装类
- 更改数据库文件的数据文件目录位置
- CSDN博客 Android 客户端下载, 阅读博客和资讯,搜索博客,离线阅读
- LuaServer
- 在Asp.NET中GridView使用
- 设置datagridview的某一列可见与否时,前提是该datagridview所在的容器可见
- Android杂谈--内存泄露(1)--contentView缓存使用与ListView优化
- jquery 在页面上根据ID定位(jQuery锚点跳转及相关操作) 经典
- 窗体Controls的OfType<>方法的使用
- 在 Mac 下使用 TeamViewer 进行远程协助
- Asp.NET中DataList 用法
- JS操作iframe
- C#多线程编程简述