PDA

View Full Version : سوال: حذف رکورد از بانک و gridview



mahsa.n
جمعه 19 شهریور 1389, 21:23 عصر
سلام به دوستان
من از این کد برای اضافه کردن کالا استفاده میکنم



<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"
DataKeyNames="SerialNum"DataSourceID="SqlDataSource1"
OnRowCommand="Insert"

<Columns>
<asp:BoundFieldDataField="SerialNum"HeaderText="شماره سریال"ReadOnly="True"
SortExpression="SerialNum"/>
<asp:BoundFieldDataField="Price"HeaderText="قیمت"SortExpression="Price"/>
<asp:BoundFieldDataField="Tedad"HeaderText="تعداد"SortExpression="Tedad"/>
<asp:BoundFieldDataField="Date"HeaderText="تاریخ ثبت"SortExpression="Date"/>
<asp:BoundFieldDataField="Name"HeaderText="نام"SortExpression="Name"/>

<asp:ButtonFieldButtonType="Button"Text="اضافه کردن به سبد خرید"CommandName="Insert"/>

</Columns>

mahsa.n
جمعه 19 شهریور 1389, 21:26 عصر
<asp:gridviewid="shopCart"runat="server"">

<Columns>

<asp:TemplateFieldHeaderText="حذف کالا">
<ItemTemplate>
<asp:ButtonID="btnDelete"runat="server"Text="حذف کالا"CommandName="Delete"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>

mahsa.n
جمعه 19 شهریور 1389, 21:27 عصر
public KharidKalaDataContext db = new KharidKalaDataContext();
public KharidKala tblKharidKala = new KharidKala();
public DataTable Cart;//represent one table of in-memory data
public DataView CartView;//represent databindable,customized view of datatable for sorting,filtering,searching,editing and navigation
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{

if (Session["ShoppingSession"] == null)
{
Cart = new DataTable();
Cart.Columns.Add(new DataColumn("شماره سریال", typeof(string)));
Cart.Columns.Add(new DataColumn("قیمت", typeof(string)));
Session["ShoppingSession"] = Cart;
}
else
{
Cart = (DataTable)Session["shoppingSession"];
}
CartView = new DataView(Cart);
}
}
protected void Insert(object sender, GridViewCommandEventArgs e)
{
int i = Convert.ToInt32(e.CommandArgument); //get the row index
GridViewRow selectedrow = ((GridView)e.CommandSource).Rows[i];//get the gridviewrow where the command is raise
tblKharidKala.SerialNum = Convert.ToInt32(selectedrow.Cells[0].Text);//selectedrow.cell[0]==SerialNum
string item = tblKharidKala.SerialNum.ToString();
tblKharidKala.UserName = Session["username"].ToString();
DateTime d = DateTime.Now;
tblKharidKala.DateOfKharid = d;
string Price = selectedrow.Cells[1].Text;//selectedrow.cell[1]==Price
if (e.CommandName == "Insert")
{
DataRow dr = Cart.NewRow();//create a new datarow with the same schema az the table
dr[0] = tblKharidKala.SerialNum;
dr[1] = Price;
Cart.Rows.Add(dr);
shopCart.DataSource = CartView;
shopCart.DataBind();
db.KharidKalas.InsertOnSubmit(tblKharidKala);
db.SubmitChanges();
}

}

--------------------
حالا چه جوری میشه وقتی روی دکمه delete کلیک شد رکورد از بانک حذف بشه و سطر اون رکورد از shopCard هم حذف بشه