How to get a value of the checkbox in a GridView - c#

I have made a C# windows forms project...
How to get a value of the checkbox in a GridView - C# winforms
I add a gridview to the form...
the first column (with index 0) is checkbox button column...
I used the next code to access the checkbox value:
dataGridView1[0, 0].Value.ToString();
but it didn't work...
and when I try to execute:
MessageBox.Show(dataGridView1[0, 0].ValueType.ToString());
I got type.boolean...
so, How can I get the value of the checkbox...

You should be using the FormattedValue instead of Value
bool isChecked = (bool)dataGridView1[0, 0].FormattedValue;

Just tried...
Made a form with a datagridview with 1 column (checkbox)
added a row then called
MessageBox.Show(dataGridView1[0, 0].Value.ToString());
shows "True" for me.
Probably just a casting issue.
like Hans says use
bool a = (bool)dataGridView1[0, 0].Value

Related

Display first value (by index) of ComboBox as default in a DataGridView

In a Windows Application I have a Combobox in a DataGridView.
My question is:
How do I get the value at the first index as default?
For example, if the ComboBox Status has three values
unknown (1)
accepted (2)
rejected (3)
the value unknown is at the first index and should be selected as default in the DataGridView cell. The ComboBox's text should be set programmatically by index, e.g. by cbbox.selectindex = 1;
How can the first index's text be displayed by default in the DataGridView?
Are you using WPF or WinForms?
If you're using WPF, try this:
DataRowView dr = (DataRowView) YourDataGrid.SelectedItem;
YourComboBox.Text = dr.Row[0].ToString();
Are you asking on how to get the first index value of your datagrid?
In the Designer, you can set the column's DefaultCellStyle NullValue.
dataGridView1.Columns[3].DefaultCellStyle.NullValue = "unknown";

change datagridviewtextbox column value from dropdownlist control

I have a window form that have two comboboxes and a datagridview which is bind with different datasource. one is for datagridview and another one is for both comboxes. This datagridview has two columns named like account1 and account2. these columns contains same value as in comboboxes.
Now my problem is that if i want to change combobox value then the current selected row value shuold change as in combox.
Suppose combobox contains values from 1 to 10. and currently selected value is 3 which is same in datagridview. If i change this combobox value from 3 to 7 then datagridview value should change from 3 to 7.
are u trying to do this
dataGridView1.Rows[0].Cells[0].Value = comboBox1.SelectedValue;//to get the selected text
dataGridView1.Rows[0].Cells[1].Value = comboBox1.SelectedIndex;//to get the selected text index
if not elaborate ur question clearly
If I have understood your query, you may follow as below:
dataGridView1[1, 2].Value = comboBox1.SelectedIndex.ToString();//Here, 1 represents row, 2 represents column.
dataGridView1.Rows[2].Cells[1].Value = comboBox1.SelectedIndex.ToString();
Both of the lines works same as per I know; just different syntax. You may use either.
Best of luck!

DataGridViewCheckBoxCell is not getting checked

