Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I've c# app, and I'm using datagridview with button ,but the event that I put didn't work which is when you press the button something happens?

I used the code from
http://csharp.net-informations.com/datagridview/csharp-datagridview-button.htm[^]

C#
private void ShowProduct_Load(object sender, EventArgs e)
        {
            con.ConnectionString = connection.x;
            try
            {
                con.Open();
                cmd = new SqlCommand("select product_id,product_name,Product_description,product_unitprice,product_sellingprice,product_quantity,total,vendor from product", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
                dataGridView1.ReadOnly = true;
                DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
                dataGridView1.Columns.Add(btn);
                btn.HeaderText = "Click Data";
                btn.Text = "Click Here";
                btn.Name = "btn";
                btn.UseColumnTextForButtonValue = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                if (cmd != null)
                {
                    cmd.Dispose();
                    cmd = null;
                }
                con.Close();
            }
        }
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 8)
            {
                MessageBox.Show((e.RowIndex + 1) + "  Row  " + (e.ColumnIndex + 1) + "  Column button clicked ");
            }
        }
Posted
Comments
Jibesh 19-Dec-12 6:34am    
why datagridview is readOnly. comment that code and see what is happening.
Member 8584763 19-Dec-12 10:32am    
Nothing changes after commenting it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900