1

私は 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();
            //    }
            //}
        }
    }
}
} 
4

1 に答える 1

0

私はこれを正しく読んでいることを願っています。

データでいっぱいの SQL のテーブルがありますか? 「詳細」と「価格」という2つの列?

同じ列を持つ Datagridview を持つ Winform がありますか?

また、クエリを実行できるように接続していますか?

それで、私はこれを考えるのが正しいですか?もしそうなら、ここに行きます。

それを行うにはいくつかの方法がありますが、SQL 経由で簡単に確認する方法もあります。

そのため、単純な Select ストアド プロシージャを作成します。

CREATE PROCEDURE [dbo].[ValidationResult]
@particular as varchar(50)
AS
SELECT Particulars, Price FROM Yourtable
WHERE Particulars = @particular

簡単なクイック クエリで、あとはリンクを設定するだけです。

public DataTable SQL_ValidateCheck(string part)
    {
        try
        {
            DataTable tbl_val = new DataTable();
            using (SqlConnection AutoConn = new SqlConnection(conn32))
            {
                AutoConn.Open();
                using (SqlCommand InfoCommand = new SqlCommand())
                {
                    using (SqlDataAdapter infoAdapter = new SqlDataAdapter(InfoCommand))
                    {
                        InfoCommand.Connection = AutoConn;
                        InfoCommand.CommandType = CommandType.StoredProcedure;
                        InfoCommand.CommandText = "dbo.ValidationResults";
                        InfoCommand.Parameters.AddWithValue("@particular", part);
                        InfoCommand.CommandTimeout = 180;

                        infoAdapter.Fill(tbl_val );
                        AutoConn.Close();
                        return tbl_val ;
                    }
                }
            }
        }
        catch (Exception e)
        {
            //MessageBox.Show("Error in connection :: " + e);

            return tbl_val ;
        }
    }

これで、Validation Select クエリの結果を含むテーブルができました。

最後に行う必要があるのは、それを使用することです。

private void Check_Val()
{
    DataTable myTable = new DataTable();
    myTable = SQL_ValidateCheck(mygrid.CurrentSelection.Cells[0].Value.ToString());
    //Iterate the table for the result, maybe use an if(mytable == null) or something
}

グリッドのイベントでこのメソッドを使用する可能性がありますが、完全にあなた次第です。構文が間違っている可能性があります。最近 Telerik で作業しているので、構文を確認してください。ただし、アイデアは得られるはずです。

于 2013-04-24T11:13:51.930 に答える