PDA

View Full Version : سوال: به دست آوردن primary keys در سطر انتخابی گریدویو



mahsa.n
دوشنبه 24 آبان 1389, 09:36 صبح
سلام به دوستان

این کد


if (e.CommandName == "Delete")
{
// get the NameId of the clicked row
int NameId = Convert.ToInt32(e.CommandArgument);

}


برای به دست آوردنه nameId((Primaty key) در سطر انتخابی از گریدویو هستش
ولی من در بانکم 3 تا primary key دارم به جای e.commandargument چی باید بگذارم؟؟

saeed262626
دوشنبه 24 آبان 1389, 10:19 صبح
سلام
ابتدا تو گرید ویو key رو تعریف کن (DATAKEYNAMES)

blackcats
دوشنبه 24 آبان 1389, 10:31 صبح
command argument رو اسم ستونی بزار که می خوای استفاده کنی

mahsa.n
دوشنبه 24 آبان 1389, 11:44 صبح
command argument رو اسم ستونی بزار که می خوای استفاده کنی
با این کد درست شد:لبخندساده:



protectedvoid GridView1_RowCommand(object sender,GridViewCommandEventArgs e)
{
if (e.CommandName == "delete")
{
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
int nameid = Convert.ToInt32(selectedrow.Cells[0].Text);//selectedrow.cell[0]==nameId
string sendermail = selectedrow.Cells[1].Text;
string date = selectedrow.Cells[2].Text;
// Delete the record
DeleteRecord(nameid, sendermail, date);
}

}

mahsa.n
دوشنبه 24 آبان 1389, 12:25 عصر
حالا نمیدونم چرا همه رکوردهارو پاک میکنه



protected void GridView1_RowCommand(object sender,GridViewCommandEventArgs e)
{
if (e.CommandName == "delete")
{
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
int nameid = Convert.ToInt32(selectedrow.Cells[0].Text);//selectedrow.cell[0]==NameId
string sendermail = selectedrow.Cells[1].Text;//selectedrow.cell[1]==SenderMail
string date = selectedrow.Cells[2].Text;//selectedrow.cell[2]==Date
// Delete the record
DeleteRecord(nameid, sendermail, date);
}

}
private void DeleteRecord(int nameid,string sendermail,string date)
{
PrivateMessage p = new PrivateMessage();
var query= from q in db.PrivateMessages
where q.NameId == nameid && q.SenderMail == sendermail && q.Date == date
select q;
db.PrivateMessages.DeleteOnSubmit(query.First());
db.SubmitChanges();
}


trace که میکنم sendermail و nameid و date دقیقا برابر با اون سطریه که deleteش رو کلیک کردم ولی همه رکوردها پاک میشن چرا؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟/