PDA

View Full Version : سوال: بروز مشکل در CRUD کردن dgvEX



NasimBamdad
یک شنبه 06 شهریور 1390, 14:31 عصر
این نمونه کد هست که من نوشتم ، همه چیز به خوبی کار می کنه و من از طریق این کد می توانم مقادیری که در DataGridViewEX لود کردم رو EDIT , INSERT , DELETE کنم ،

شرح مشکل :

من یک TEXTBOX قرار دادم که DataGridViewEX رو بر اساس یک شرط برام مرتب می کنه ، در اینجا من کوری شماره پرونده رو قرار دادم و اگه شماره پرونده در TEXTBOX نوشته شود ، اون بیمار رو برام نمایش میده ( همون قسمتی که این توضیح رو دارد ___ //Daroo Usage By Patientid in TEXT_BOX ____ )





دقیقا مشکل من اینجا است که در حالت عادی همه چیز درسته ، یعنی یک سطر رو انتخاب می کنم و می توانم مقادیر K_Jarahi , K_Bihooshi , K_otaghAmal رو EDIT کنم ، و ... ، اما زمانی که Record ها زیاد باشند نمیشه کل DataGridViewEX رو زیر و رو کرد که RECORD مورد نظر رو پیدا کرد ، پس من ( به کمک دوستان ) یک دستور نوشتم که اون بیماری رو لیست کنه که شماره پرونده اش برابر با مقدار TEXTBOX هست و بعد کار راحت تر هست و می خواهم CRUD رو انجام بدم

اما اگرDataGridViewEX رو با اون روش TEXTBOX مرتب کنم و بعد بخواهم EDIT , UPDATE , INSERT انجام بدم نمیشه ، یعنی تغییرات اعمال نمی شود

http://0000.2.img98.net/out.php/i28438_1.gif

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace NoorAutomation
{
public partial class Form3 : Form
{

//You should declare object variables as a class field.
private DataTable dataTable1 = new DataTable();
private SqlConnection SQLCNN1 = new SqlConnection();
private SqlDataAdapter SQLDA1 = new SqlDataAdapter();

public Form3()
{
InitializeComponent();

this.SQLCNN1.ConnectionString = (@"Data Source=.\SQLEXPRESS;initial catalog=NoorAutomation;integrated security=true");
this.SQLDA1.SelectCommand = new SqlCommand("SELECT dbo.patients.fname, dbo.patients.lname, dbo.k_usage.k_jarahi, dbo.k_usage.k_bihooshi, dbo.k_usage.k_otaghAmal, dbo.k_usage.patientid FROM dbo.patients INNER JOIN dbo.k_usage ON dbo.patients.id = dbo.k_usage.patientid", this.SQLCNN1);
this.SQLCNN1.Open();
this.SQLDA1.Fill(this.dataTable1);
this.dataGridViewX1.DataSource = dataTable1;

//Initialize UPDATE Command.
this.SQLDA1.UpdateCommand = new SqlCommand("UPDATE k_usage SET k_jarahi = @k_jarahi, k_bihooshi = @k_bihooshi, k_otaghAmal = @k_otaghAmal WHERE patientid = @patientid", this.SQLCNN1);
this.SQLDA1.UpdateCommand.Parameters.AddWithValue("@k_jarahi", typeof(string)).SourceColumn = "k_jarahi";
this.SQLDA1.UpdateCommand.Parameters.AddWithValue("@k_bihooshi", typeof(string)).SourceColumn = "k_bihooshi";
this.SQLDA1.UpdateCommand.Parameters.AddWithValue("@k_otaghAmal", typeof(string)).SourceColumn = "k_otaghAmal";
this.SQLDA1.UpdateCommand.Parameters.AddWithValue("@patientid", typeof(int)).SourceColumn = "patientid";

//Initialize INSERT Command.
this.SQLDA1.InsertCommand = new SqlCommand("INSERT INTO k_usage (k_jarahi, k_bihooshi, k_otaghAmal) VALUES (@k_jarahi, @k_bihooshi, @k_otaghAmal)", this.SQLCNN1);
this.SQLDA1.InsertCommand.Parameters.AddWithValue("@k_jarahi", typeof(string)).SourceColumn = "k_jarahi";
this.SQLDA1.InsertCommand.Parameters.AddWithValue("@k_bihooshi", typeof(string)).SourceColumn = "k_bihooshi";
this.SQLDA1.InsertCommand.Parameters.AddWithValue("@k_otaghAmal", typeof(string)).SourceColumn = "k_otaghAmal";

//Initialize DELETE Command.
this.SQLDA1.DeleteCommand = new SqlCommand("DELETE FROM k_usage WHERE patientid = @patientid", this.SQLCNN1);
this.SQLDA1.DeleteCommand.Parameters.AddWithValue("@patientid", typeof(int)).SourceColumn = "patientid";


this.SQLCNN1.Close();

}

private void Form3_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'noorAutomationDataSet9._test_dgex' table. You can move, or remove it, as needed.
this.test_dgexTableAdapter.Fill(this.noorAutomatio nDataSet9._test_dgex);
// TODO: This line of code loads data into the 'noorAutomationDataSet9._test_dgex' table. You can move, or remove it, as needed.
this.test_dgexTableAdapter.Fill(this.noorAutomatio nDataSet9._test_dgex);

}

private void buttonX1_Click(object sender, EventArgs e)
{

//Updating DataTable
this.SQLDA1.Update(dataTable1);
MessageBox.Show("DONE");
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
//Daroo Usage By Patientid in TEXT_BOX
SqlConnection cnn = new SqlConnection(@"Data Source=.\SQLEXPRESS;initial catalog=NoorAutomation;integrated security=true");
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
cnn.Open();
cmd.CommandText = "SELECT dbo.patients.fname, dbo.patients.lname, dbo.k_usage.k_jarahi, dbo.k_usage.k_bihooshi, dbo.k_usage.patientid, dbo.k_usage.k_otaghAmal FROM dbo.k_usage INNER JOIN dbo.patients ON dbo.k_usage.patientid = dbo.patients.id WHERE patientid ='" + textBox1.Text + "'";
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridViewX1.DataSource = ds.Tables[0];
cnn.Close();
}
}
}