none
Creating click event programmatically winforms C# on DataGridView ImageButton Winforms

    Question

  • Hello gurus, I have a little problem here that I'm sure one of you could easily solve. I'm creating my datagridview dynamically using the dataset from my sql query. What I want to do is to associate my DataGridViewImageColumn on each row with a click event. I'm not sure how to do this but I'm certain it could be done via code. Since the grid view doesn't get created until a userID is selected, I am unable to click on the image to expose a click event. Here is the relevant part of my code.

    
    
                if (cbPatientID.SelectedIndex > 0)
                {
                    radioButton1.Visible = false;
                    radioButton2.Visible = false;
     
                    tabControl1.TabPages.Remove(Options);
                    tabControl1.TabPages.Remove(Profiles);
    
                    if (cbPatientID.SelectedIndex == 1)
                    {
                        Form f2 = new CreatePatientRecord();
                        this.Hide();
                        f2.ShowDialog();
                        this.Close();
                    }
                    else
                    {
                        radioButton1.Visible = true;
                        radioButton2.Visible = true;
    
                        BGGridView.Visible = true;
                        BPGridView.Visible = true;
                        FoodGridView.Visible = true;
                        InsPumpGridView.Visible = true;
    
                        Image editimg = Properties.Resources.editor;
                        Image delimg = Properties.Resources.del;
                        DataGridViewImageColumn btnEditBG = new DataGridViewImageColumn();
                        DataGridViewImageColumn btnDelBG = new DataGridViewImageColumn();
                        btnEditBG.HeaderText = "Edit";
                        btnDelBG.HeaderText = "Delete";
                        btnEditBG.Image=editimg;
                        btnDelBG.Image=delimg;
                        BGGridView.Columns.Add(btnEditBG);
                        BGGridView.Columns.Add(btnDelBG);
                        btnEditBG.Width = 30;
                        btnDelBG.Width = 30;
                        
    


    Microsoft Certified Professional


    • Edited by bbcompent1 Thursday, March 07, 2013 5:57 PM
    Thursday, March 07, 2013 5:57 PM

Answers

  • The datagridview already handles the cellclick event, you can use that to determine which column was clicked, gather whatever data you need from that row and process. Put the first line in the code where you create your datagridview.

    demographicsDataGridView.CellClick += new DataGridViewCellEventHandler(demographicsDataGridView_CellClick);

            private void demographicsDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex < 0 || e.RowIndex < 0) return; // header clicked
    
                if (e.ColumnIndex == demographicsDataGridView.Columns["ImageColumn"].Index)
                {
                    // Your logic here. You can gain access to any cell value via DataGridViewCellEventArgs
                    string address = demographicsDataGridView["Address1", e.RowIndex].Value.ToString();
                }
            }


    Thursday, March 07, 2013 11:31 PM

All replies

  • The datagridview already handles the cellclick event, you can use that to determine which column was clicked, gather whatever data you need from that row and process. Put the first line in the code where you create your datagridview.

    demographicsDataGridView.CellClick += new DataGridViewCellEventHandler(demographicsDataGridView_CellClick);

            private void demographicsDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex < 0 || e.RowIndex < 0) return; // header clicked
    
                if (e.ColumnIndex == demographicsDataGridView.Columns["ImageColumn"].Index)
                {
                    // Your logic here. You can gain access to any cell value via DataGridViewCellEventArgs
                    string address = demographicsDataGridView["Address1", e.RowIndex].Value.ToString();
                }
            }


    Thursday, March 07, 2013 11:31 PM
  • I will give this a try and report back as soon as I can. Thank you! Though I have a lot of asp.net experience, only recently have I been trying my hand at windows/console app development. Thank you for your willingness to help me!

    Microsoft Certified Professional

    Friday, March 08, 2013 12:33 PM
  • Works like a charm. Now I have a starting point, thanks again Steve!!!

    Microsoft Certified Professional

    Friday, March 08, 2013 1:08 PM