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
Related
I use a DataGridView to view and edit data in my PostgreSQL database, which works fine. I want to make it a little more user-friendly by choosing the right Control to input data. Specifically, I want to create a ComboBox in a column to let the user search and select a value fast.
To do so, I think I need the EditingControlShowing event to fill the combobox. However, the column I get is of type DataGridViewTextBoxColumn, so the corresponding e.Control is a TextBox instead of a ComboBox.
I never initialise those columns, because they come from views in the database. How can I cast/initialise the column to DataGridViewComboBoxColumn ?
This is how I populate my DataGridView:
dgView.DataSource = getView();
getView() returns a DataTable as can be gotten from NpgsqlDataAdaper.
Since you want to use something other than a DataGridViewTextBoxColumn you need to set AutoGenerateColumns to false and actually define the columns yourself. When defining them, choose a DataGridViewComboBox column for the appropriate fields.
This can be done in code, or in the designer, and based off your question I think you should be able to simply do it through the designer.
You should do it in 2 steps.
1. First of all ( as mentioned by Michael) you should set AutoGenerateColumns to false and then create the required columns using the datagrid view columns option (in properties)
2. Then you should loop through your records and assign appropriate values to appropriate columns.
In case you have hundreds of records that you are showing in grid view, than you should consider using server side paging.
I use a DataGridView with AutoGenerateColumns but would like to display one of the columns (integer) as a CheckBoxColum, is there an easy way or do I have to set AutoGenerateColumns off and add all of the columns manually ?
Changing the cellTemplate causes a error
dgvKlanten.AutoGenerateColumns = true;
dgvKlanten.DataSource = dsKlantenBeheer.Tables["Klanten"];
dgvKlanten.Columns["Nieuwsbrief"].Width = 70;
//dgvKlanten.Columns["Nieuwsbrief"].CellTemplate = new DataGridViewCheckBoxCell();
You could just hide the auto-generated column for that column, then manually add a checkbox column to take its place.
On a side note, I'm assuming this integer column only supports values zero and one? Since this is an integer and not a boolean like the checkbox expects, you may have to use the grid events, such as CellFormatting, to read the value from the data source and set the checkbox accordingly, but I'm not sure. Or, if this is an object, you could expose the integer value as a boolean using another, custom property. No matter what, you are likely going to need to do more work than simply adding a checkbox column and pointing it to the datasource column.
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
well i have worked with datagridview now i need to work with a listview i dont know how to select the value it has in one cell..
my another option is when a cell was added or deleted, all this information is on datagridview
but it doesn't have a (datasourcechanged) then how can i anyone of these?
I tried secund with this code:
DataGridView1.DataSource = lvDevices
so i could select anyone but i need when in datagrid changed its value, it passed to datagridview well my another way is
how can i select the value of one cell in a ListView?
you could try this in this listview1_doubleclick event. ...
string a = listView1.SelectedItems[0].SubItems[column index corresponding selected cell].Text.
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