私は C# と Grid の初心者です。SQL に 2 つの列、Particulars と Price を含むテーブルがあり、両側にいくつかの値があります。一方、C# win.form grid には、Particulars と grid の 2 つの列があります。ユーザーが列1(詳細)にデータを入力し、入力されたデータがテーブル特定の値の値と一致しない場合、例外をスローする必要があります。これにはCellEndEditイベントを使用しましたが、ユーザーが空のセルにデータを入力した後、入力されたデータがDBテーブルの値に従って正しいかどうかを確認するにはどうすればよいですか。
フォームをDBに正常に接続し、VSのデータ設定オプションでこれを正常に実行しましたが、SQLデータベースでそれを行う方法がわかりません。試しましたが、SQLデータベースでの検証で混乱しています.Hereは私のコードです:
namespace Small_Billing_System
{
public partial class hitbill : Form
{
SqlConnection cn = new SqlConnection(@"server=people-pc\sqlexpress;integrated security=true;database=nec");
SqlCommand cm = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
//da1, da2;
// ds1, ds2;
DataSet ds = new DataSet();
SqlCommandBuilder cb = new SqlCommandBuilder();
public hitbill()
{
InitializeComponent();
}
private void control()
{
//dataGridView_2.DataSource = ds;
//dataGridView_2.DataMember = "tblfood";
//totalbox.DataBindings.Add("Text", ds, "tblfood.Price");
}
private void hitbill_Load(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection(@"server=people-pc\sqlexpress;integrated security=true;database=nec");
try
{
cn.Open();
MessageBox.Show("Data base connected");
}
catch (Exception)
{
MessageBox.Show("Data base connection failed");
throw;
}
cn.Open();
cm = new SqlCommand("select * from tblfood", cn);
da = new SqlDataAdapter(cm);
da.Fill(ds, "tblfood");
}
private void dataGridView_2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DataGridViewCell currCell = dataGridView_2.CurrentCell;
string currCellContent = currCell.Value.ToString();
// SqlCommand cmd = new SqlCommand("select * FROM tblfood");
//check whether inputted values are in DB or not
if (dataGridView_2.CurrentCell.ColumnIndex == 0)
{
// if (!((currCellContent == ???????????????.
// if (!((currCellContent == "Beans") || (currCellContent == "Juice"))) //cannot do this because there might be many particulars not just 2 or 3
//And when there are many particulars every time when user inputs value it should be checked with database and throw an exception/error in case of wrong input.
// {
// MessageBox.Show("Paticulars not found");
// dataGridView_2.CurrentCell.Value = "";
// }
// }
// else if (dataGridView_2.CurrentCell.ColumnIndex ==1)
// {
// double R = 0.00;
// string particular = dataGridView_2.CurrentRow.Cells[0].Value.ToString().Trim();
// if (particular == "Beans")
// {
// R = Double.Parse(currCellContent) * Properties.Data1.Default.Apple;
// }
// else if (particular == "Juice")
// {
// R = Double.Parse(currCellContent) * Properties.Data1.Default.Orange;
// }
// else
// {
// R = 0.00;
// }
// dataGridView_2.CurrentRow.Cells[2].Value = R.ToString();
// DataGridViewRowCollection rows = dataGridView_2.Rows;
// double total = 0.00;
// foreach (DataGridViewRow row in rows)
// {
// if (!row.IsNewRow)
// {
// double price = Double.Parse(row.Cells[2].Value.ToString());
// total = total + price;
// }
// }
// totalbox.Text = total.ToString();
// }
//}
}
}
}
}