简体   繁体   中英

Adding datatable row and column and updating datagridview

            string conString = "Server=192.168.1.100;Database=product;Uid=newuser;Pwd=password";
            MySqlConnection conn = new MySqlConnection(conString);
            DataTable dt = new DataTable();
            DataRow row = dt.NewRow();
            conn.Open();
            //cmd = conn.CreateCommand();
            //cmd.CommandText = "Select * From tblindividualproduct";
            if (e.KeyCode == Keys.Enter)
            {
                if (txtBarcode.Text == "")
                {
                    MessageBox.Show("Please Fill the correct ProductID");
                }
                else
                {
                    string sql = "Select * From tblindividualproduct where ProductID = @ProductIdText";

                    using (var adapt = new MySqlDataAdapter(sql, conn))
                    using (var cmd = new MySqlCommandBuilder(adapt)) //Not sure what you need this for unless you are going to update the database later.
                    {
                        adapt.SelectCommand.Parameters.AddWithValue("@ProductIdText", txtBarcode.Text);
                        BindingSource bs = new BindingSource();
                        adapt.Fill(dt);
                        bs.DataSource = dt;
                        dgItems.ReadOnly = true;
                        dgItems.DataSource = bs;
                    }
                }
            }

How do I make the results to add, not just replace the last result. This is the whole code as requested. I just dont know if it needs manually adding of rows or there is an easy way to make it. Thank in advance

First of all, you should use Parameterized SQL to avoid SQL Injection .

Then you just need to put the code you have in an event handler of some sort. I would think the user would probably hit the enter key or something in the TextBox .

As long as you don't clear your DataTable it will just keep adding rows every time you run the query. If you want to clear the table at some other point in your code just call dt.Clear() .

Something like this should get what you want and be safe from injection:

    private void txtBarCode_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            if(String.IsNullOrEmpty(txtBarcode.Text)) 
            {
                MessageBox.Show("Please Fill the correct ProductID");
            } 
            else 
            {
                 string sql = "Select * From tblindividualproduct where ProductID = @ProductIdText";

                 using (var adapt = new MySqlDataAdapter(sql, conn))
                 using (var cmd = new MySqlCommandBuilder(adapt)) //Not sure what you need this for unless you are going to update the database later.
                 {
                     adapt.SelectCommand.Parameters.AddWithValue("@ProductIdText", txtBarCode.Text);
                     BindingSource bs = new BindingSource();
                     adapt.Fill(dt);
                     bs.DataSource = dt;
                     dgItems.ReadOnly = true;
                     dgItems.DataSource = bs;
                 }
            }
        }
    }

Then make sure you have attached this event to your TextBox somewhere in your Form class like this:

this.txtBarCode.KeyUP += txtBarCode_KeyUp;

Now when the user types in the box and presses enter the query will run and the results will display in the DataGridView . No need to manually add columns and rows, the adapt.Fill(dt); Will do that for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM