删除C#中的DataSet的一条数据 和修改一条数据,怎么写?请给个例子,谢谢!
DataSet是一个数据集,需要一个东西来装里面的数据,我这里是用DataGrid
前台的:
<asp:datagrid id="DataGrid1" runat="Server"
AllowSorting="True"
AutoGenerateColumns="False"
cellpadding="3"
DataKeyField="id"
OnDeleteCommand="ItemsGrid_Dele"
OnEditCommand="ItemsGrid_Edit"
OnCancelCommand="ItemsGrid_Cancel"
OnUpdateCommand="ItemsGrid_Update"
BackColor="White"
BorderStyle="None"
PageSize="20"
PagerStyle-HorizontalAlign="Right"
OnPageIndexChanged="DataGridPage"
AllowPaging="True" Height="100%" Width="100%" >
<PagerStyle HorizontalAlign="Right" Mode="NumericPages"></PagerStyle>
<AlternatingItemStyle BackColor="White"></AlternatingItemStyle>
<ItemStyle BackColor="#99FFCC"></ItemStyle>
<HeaderStyle BackColor="#FFCC99"></HeaderStyle>
<Columns>
<asp:EditCommandColumn
UpdateText="更新"
HeaderText="数据更新"
CancelText="取消"
EditText="编辑">
</asp:EditCommandColumn>
<asp:ButtonColumn
Text="删除"
HeaderText="删除"
CommandName="Delete">
</asp:ButtonColumn>
<asp:TemplateColumn
SortExpression="pid"
HeaderText="编号">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"id")%>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="商品名称">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "CPmingcheng")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="CPmingcheng"
Text='<%#DataBinder.Eval(Container.DataItem,"CPmingcheng")%>' runat="Server"/>
<asp:RequiredFieldValidator ControlToValidate="CPmingcheng" Display="Dynamic" runat="Server" ErrorMessage="<br>输入不能为空!"
ID="Requiredfieldvalidator1" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="商品价格">
<ItemStyle HorizontalAlign="Center">
</ItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"CPjiage")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="CPjiage"
Text='<%# DataBinder.Eval(Container.DataItem,"CPjiage")%>' runat="Server"/>
<asp:RequiredFieldValidator ControlToValidate="CPjiage" Display="Dynamic" runat="Server" ErrorMessage="<br>输入不能为空!"
ID="Requiredfieldvalidator2" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="库存">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "kucunqingkuang")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="kucunqingkuang"
Text='<%# DataBinder.Eval(Container.DataItem,"kucunqingkuang")%>' runat="Server"/>
<asp:RequiredFieldValidator ControlToValidate="kucunqingkuang" Display="Dynamic" runat="Server" ErrorMessage="<br>输入不能为空!"
ID="Requiredfieldvalidator3" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="等级">
<ItemStyle HorizontalAlign="Center">
</ItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"shangpindengji")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="shangpindengji"
Text='<%# DataBinder.Eval(Container.DataItem,"shangpindengji")%>' runat="Server"/>
<asp:RequiredFieldValidator ControlToValidate="shangpindengji" Display="Dynamic" runat="Server" ErrorMessage="<br>输入不能为空!"
ID="Requiredfieldvalidator4" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="商品型号">
<ItemStyle HorizontalAlign="Center">
</ItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"shangpinxinghao")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="shangpinxinghao"
Text='<%# DataBinder.Eval(Container.DataItem,"shangpinxinghao")%>' runat="Server"/>
<asp:RequiredFieldValidator ControlToValidate="shangpinxinghao" Display="Dynamic" runat="Server" ErrorMessage="<br>输入不能为空!"
ID="Requiredfieldvalidator5" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="商品介绍">
<ItemStyle HorizontalAlign="Center">
</ItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"shangpinjieshao")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="shangpinjieshao"
Text='<%# DataBinder.Eval(Container.DataItem,"shangpinjieshao")%>' runat="Server"/>
<asp:RequiredFieldValidator ControlToValidate="shangpinjieshao" Display="Dynamic" runat="Server" ErrorMessage="<br>输入不能为空!"
ID="Requiredfieldvalidator6" />
</EditItemTemplate>
</asp:TemplateColumn>
<%--<asp:TemplateColumn HeaderText="分类">
<ItemStyle HorizontalAlign="Center">
</ItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"shangpinfenlei")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="shangpinfenlei"
Text='<%# DataBinder.Eval(Container.DataItem,"shangpinfenlei")%>' runat="Server"/>
<asp:RequiredFieldValidator ControlToValidate="shangpinfenlei" Display="Dynamic" runat="Server" ErrorMessage="<br>输入不能为空!"
ID="Requiredfieldvalidator7" />
</EditItemTemplate>
</asp:TemplateColumn>--%>
<asp:TemplateColumn HeaderText="上市日期">
<ItemStyle HorizontalAlign="Center">
</ItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"shangshiriqi")%>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
后台:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class admin_Maproducts : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["branch"] == null && Session["username"] == null)
{ Response.Write("<script>window.alert('您还未登录,请您先登录!');window.location.href='../login.aspx';</script>"); }
else if (Session["branch"].ToString() == "0" && !this.IsPostBack)
{
GetSource();
}
}
protected void GetSource()
{
string strSQL="SELECT * FROM products";
SqlConnection A = DB.createcon();
SqlDataAdapter D = new SqlDataAdapter(strSQL,A ) ;
DataSet ds = new DataSet () ;
D.Fill (ds,"products" );
DataGrid1.DataSource = ds.Tables["products"].DefaultView;
DataGrid1.DataBind();
ds.Dispose();
A.Close();
}
protected void ItemsGrid_Dele(Object sender, DataGridCommandEventArgs e)
{
string strSQL = "DELETE FROM products WHERE id =@id";
SqlConnection A = DB.createcon();
SqlCommand B = new SqlCommand(strSQL, A);
B.Parameters.Add(new SqlParameter("@id", SqlDbType.Int, 4)).Value = this.DataGrid1.DataKeys[e.Item.ItemIndex];
A.Open();
B.ExecuteNonQuery();
B.Dispose();
A.Close();
DataGrid1.EditItemIndex = -1;
GetSource();
}
protected void ItemsGrid_Edit(Object sender, DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
GetSource();
}
protected void ItemsGrid_Cancel(Object sender, DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
GetSource();
}
protected void ItemsGrid_Update(Object sender, DataGridCommandEventArgs e)
{
TextBox CPmingcheng = (TextBox)e.Item.FindControl("CPmingcheng");
TextBox CPjiage = (TextBox)e.Item.FindControl("CPjiage");
TextBox kucunqingkuang = (TextBox)e.Item.FindControl("kucunqingkuang");
TextBox shangpindengji = (TextBox)e.Item.FindControl("shangpindengji");
TextBox shangpinxinghao = (TextBox)e.Item.FindControl("shangpinxinghao");
TextBox shangpinjieshao = (TextBox)e.Item.FindControl("shangpinjieshao");
TextBox shangpinfenlei = (TextBox)e.Item.FindControl("shangpinfenlei");
string strSQL = "UPDATE products SET ";
strSQL += "CPmingcheng='" + CPmingcheng.Text + "',";
strSQL += "CPjiage='" + CPjiage.Text + "',";
strSQL += "kucunqingkuang='" + kucunqingkuang.Text + "',";
strSQL += "shangpindengji='" + shangpindengji.Text + "',";
strSQL += "shangpinxinghao='" + shangpinxinghao.Text + "',";
strSQL += "shangpinjieshao='" + shangpinjieshao.Text + "',";
strSQL += "shangpinfenlei='" + shangpinfenlei.Text + "'";
strSQL += " WHERE id=@id";
SqlConnection A = DB.createcon();
SqlCommand B = new SqlCommand(strSQL, A);
B.Parameters.Add(new SqlParameter("@id", SqlDbType.Int, 4)).Value = this.DataGrid1.DataKeys[e.Item.ItemIndex];
A.Open();
try
{
B.ExecuteNonQuery();
}
catch { Response.Write("失败!"); }
finally { B.Dispose(); A.Close(); }
DataGrid1.EditItemIndex = -1;
GetSource();
}
protected void DataGridPage(Object sender, DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
GetSource();
}
protected void Button1_Click(object sender, EventArgs e)
{
string mysql = null;
if (this.DropDownList1.SelectedItem.Text == "商品名称")
{
mysql = "select * from products where CPmingcheng like '%" + this.TextBox1.Text + "%'";
}
if (this.DropDownList1.SelectedItem.Text == "商品型号")
{
mysql = "select * from products where shangpinxinghao like '%" + this.TextBox1.Text + "%'";
}
SqlConnection A = DB.createcon();
A.Open();
SqlDataAdapter D = new SqlDataAdapter(mysql, A);
DataSet ds = new DataSet();
D.Fill(ds, "products");
DataGrid1.DataSource = ds.Tables["products"].DefaultView;
DataGrid1.DataBind();
ds.Dispose();
A.Close();
TextBox1.Text = "";
}
}
你试试看........修改一下就可以用的,我测试过的了!