Click here to Skip to main content
14,428,239 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,,

In my windows form there is a datagridview and i have added checkboxes dynamically using this code
dgResult.AllowUserToAddRows = false;

                   //Below i create on check box column in the datagrid view
                   dgResult.Columns.Clear();
                   DataGridViewCheckBoxColumn colCB = new DataGridViewCheckBoxColumn();

                   //set name for the check box column
                   colCB.Name = "chkcol";
                   colCB.HeaderText = "";
                   //If you use header check box then use it
                   colCB.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                   dgResult.Columns.Add(colCB);


                   //Select cell where checkbox to be display
                   Rectangle rect = this.dgResult.GetCellDisplayRectangle(0, -1, true);            //0 Column index -1(header row) is row index

                   //Mention size of the checkbox
                   chkbox.Size = new Size(18, 18);

                   //set position of header checkbox where to places
                   rect.Offset(40, 2);
                   chkbox.Location = rect.Location;

                   chkbox.CheckedChanged += chkBoxChange;

                   //Add CheckBox control to datagridView
                   this.dgResult.Controls.Add(chkbox);



Now i want to get the selected checkbox and get the entire row values ... Is it possible , if so can anyone help me...

Thanks in advance
Posted
Comments
praks_1 17-Jul-13 2:14am
   
U can do event bubbling and get the selected row
Rate this:
Please Sign up or sign in to vote.

Solution 1

hello friend,

Try like this...


for (int i = 0; i < GridView1.Rows.Count; i++)
           {
               CheckBox check1=(CheckBox)GridView1.Rows[i].FindControl("Check1");

               if (check1.Checked == true)
               {                  
                    String rowval1= GridView1.Rows[i].Cells[1].Text;                  
                    String rowval2 = GridView1.Rows[i].Cells[2].Text;                  
               }
   
v3
Rate this:
Please Sign up or sign in to vote.

Solution 2

Thanks All For Your Answers...
I did it this way and it got me the required result
foreach (DataGridViewRow row in dgResult.Rows)
               {
                   if (row.Cells[SelectColumnIndex].Value != null && Convert.ToBoolean(row.Cells[SelectColumnIndex].Value) == true)
                   {
                       objTempDevice.DeviceName = row.Cells["DeviceName"].Value.ToString();
                       objTempDevice.BroadworksDeviceId = row.Cells["BroadworksDeviceID"].Value.ToString();
                       objTempDevice.GroupId = row.Cells["BusinessVoIP"].Value.ToString();
                       objTempDevice.ServiceProviderId = row.Cells["ServiceProviderId"].Value.ToString();
                       lstDeviceDetails.Add(objTempDevice);
                   }
               }
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100