VS2008中用gridview如何实现添加,删除信息(数据库用的SQL)
我写的删除信息的程序publicpartialclassManageMessage:System.Web.UI.Page{protectedvoidPage_Load(o...
我写的删除信息的程序
public partial class ManageMessage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string str = "Data Source=localhost;Initial Catalog=OnlineLearningSystem;User ID=sa;Password=123";
SqlConnection Conn = new SqlConnection(str);
DataSet ds = new DataSet();
string cmdQuery = "SELECT * FROM MessageBoard";
Conn.Open();
SqlCommand Cmd = new SqlCommand(cmdQuery, Conn);
SqlDataAdapter sda = new SqlDataAdapter(Cmd);
sda.Fill(ds);
Message1.DataSource = ds;
Message1.DataBind();
}
protected void Message1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string str = "Data Source=localhost;Initial Catalog=OnlineLearningSystem;User ID=sa;Password=123";
SqlConnection Conn = new SqlConnection(str);
string cmdQuery = "delete from MessageBoard where Message='" + Message1.Rows[e.RowIndex].Cells[0].ToString() + "'";
SqlCommand Cmd = new SqlCommand(cmdQuery, Conn);
SqlDataAdapter sda = new SqlDataAdapter(Cmd);
Conn.Open();
Cmd.ExecuteNonQuery();
Conn.Close();
点了删除没反应,跪求高手帮忙!!
页面代码:
<asp:GridView ID="Message1" runat="server" AllowPaging="True" CellPadding="4"
ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" onrowdeleting="Message1_RowDeleting"
>
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="Name" HeaderText="用户名" />
<asp:BoundField DataField="Message" HeaderText="内容" />
<asp:BoundField DataField="PostDate" HeaderText="日期" />
<asp:ButtonField Text="删除" CommandName="delete" />
</Columns>
</asp:GridView> 展开
public partial class ManageMessage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string str = "Data Source=localhost;Initial Catalog=OnlineLearningSystem;User ID=sa;Password=123";
SqlConnection Conn = new SqlConnection(str);
DataSet ds = new DataSet();
string cmdQuery = "SELECT * FROM MessageBoard";
Conn.Open();
SqlCommand Cmd = new SqlCommand(cmdQuery, Conn);
SqlDataAdapter sda = new SqlDataAdapter(Cmd);
sda.Fill(ds);
Message1.DataSource = ds;
Message1.DataBind();
}
protected void Message1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string str = "Data Source=localhost;Initial Catalog=OnlineLearningSystem;User ID=sa;Password=123";
SqlConnection Conn = new SqlConnection(str);
string cmdQuery = "delete from MessageBoard where Message='" + Message1.Rows[e.RowIndex].Cells[0].ToString() + "'";
SqlCommand Cmd = new SqlCommand(cmdQuery, Conn);
SqlDataAdapter sda = new SqlDataAdapter(Cmd);
Conn.Open();
Cmd.ExecuteNonQuery();
Conn.Close();
点了删除没反应,跪求高手帮忙!!
页面代码:
<asp:GridView ID="Message1" runat="server" AllowPaging="True" CellPadding="4"
ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" onrowdeleting="Message1_RowDeleting"
>
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="Name" HeaderText="用户名" />
<asp:BoundField DataField="Message" HeaderText="内容" />
<asp:BoundField DataField="PostDate" HeaderText="日期" />
<asp:ButtonField Text="删除" CommandName="delete" />
</Columns>
</asp:GridView> 展开
展开全部
首先GridView的属性datakeys绑定你表的主键ID列
然后添加删除列
激活事件
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
//再把id做为参数传给删除语句,注意类型转换
"delete from MessageBoard where Message="+id
}
然后添加删除列
激活事件
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
//再把id做为参数传给删除语句,注意类型转换
"delete from MessageBoard where Message="+id
}
展开全部
1、当你的GRIDVIEW显示出数据说明你已经把GRIDVIEW和数据'集成'到了一起。
2、把GRIDVIEW属性中的ReadOnly的值设成False
---
以上,希望对你有所帮助。
2、把GRIDVIEW属性中的ReadOnly的值设成False
---
以上,希望对你有所帮助。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
.......
GRIDVIEW的修改和删除是可以直接进行的
把属性中的ReadOnly的值设成False就行了
GRIDVIEW的修改和删除是可以直接进行的
把属性中的ReadOnly的值设成False就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
一般我都不用RowDeleting事件,而是rowcommand事件
PS.要养成在Page_Load事件里面写 IsPostBack !!!
PS.要养成在Page_Load事件里面写 IsPostBack !!!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先触发Rowcommand事件,然后再触发RowDeleting事件。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询