PDA

View Full Version : اشکال در متد حذف



Bahar_HS
چهارشنبه 24 مهر 1387, 07:59 صبح
با سلام
در برنامه ای که نوشتم موقع Load شدن فرم ،همه رکوردهای جدول نمایش داده میشه،
با انتخاب سطر موردنظر ، محتوای سلولهای DataGrideViewبه کنترلهای متناظر روی فرم برمی گرده و می خوام با استفاده از مقادیر کنترلها ،عملیاتهای "ذخیره" ، "ویرایش" ، "حذف" را انجام بدم ،
برای متد حذف این کد رو نوشتم که موقع Debug کردن برنامه Erorrمیده،
میخوام "حذف" همزمان هم از DataGrideView وهم از DataBase انجام بشه،
همین طور در مورد متدهای "ذخیره " و "ویرایش"



if (radioButton1.Checked)
strdegree = "دکترا";
else if (radioButton7.Checked)
strdegree = "کارشناسی ارشد";
else if (radioButton2.Checked)
strdegree = "کارشناسی";
else if (radioButton3.Checked)
strdegree = "کاردانی";

if (radioButton4.Checked)
strkhedmat = "طرحی";
else if (radioButton5.Checked)
strkhedmat = "پبام آور";
else if (radioButton6.Checked)
strkhedmat = "ضریب کا";

myDataAdapter.DeleteCommand = new SqlCommand();
myDataAdapter.DeleteCommand.Connection = con;
myDataAdapter.DeleteCommand.CommandText = "DELETE FROM Table1 WHERE finishday is NULL and finishmounth is NULL and finishyear is NULL and name = '"+txtName.Text +"'" + " and family = '"+ txtfamily.Text+"'"+"and namefather = N'"+txtnamefather.Text+"'"+"and birth = '"+txtbirth.Text+"'"+ " and numbershe = '"+txtnumbershe.Text+"'"+"and degreerr = '"+txtdegreerr.Text+"'"+"and khedmatm = '"+txtkhedmatm.Text+"'"+"and degree = '"+strdegree+"'"+"and khedmat = '"+strkhedmat+"'"+"and startday = '"+comboBox1.Text+"'"+"and startmounth = '"+comboBox2.Text+"'"+"and startyear = '"+comboBox3.Text+"'"+"and birthday ='"+comboBox4.Text+"'"+"and birthmounth = '"+comboBox5.Text+"'"+"and birthyear = '"+comboBox6.Text+"'" ;

myDataAdapter.DeleteCommand.CommandType = CommandType.Text;
//con.Open();
myDataAdapter.Fill(myDataSet, "Table1");
//con.Close();
mydataGridView.AutoGenerateColumns = true;
mydataGridView.DataSource = myDataSet;
mydataGridView.DataMember = "Table1";
myDataAdapter.Update(myDataSet


منون میشم اگه راهنمایی کنید ، چه دستوری در کد کمه؟؟

rostamkhani
چهارشنبه 24 مهر 1387, 16:27 عصر
سلام شما میتونین با استفاده از SqlCommandBuilder و آماده کردن DataAdapter برای
myDataAdapter.Update با هر تغییری در DataGrid یا در DataTable و اجرای myDataAdapter.Update
تغییرات رو هم در DataGrid و هم زمان در DataBase اعمال کنید .



dt.rows.add();
dataGridv.Rows.Remove(dataGridv.SelectedRows[0]);
dgvRow.Cells[i].Value = "مقدار جدید";