Add a check box to the first column of the datagridview and select a check box.

Source: Internet
Author: User
  1. Datatable DT; (able bound to the datagridview dview)
  2. Datagridview dgview;
  3. DT. Columns. Add ("check", typeof (bool ));
  4. Dgview. Columns. Clear ();
  5. Dgview. autogeneratecolumns = false;
  6. Dgview. datasource = DT;
  7. Datagridviewcheckboxcolumn dtcheck = new datagridviewcheckboxcolumn ();
  8. // (Note that this is the datagridviewcheckboxcolumn instead of the datagridviewtextboxcolumn)
  9. Dtcheck. datapropertyname = "check ";
  10. Dtcheck. headertext = "";
  11. Dgview. Columns. Add (dtcheck );
  12. Dgview. Columns [0]. width = 30;

After adding the workbook, open the small arrow in the upper-right corner of the datagridview form and select the check box before "editable". Otherwise, the check box cannot be used.

If you set the columnheadersvisible attribute of the datagridview to false, there will be no column headers, which is better!

  1. DatatableDt = (Datatable) Dgview. datasource;
  2. // Usage
  3. Foreach (datarow row in DT. Rows)
  4. {
  5. If (row ["check"]. tostring () = "true ")
  6. {
  7. //
  8. }
  9. }

When you cannot select multiple check boxes, add the following code to the cellvaluechanged and currentcelldirtystatechanged events of the dview:

  1. Private void datagridview1_currentcelldirtystatechanged (Object sender, eventargs E)
  2. {
  3. Datagridview dgv = (datagridview) sender;
  4. If (dgv. iscurrentcelldirty)
  5. {
  6. Dgv. commitedit (maid. Commit );
  7. }
  8. }
  9. Private void maid (Object sender, maid E)
  10. {
  11. If (E. columnindex = 0)
  12. {
  13. Datagridview dgv = (datagridview) sender;
  14. Datatable dt = (datatable) dgv. datasource;
  15. Datagridviewcheckboxcell checkcell = (datagridviewcheckboxcell) dgv. Rows [E. rowindex]. cells [0];
  16. Bool value = (Boolean) checkcell. value;
  17. For (INT I = 0; I <DT. Rows. Count; I ++)
  18. {
  19. Datarow ROW = DT. Rows [I];
  20. If (I! = E. rowindex & value)
  21. {
  22. Row ["check"] = false;
  23. }
  24. }
  25. Dgv. invalidate ();
  26. }
  27. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.