Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

c# - Datagridview Button Click event not firing

im using the following code to populate a Gridview.I add 2 buttons for editing and deleting at the end.Hook to the click event.. but after i add the delete button the click events are not firing.What im i doing wrong?

private void BindGrid2()
{
    try
    {
        string constr = "Data Source=INSPIRE-1;" +
        "Initial Catalog=testdatabase;" +
        "User id=testuser;" +
        "Password=tester;";
        using (SqlConnection con = new SqlConnection(constr))
        {
            string commandText = "SELECT invnumber,itemname,quantity,rate FROM mytable2 where invnumber= @name";
            using (SqlCommand command = new SqlCommand(commandText, con))
            {



                command.Parameters.AddWithValue("@name", text_inv.Text);

                using (SqlDataAdapter sda = new SqlDataAdapter(command))
                {
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        dataGridView2.DataSource = dt;
                    }
                }
            }
            if (flag2 == false)
            {

                flag2 = true;
                DataGridViewButtonColumn uninstallButtonColumn = new DataGridViewButtonColumn();
                uninstallButtonColumn.Name = "Edit";
                uninstallButtonColumn.Text = "Edit";
                dataGridView2.Columns.Insert(0, uninstallButtonColumn);
                dataGridView2.Columns[0].DisplayIndex = 4;


                DataGridViewButtonColumn uninstallButtonColumn2 = new DataGridViewButtonColumn();
                uninstallButtonColumn.Name = "Delete";
                uninstallButtonColumn.Text = "Delete";
                dataGridView2.Columns.Insert(5, uninstallButtonColumn2);
                dataGridView2.Columns[5].DisplayIndex = 5;
            }
        }


    }
    catch (Exception error)
    {
        MessageBox.Show(error.Message);
    }
}

void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{

    var senderGrid = (DataGridView)sender;
    string orderId;
    if (e.ColumnIndex == 4)
    {




        try
        {
            orderId = (string)dataGridView1.SelectedCells[0].OwningRow.Cells[1].Value;

            using (SqlConnection conn = new SqlConnection(constr))
            {

                try
                {
                    conn.Open();
                    SqlDataReader myReader = null;
                    string commandText = "select * from mytable2 where invnumber= @name";
                    SqlCommand command = new SqlCommand(commandText, conn);
                    command.Parameters.AddWithValue("@name", text_inv.Text);
                    myReader = command.ExecuteReader();
                    while (myReader.Read())
                    {
                        text_cname.Text = myReader["cname"].ToString();
                        text_quantity.Text = myReader["quantity"].ToString();
                        text_item.Text = myReader["itemname"].ToString();
                        text_rate.Text = myReader["rate"].ToString();
                        dateTimePicker1.Text = myReader["date"].ToString();
                        // textBox4.Text = myReader["stock"].ToString();


                    }
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                }

            }
        }
        catch (Exception error)
        {

        }
    }
    else if (e.ColumnIndex == 5)
    {

        using (SqlConnection conn = new SqlConnection(constr))
        {
            orderId = (string)dataGridView1.SelectedCells[0].OwningRow.Cells[1].Value;
            conn.Open();
            SqlDataReader myReader = null;
            string commandText = "delete from mytable2 where invnumber= @name";
            SqlCommand command = new SqlCommand(commandText, conn);
            command.Parameters.AddWithValue("@name", text_inv.Text);
            myReader = command.ExecuteReader();
        }
        BindGrid2();
    }




}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Corrections below

  1. dataGridView2.Columns.Insert(5, uninstallButtonColumn2) to dataGridView2.Columns.Insert(1, uninstallButtonColumn2)

  2. if (e.ColumnIndex == 4) to if (e.ColumnIndex == 0)

  3. orderId = (string)dataGridView1.SelectedCells[0].OwningRow.Cells[1].Value; to orderId = (string)dataGridView1.SelectedCells[0].OwningRow.Cells[2].Value; at both the places

  4. else if (e.ColumnIndex == 5) to else if (e.ColumnIndex == 1)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...