过滤结果后,DataGridView中的更新/删除按钮不起作用

时间:2017-10-12 14:38:40

标签: c# winforms datagridview

C#winform应用程序:我在每行的datagridview中添加了按钮(更新,删除)。它工作正常,但是当我在此gridview中搜索数据时,按钮不会对搜索结果起作用。 这是我的代码。

namespace MyBusiness
{
    public partial class ExpenceDetails : Form
    {
        public ExpenceDetails()
        {
            InitializeComponent();
        }

        private void ExpenceDetails_Load(object sender, EventArgs e)
        {
            update();
        }

        DataTable data;
        public void update()
        {
            string connectionstring = null;
            SqlConnection cnn;
            connectionstring = @"Server=(local)\SQLEXPRESS;Database=mrtraders;Trusted_Connection=True";
            cnn = new SqlConnection(connectionstring);

            try
            {
                cnn.Open();

                dataGridView1.ColumnCount = 0;

                SqlCommand cmd1 = new SqlCommand("SELECT * FROM Expense", cnn);

                SqlDataReader reader = cmd1.ExecuteReader();
                if (reader.HasRows)
                {
                    data = new DataTable();
                    data.Load(reader);
                    dataGridView1.DataSource = data;
                }

                DataGridViewButtonColumn btn = new DataGridViewButtonColumn();

                btn.Text = "Update";
                btn.UseColumnTextForButtonValue = true;
                dataGridView1.Columns.Add(btn);

                DataGridViewButtonColumn btn2 = new DataGridViewButtonColumn();

                btn2.Text = "Delete";
                btn2.UseColumnTextForButtonValue = true;
                dataGridView1.Columns.Add(btn2);

                cnn.Close();
            }
            catch (Exception)
            {
                myMessageBox my = new myMessageBox("Internal Error...");
                my.ShowDialog();
            }
        }

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

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 4)
            {
                if (e.RowIndex >= 0)
                {
                    //// get ExpenseId//// IMP
                    int id = Convert.ToInt32(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value);
                    string desc = Convert.ToString(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value);
                    int am = Convert.ToInt32(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[3].Value);
                    UpdateExpense a = new UpdateExpense(id,desc,am);
                    a.ShowDialog();

                    dataGridView1.DataSource = null;
                    update();

                }
            }
            else if (e.ColumnIndex == 5)
            {
                if(e.RowIndex >= 0)
                {
                    int id = Convert.ToInt32(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value);
                    string connectionstring = null;
                    SqlConnection cnn;
                    connectionstring = @"Server=(local)\SQLEXPRESS;Database=mrtraders;Trusted_Connection=True";
                    cnn = new SqlConnection(connectionstring);

                    try
                    {
                        cnn.Open();

                        SqlCommand cmd1 = new SqlCommand("Delete FROM Expense where expenseId = '"+id+"' ", cnn);

                        cmd1.ExecuteNonQuery();
                        myMessageBox my = new myMessageBox("Deleted Successfully...");
                        my.ShowDialog();

                        dataGridView1.DataSource = null;
                        cnn.Close();
                        update();
                    }
                    catch (Exception)
                    {
                        myMessageBox my = new myMessageBox("Internal Error...");
                        my.ShowDialog();
                    }
                }
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            DataView dv = new DataView(data);

            dv.RowFilter = string.Format("Description like '%{0}%'", textBox1.Text);
            dataGridView1.DataSource = dv.Table;


        }
    }
}

0 个答案:

没有答案