[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 540
  • Last Modified:

How to set checkbox to true VS C#

I have this query embedded in my VS C# program. The field of interest here is WasImported. I present to the user a list of transactions that have been flagged as imported. If for some reason they want to import it again they check the box to import it again. How do I get the checkbox do have a check in it when this data is presented on the screen? I need to set the value of the checkbox to true for each record that is displayed. Right now, all boxes are displayed with no checks in them even though the underlying value in the database is true.
========================================================================
SQL Query
========================================================================
DataCommand.CommandText = "SELECT Number,ItemNumber,VendorID,ItemGLCombination,convert(char(10),PurchaseDate,101) as PurchaseDate,Purchases," +
                 "_ItemDescription AS 'Description',coalesce(Reference,'') as Reference,WasImported,VendorBatchLoadID " +
                 "FROM_VendorBatchLoadTransaction WHERE WasImported = 1 ORDER BY PurchaseDate,Number,ItemNumber DESC";

=======================================================================
Definition of checkbox
=======================================================================
DataGridViewCheckBoxColumn colCheckBox = new DataGridViewCheckBoxColumn();
                colCheckBox.HeaderText = "Was Imported";
                colCheckBox.Name = "cbxImport";
                colCheckBox.ToolTipText = "Check to include, Uncheck to exclude";

                dgvFlagTransactionsView.Columns.Insert(8, colCheckBox);
                dgvFlagTransactionsView.Columns[8].Width = 50;
0
rwheeler23
Asked:
rwheeler23
  • 3
  • 2
  • 2
1 Solution
 
Miguel OzSoftware EngineerCommented:
It seems that check box is generated dynamically after data is bound to DataGridView .
Is the check box defined before you do a data bind in the DataGridView?
Can you post you binding code?
0
 
rwheeler23Author Commented:
I have attached the entire function.
DisplayTransactions.txt
0
 
käµfm³d 👽Commented:
Is this boolean column included in your SELECT statement? The runtime should automatically create a column for this field in the grid without you having to manually create and insert one.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
rwheeler23Author Commented:
WasImported is a bit field. I thought when I first started with this code it was showing as 0 or 1. I can switch it over and see if it gives me a checkbox.  Is there anything I need to do so it will appear as a checkbox in the dgv?
0
 
Miguel OzSoftware EngineerCommented:
no need to create checkbox column in code
Just make sure that WasImported is a boolean type, then when you assign the data source, the DataGridView will do the job for you, check:
http://stackoverflow.com/questions/5630933/how-to-add-a-checkbox-control-to-a-datatable
0
 
käµfm³d 👽Commented:
I used your code as is with a test database, and it seems to work fine:

Form with CheckBoxTest Database
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace _27858601
{
    public partial class Form1 : Form
    {
        private DataSet DataDataSet;
        private SqlCommand DataCommand;
        private SqlDataAdapter DataDataAdapter;
        private SqlConnection DataConnection;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DataConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=Test;Integrated Security=SSPI");
            DisplayFlagTransactions();
        }

        private void DisplayFlagTransactions()
        {
            try
            {
                /* Define dataset for transactions that failed to import */
                DataDataSet = new System.Data.DataSet();
                DataDataSet.CaseSensitive = false;
                DataCommand = new System.Data.SqlClient.SqlCommand();
                DataCommand.Connection = DataConnection;
                /* DataCommand.CommandType = CommandType.StoredProcedure; */
                DataCommand.CommandText = "SELECT Number,ItemNumber,VendorID,ItemGLCombination,convert(char(10),PurchaseDate,101) as PurchaseDate,Purchases," +
                 "_ItemDescription AS 'Description',coalesce(Reference,'') as Reference,WasImported,VendorBatchLoadID " +
                 "FROM PEF_VendorBatchLoadTransaction WHERE WasImported = 1 ORDER BY PurchaseDate,Number,ItemNumber DESC";
                DataDataAdapter = new System.Data.SqlClient.SqlDataAdapter();
                DataDataAdapter.SelectCommand = DataCommand;
                //_commandBuilder = new SqlCommandBuilder(DataDataAdapter);

                DataDataAdapter.Fill(DataDataSet);

                //dgvFlagTransactionsView.DataError += new DataGridViewDataErrorEventHandler(dgvFlagTransactionsView_DataError);

                dgvFlagTransactionsView.RowHeadersVisible = false;
                dgvFlagTransactionsView.DataSource = DataDataSet.Tables[0].DefaultView;
                dgvFlagTransactionsView.AutoGenerateColumns = true;
                dgvFlagTransactionsView.DefaultCellStyle.NullValue = ' ';
                dgvFlagTransactionsView.AllowUserToAddRows = false;

                dgvFlagTransactionsView.Columns[0].Width = 100;
                dgvFlagTransactionsView.Columns[0].ReadOnly = true;
                dgvFlagTransactionsView.Columns[0].HeaderText = "TXN Number";

                dgvFlagTransactionsView.Columns[2].Width = 140;
                dgvFlagTransactionsView.Columns[2].ReadOnly = true;
                dgvFlagTransactionsView.Columns[2].HeaderText = "Vendor ID";

                dgvFlagTransactionsView.Columns[3].Width = 160;
                dgvFlagTransactionsView.Columns[3].ReadOnly = true;
                dgvFlagTransactionsView.Columns[3].HeaderText = "Item GL Combination";

                dgvFlagTransactionsView.Columns[4].Width = 70;
                dgvFlagTransactionsView.Columns[4].ReadOnly = true;
                dgvFlagTransactionsView.Columns[4].HeaderText = "Purchase Date";

                dgvFlagTransactionsView.Columns[5].Width = 100;
                dgvFlagTransactionsView.Columns[5].ReadOnly = true;
                dgvFlagTransactionsView.Columns[5].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                dgvFlagTransactionsView.Columns[5].DefaultCellStyle.Format = "c";
                dgvFlagTransactionsView.Columns[5].HeaderText = "Trx Amount";

                dgvFlagTransactionsView.Columns[6].Width = 292;
                dgvFlagTransactionsView.Columns[6].ReadOnly = false;
                dgvFlagTransactionsView.Columns[6].HeaderText = "Trx Description";

                dgvFlagTransactionsView.Columns[7].Width = 292;
                dgvFlagTransactionsView.Columns[7].ReadOnly = false;
                dgvFlagTransactionsView.Columns[7].HeaderText = "Trx Reference";

                DataGridViewCheckBoxColumn colCheckBox = new DataGridViewCheckBoxColumn();
                colCheckBox.HeaderText = "Was Imported";
                colCheckBox.Name = "cbxImport";
                colCheckBox.ToolTipText = "Check to include, Uncheck to exclude";

                dgvFlagTransactionsView.Columns.Insert(8, colCheckBox);
                dgvFlagTransactionsView.Columns[8].Width = 50;

                /* for (i = 0; i <= dgvFlagTransactionsView.RowCount - 1; i++)
                    dgvFlagTransactionsView.Rows[i].Cells["cbxImport"].Value = true; */

                foreach (DataGridViewRow row in dgvFlagTransactionsView.Rows)
                {
                    /* row.Cells["cbxImport"].Value = true; */
                    row.Cells[colCheckBox.Name].Value = true;
                }

                dgvFlagTransactionsView.Columns[1].Visible = false;
                dgvFlagTransactionsView.Columns[9].Visible = false;
                dgvFlagTransactionsView.Columns[10].Visible = false;

                dgvFlagTransactionsView.EditMode = DataGridViewEditMode.EditOnEnter;
                dgvFlagTransactionsView.Focus();
            }
            catch (Exception ex)
            {
                string eMsg = "001Flag: ERROR: " + ex.Message;
                //if (stackTraceWanted) eMsg += "\n" + ex.StackTrace;
                MessageBox.Show(eMsg);
            }
        }
    }
}

Open in new window

0
 
rwheeler23Author Commented:
Interesting that is worked for you(kaufmed). When I run it the checkbox is always unchecked even though the value is true in the database. As per maz_oz2003, I simply removed the checkbox altogether and just let the dgv control the show and it worked fine. Thanks for everyone's help on this.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now