DataGridViewCheckBoxCell is not getting checked. I have inserted the first column like this
DataGridView1.Columns.Insert(0, new DataGridViewCheckBoxColumn { Name = "Print" });
This is not working
DataGridView1.Rows[0].Cells[0].Value = true;
Neither this is working
DataGridView1.Rows[0].Cells[0].Value = cell.TrueValue;
What could be the reason it is not getting checked?
I was having issues with this myself. I was using the following code, and I couldnt figure out why it wasn't working right all of the time.
(Note that my code is in VB, but I think the same concept would apply to other .NET languages)
Dim check As DataGridViewCheckBoxCell = DataGridView1.Rows(i).Cells("columnNameHere")
If check.Value = check.TrueValue Then
'do stuff
End If
I then realized that it was because the underlying value of the cell isnt changed until it loses focus. I think DGVs always behave like this, but it can be easy to forget.
My solution was to simply add a little bit of code to the data grid views on click event handler. When the user clicks the checkbox, all it does is shift the focus elsewhere. I shifted the focus to a label so that it doesnt have any unintended consequences. (Ex: if you shift it to a button the user might be surprised when they press enter and a random button activates.) Depending on what else is in your DGV, you may want to check the column index of the event so that you are only doing this for the checkbox columns. All of the other columns in my DGV are read only anyways, so it didnt matter in my case.
Private Sub DataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Label1.Focus()
End Sub
If the code in question was contained in a button, the focus would already be shifted away from the DGV to the button, so this wouldn't be an issue. In my case, the code was activated by a timer - meaning that the DGV cell in question wouldn't necissarily lose focus before the timer fired.
I don't know why it is not working for you, but you can try following method instead.
dataGridView1.Rows[0].SetValues(true);
This will only check the first item. When you want to set values for more cells, just use more parameters.
var values = new bool[] { true, false, true };
dataGridView1.Rows[0].SetValues(values);
This will check the first and the third cell, but the second cell will remain unchecked.
your first line of code actually worked for me:
DataGridView1.Rows[0].Cells[0].Value = true;
checked the first line. the second line isn't working because there is no meaning to cell.TrueValue, and a cell's property TrueValue is not a const of a checked check box
let me just add that the way you address your DataGridView's properties is not very safe and can cause exceptions
Is your datagridview Edit Mode property is set to Edit Programmatically ?
I was having the same issue, So I changed my datagridview Edit Mode property to Edit on Enter. Now it's working fine for me.
Please set your datagridview Edit Mode to Edit on Enter.
If you want to make other (Non Checkbox) columns read only use DataDridView1.Column("Column Name Here Or Index").Readonly = True to made them read only.
Sorry For Late, But other searchers can get some helps from this answer.
If you just want to add new checkbox column and want to check`uncheckcheckboxes in column you need to addTrueValueandFalseValuefor yourCheckBoxColumn`. Go to the Edit Columns dialog and set TrueValue and FalseValue attributes for your column.
Thanks.

Programmatically checking checkbox in a gridview

I have a Datagridview with first column as checkbox.I have created the checkboxcolumn at design itself.While updating the gridview according to the entries from database, I have to check and uncheck the checkbox programatically not all at a time but only a specific row.Please tell me how can I update check boxes programmatically.
You can bind the data from database directly to checkbox column if it is bit type in DB.
Do something like this
(row.Cells[CheckBoxColumn.Index] as DataGridViewCheckBoxCell).value = false;
During bind data you need to check it manually if flag is true then set it checked otherwise unchecked as like
set checkbox1.checked=true or false
Check datagrid prerender event, get reference to the checkbox and set value accordingly.
At the time of databinding use this code
CheckBox chkbx= e.Item.FindControl("CheckBox1") as CheckBox;
then you can manipulate chkbx.Checked to true or false based on your values
and similarly it can be used for rest of the checkboxes buttons

Checkbox column in datagridView

I fill a datagridview with an data table filled by an adapter. I have some columns that are smallint. They are used as flags, like booleans.
How do I display this columns as checkboxes?
Notice, that I can't change the database column type to boolean.
You just need to create an DataGridViewCheckBoxColumn then you tell it what's falseand what's true
this.ckbCol = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this.dataGridView.Columns.Add(this.active);
this.ckbCol.DataPropertyName = "ACTIVE"; //if u want to bind it to a table or something
this.ckbCol.HeaderText = "Aktiv";
this.ckbCol.Name = "Aktiv";
//Now the important stuff follows!
this.ckbCol.FalseValue = "0";
this.ckbCol.TrueValue = "1";
This works just fine for me and it's even possible to set it in the Designer!
You could use a TemplateColumn with DataBinder.Eval properly assigning the checked value to the checkbox or in the RowDataBound event handler you can check the row.DataItem and if your column is '1' you set the checkbox as checked. in the second case you get a reference to the checkbox control using (FindControl("checkboxId") as CheckBox)
You have 2 ways to do this if I remember my .Net correctly. First is the simple one don't use smallint use boolean and is will show you checkboxes by default. The second one is you have to do the gridview programatically. Use TemplateColumn and bind the data programatically in RowDataBound. her's a tutorial to help you get started http://www.asp.net/data-access/tutorials/adding-a-gridview-column-of-checkboxes-vb

Categories