尝试在datagridview上更新数据直接

时间:2015-07-01 14:21:24

标签: c# visual-studio

我正在尝试直接在datagridview上编辑数据库中的数据,但由于某些原因我遇到了错误。

  

对象引用未设置为对象的实例

我尝试了一些不同的方法,但没有奏效,有同样的错误。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Threading;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication2
{
    public partial class Principal : Form
    {
        SqlConnection con;
        SqlDataAdapter adap;
        SqlCommandBuilder scb;
        DataSet ds;
        SqlCommandBuilder cmdb1;

        public Principal()
        {
            InitializeComponent();
            Load += new EventHandler(Principal_Load);
        }


        private void fusionButton1_Click(object sender, EventArgs e)
        {
            AdicionarFornecedor add = new AdicionarFornecedor();
            add.ShowDialog();
        }

        private void fusionButton2_Click(object sender, EventArgs e)
        {
            VerFornecedores add = new VerFornecedores();
            add.ShowDialog();
        }

        private void Principal_Load(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con;
                SqlDataAdapter adap;
                DataSet ds;
                con = new SqlConnection();
                con.ConnectionString = (@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|PAPPloran.mdf;Integrated Security=True;Connect Timeout=30");
                con.Open();
                adap = new SqlDataAdapter("select * from Pagamentos", con);
                ds = new System.Data.DataSet();
                adap.Fill(ds, "P");
                dataGridView1.DataSource = ds.Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show("Erro\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }


        }

        private void fusionTheme1_Click(object sender, EventArgs e)
        {

        }

        private void fusionButton3_Click(object sender, EventArgs e)
        {
            AdicionarPagamento add2 = new AdicionarPagamento();
            add2.ShowDialog();
        }

        private void fusionButton4_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|PAPPloran.mdf;Integrated Security=True;Connect Timeout=30");
            con.Open();
            SqlCommand cmd = new SqlCommand(@"delete from Pagamentos WHERE (IdFornecedor = '" + textBox3.Text + "')", con);
            cmd.ExecuteNonQuery();
            MessageBox.Show("Dados Eliminados com Sucesso ! ");
            textBox3.Text = "";
            con.Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            con = new SqlConnection();
            con.ConnectionString = (@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|PAPPloran.mdf;Integrated Security=True;Connect Timeout=30");
            con.Open();
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from Pagamentos where NomeFornecedor like ('" + textBox1.Text + "%')";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            dataGridView1.DataSource = dt;

            con.Close();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void fusionButton5_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
                if (row.Cells[7].Value != null && row.Cells[7].Value.ToString() == "1- Pago")
                {
                    row.DefaultCellStyle.BackColor = Color.GreenYellow;

                }
                else 
                {
                    row.DefaultCellStyle.BackColor = Color.Tomato;
                }
        }

        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;
        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        private void fusionTheme1_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
        }

        private void fusionButton6_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, DataGridViewCellFormattingEventArgs e)
        {
        }

        private void fusionButton7_Click(object sender, EventArgs e)
        {
            SqlConnection con;
            SqlDataAdapter adap;
            DataSet ds;
            con = new SqlConnection();
            con.ConnectionString = (@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|PAPPloran.mdf;Integrated Security=True;Connect Timeout=30");
            con.Open();
            adap = new SqlDataAdapter("select * from Pagamentos", con);
            ds = new System.Data.DataSet();
            adap.Fill(ds, "P");
            dataGridView1.DataSource = ds.Tables[0];
        }

        private void fusionButton8_Click(object sender, EventArgs e)
        {
            con.ConnectionString = (@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|PAPPloran.mdf;Integrated Security=True;Connect Timeout=30");
            con.Open();
            SqlCommand cmd = new SqlCommand(@"delete from Pagamentos WHERE (NomeFornecedor = '" + textBox3.Text + "')", con);
            cmd.ExecuteNonQuery();
            MessageBox.Show("Dados Eliminados com Sucesso ! ");
            textBox3.Text = "";
            con.Close();
        }

        private void Principal_Load_1(object sender, EventArgs e)
        {
        }

        private void fusionButton9_Click(object sender, EventArgs e)
        {
            try
            {
                cmdb1 = new SqlCommandBuilder(adap);
                adap.Update(ds);
                MessageBox.Show("Data updated", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
    }

http://i.stack.imgur.com/1lSIl.png

0 个答案:

没有答案