|
-
Jun 20th, 2007, 04:07 PM
#1
Thread Starter
Hyperactive Member
[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:
DataTable dt = new DataTable();
DataColumn dcId = new DataColumn();
dcId.ColumnName = "ComplexId";
dcId.DataType = System.Type.GetType("System.Int64");
dt.Columns.Add(dcId);
DataColumn dcName = new DataColumn();
dcName.ColumnName = "ComplexName";
dcName.DataType = System.Type.GetType("System.String");
dt.Columns.Add(dcName);
DataColumn dcTrueFalse = new DataColumn();
dcTrueFalse.ColumnName = "True/False\nLook! Two lines...";
dcTrueFalse.DataType = System.Type.GetType("System.Boolean");
dt.Columns.Add(dcTrueFalse);
DataRow row = dt.NewRow();
row["ComplexId"] = 100;
row["ComplexName"] = "100%";
dt.Rows.Add(row);
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:
private void dgSearchResults_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
//MessageBox.Show(dgSearchResults.SelectedRows[0].Cells[0].Value.ToString());
for (int i = 0; i < dgSearchResults.SelectedRows[0].Cells.Count; i++)
{
MessageBox.Show(dgSearchResults.SelectedRows[0].Cells[i].Value.ToString() + "\nCurrnet index: " + dgSearchResults.SelectedRows[0].Cells[i].ToString());
}
}
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!
-
Jun 20th, 2007, 05:06 PM
#2
Member
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.
-
Jun 20th, 2007, 07:03 PM
#3
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());
}
-
Jun 21st, 2007, 08:16 AM
#4
Thread Starter
Hyperactive Member
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
-
Jun 21st, 2007, 08:39 AM
#5
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.
-
Jun 21st, 2007, 12:05 PM
#6
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|