Results 1 to 6 of 6

Thread: [RESOLVED] getting the checked state of a boolean value in a dataGridView control

  1. #1

    Thread Starter
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    Resolved [RESOLVED] getting the checked state of a boolean value in a dataGridView control

    Hi there,

    Ok - So I created this simple example. I have dataGridView control which I have added some dataColums to... Check ehm out! Notice that the last on is of type boolean!

    vb Code:
    1. DataTable dt = new DataTable();
    2.             DataColumn dcId = new DataColumn();
    3.             dcId.ColumnName = "ComplexId";
    4.             dcId.DataType = System.Type.GetType("System.Int64");
    5.             dt.Columns.Add(dcId);
    6.  
    7.             DataColumn dcName = new DataColumn();
    8.             dcName.ColumnName = "ComplexName";
    9.             dcName.DataType = System.Type.GetType("System.String");
    10.             dt.Columns.Add(dcName);
    11.  
    12.             DataColumn dcTrueFalse = new DataColumn();
    13.             dcTrueFalse.ColumnName = "True/False\nLook! Two lines...";
    14.             dcTrueFalse.DataType = System.Type.GetType("System.Boolean");
    15.             dt.Columns.Add(dcTrueFalse);
    16.  
    17.             DataRow row = dt.NewRow();
    18.             row["ComplexId"] = 100;
    19.             row["ComplexName"] = "100%";
    20.             dt.Rows.Add(row);
    21.  
    22.             dgSearchResults.DataSource = dt;

    That's all spiffy - and just for test purposes I want to check and see what they values are when you select the row, the code is below
    vb Code:
    1. private void dgSearchResults_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    2.         {
    3.             //MessageBox.Show(dgSearchResults.SelectedRows[0].Cells[0].Value.ToString());
    4.             for (int i = 0; i < dgSearchResults.SelectedRows[0].Cells.Count; i++)
    5.             {
    6.                 MessageBox.Show(dgSearchResults.SelectedRows[0].Cells[i].Value.ToString() + "\nCurrnet index: " + dgSearchResults.SelectedRows[0].Cells[i].ToString());
    7.             }
    8.         }

    only problem is - how can I figure out if the last datacolumn is checked? It doesn't show anything when I run though the array of cells? Am I going abou t this in the wrong way? Is there a more efficient way to do this?

    Thanks!

  2. #2
    Member
    Join Date
    Aug 2001
    Posts
    33

    Re: getting the checked state of a boolean value in a dataGridView control

    You can add:
    dcTrueFalse.DefaultValue = false;

    Then you can do:
    Code:
     if ((bool)dgSearchResults.SelectedRows[0].Cells[2].Value == true)
                    MessageBox.Show("Checked!");
    Last edited by korven; Jun 20th, 2007 at 05:19 PM.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: getting the checked state of a boolean value in a dataGridView control

    You should be binding your DataTable to a BindingSource and binding that BindingSource to the DataGridView. You then interact with the BindingSource in code. The Current property of the BindingSource will return the DataRowView that is bound to the selected row of the grid. This way you're acting on the data itself and not the UI, which should be for the benefit of the user.
    Code:
    DataRowView selectedRow = myBindingSource.Current as DataRowView;
    
    if (selectedRow != null)
    {
        MessageBox.Show(((bool)selectedRow["column name here"]).ToString());
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    Re: [RESOLVED] getting the checked state of a boolean value in a dataGridView control

    Thank you very much - I see I was missing just one piece, actually a rather nice little class... I was very excited to see that the bindingSource class provides a currency manger - in the past I have had to do this myself.

    Just one other note. In order to make it work I had to give the check box a default value of false - without it, it failed no matter what - I don't know why, when I debugged even after I checked it I would look at the actual row (in the debugger) and see the first value, second but then the 3rd item would always be {}... Again, no idea why but that fixed it.

    Thanks

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] getting the checked state of a boolean value in a dataGridView control

    If the column doesn't have a default value then a row will have a null value in that column unless you specifically set the value. You cannot cast a null value as a bool because it isn't one, thus the error.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    Re: [RESOLVED] getting the checked state of a boolean value in a dataGridView control

    Well I have noticed a bit of a problem. I set add and delete to false on the dataGridView. Now the problem is when I click the true/false checkbox and then click cycle through the rows it doesn't see that it is clicked till I click away - say I click the table header.

    I tried calling bs.CurrencyManager.Refresh(); before I run through the list but that doesn't help - it just sets it back to an unchecked state cause as far as the bindingSource is concerned it is unchecked.

    somewhere there is a disconnect - I don't totally understand this but I think it has something to do with the listChangedEvent? maybe... or I am totally confused?

    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width