WPF access DataGrid row element - c#

I've a got strange situation. I've got a datagrid, and in one of the columns is a datepicker and checkbox. The checkbox if selected will disable the datepicker.
*****************************
* | *
* DatePicker | Checkbox *
* | *
*****************************
I've had a stab at this, and a poke around but can't seem to find where I'd access the datepicker row object to disable it. ItemArray seems to enumerate the column values fine, but I need direct access so I can set IsEnabled
object selected = the_datagrid.SelectedItem;

Why don't you just bind DatePicker.IsEnabled to CheckBox.IsChecked?

It should be possible to poke around in the visual tree to find the DatePicker next to the checkbox that the user clicked. Another solution would be to add a bool property that the checkbox sets if checked and that controls IsEnabled of the DataPicker. The DatePicker and Checkbox should have the same DataContext since they are on the same row.

Related

Devexpress Winforms Gridview RowSelection Checkbox in intermediate state

I'm using WinForms Devexpress and bumped into the following task:
Somehow I need for DevExpress GridView to programmatically set data row selection checkbox to intermediate state (as the state of the top checkbox at the header at the picture below:
I'm wondering if it is possible or how to overcome the issue.
You should set the column editor's AllowGrayed property to true.
this.yourBooleanColumn.ColumnEdit = this.repositoryItemCheckEdit1;
this.repositoryItemCheckEdit1.AllowGrayed = true;

WPF datagrid - how to set checkbox in template column without binding?

I have a datagrid, which is populated programatically by data retrieved from a webservice. The last column of the datagrid contains checkboxes, which allow to show each rows value on a map. The column is created with a template column like this:
DataGridTemplateColumn column = new DataGridTemplateColumn();
column.Header = "Show on map";
DataTemplate dt = new DataTemplate();
FrameworkElementFactory checkBoxFactory = new FrameworkElementFactory(typeof(CheckBox));
checkBoxFactory.AddHandler(CheckBox.ClickEvent, new RoutedEventHandler(CheckBoxClicked), true);
dt.VisualTree = checkBoxFactory;
column.CellTemplate = dt;
DataGrid.Columns.Add(column);
If the users clicks on one of the checkboxes, the CheckBoxClicked method is invoked and displays the corresponding row values as an icon on the map (as already mentioned). This works very well. However, the user may close the window containing the datagrid, whereas the map stays on the screen, together with the displayed icons.
Now, when the user reopens the window with the datagrid, I'd like to preselect the checkbox, according to the already showing icons. Since the checkboxes do not have a binding, I'm unable to match the icons to them.
How can this be done? Is there a way to programatically preselect them? Or is there still a way to create a binding?
Finally, I've found the solution. Hosch250s comment lead me on the right track (how can I upvote the comment?). The answer can be found here: binding in code.
The easiest way set the checkbox state programmatically is to create this binding:
Binding binding = new Binding("IsChecked");
binding.Mode = BindingMode.OneWay;
binding.Source = this;
checkBoxFactory.SetBinding(CheckBox.IsCheckedProperty, binding);
And implement the property in the current context:
public bool IsChecked {
get {
return doWhateverToFindOutIfChecked();
}
}
The first approach (see my answer with method binding) was simple and strait forward. However, one can not really determine, for which row the binding is called. I've changed the soluntion as followed:
Instead of a DataGridTemplateColumn I'm using a DataGridCheckBoxColumn, now. My data is dynamically stored in rows and bound to the ItemsSource property, using a ListCollectionView. The boolean values, required for the checkbox columns are dynamically inserted into the row values. My datagrid is read only and the selection unit is cell. Settings these properties, I can us the MouseUp event to detect a mouse click on a cell. Inside the handler method, I'll check if the click belonged to a checkbox cell. If so, I change the corresponding boolean value in the row values and call Items.refresh().

How can I tick or untick a checkbox in a datagridview using White

I have a WinForms app, that displays a DataGridView. It is automatically populated from a BindingSource, and contains several rows of data. The columns include the standard things like strings. Some of these columns are CheckBoxes.
Name | User | Admin
---- ---- -----
John | X |
Fred | | X
I am writing some automated tests using TestStack.White. I can read the existing state of the CheckBoxes without issue.
How do I set or clear the checkboxes?
I've tried:
Table.Rows[0].Cells[1].Value = true;
Fails because the underlying row/cell is deleted before White can read back the current value.
And also:
Table.Rows[0].Cells[1].SetValue(true);
Fails to set the new value.
Because your DataGridView is bound to BindingSource, you should change the Boolean value in BindingSource rather than through the dataGridView control.
Because TestStack.White cannot access the underlying object model you will have to come up with a New button or Reset button. Or some GUI way (with logic) to clear the Checkbox values by setting the BindingSource's boolean fields to false.
Here is some psuedo code to illustrate.
private void New_Click(object sender, System.EventArgs e)
{
DataTable dt = (DataTable)dgv.DataSource;
dt[1]["IsAdmin"].value = false;
//In ASP.Net you also need a dgv.Bind(); //this is not necessary in winforms
}
There should already be a way in your system to test with Checkboxes cleared, it should be consistent with how the end user currently do it.
My this be an helpful way to set values? DataGridView checkbox column - value and functionality:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.Cells[CheckBoxColumn1.Name].Value = true;
}
Or what is your Table for an Object?
I believe the check boxes in c# has a property 'Checked'. And even though it's in the DataGridView, I believe the property of the controls remains the same.
So, can you try this instead
Table.Rows[0].Cells[1].Checked = true;
Resources:
http://www.c-sharpcorner.com/uploadfile/mahesh/checkbox-in-C-Sharp3/
I also could not extract checkbox that belongs to datagridviewcheckboxcolumn neither from cell nor from row, and finally ended up with a hack using mouse, something like this:
Rect bounds = table.Rows[rowNo].Cells[colNo].Bounds;
Point center = new Point(bounds.X + bounds.Width / 2, bounds.Y + bounds.Height / 2);
Mouse.Instance.Click(center);

How to make a specific Column Uneditable In datagridview?

Using a DataGridView, how can I make a specific column uneditable while the grid view itself has "Allow editing" enabled?
Also, how can I execute an event when the selected index in a ComboBox in the DataGridView has changed? Here, ComboBox is a column type.
Another question is, how can I make the header title aligned to the center? I can't find the appropriate property.
You've got a few questions here.
(1) How can I make a specific column uneditable in DataGridView?
Set the ReadOnly flag on the particular column you want to make uneditable.
dataGridView.Columns["YourColumnName"].ReadOnly = true;
(2) How can I execute an event when the selected index on a ComboBox in the DataGridView changes?
If it's in your DataGridView, it's not a ComboBox; it's a DataGridViewComboBoxColumn. According to MSDN:
Unlike the ComboBox control, the DataGridViewComboBoxCell does not have SelectedIndex and SelectedValue properties. Instead, selecting a value from a drop-down list sets the cell Value property.
This one I'm not familiar with, as I've never tried it myself. It appears you want to subscribe to the EditingControlShowing event and then see if something like this works for you (with a little tweaking).
(3) How can I make the header title align in the center?
Set the HeaderCell.Style.Alignment
dataGridView.Columns["YourColumnName"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;

Is it possible to create a DataGridTemplateColumn that can switch (dynamically) on row level between checkbox and textblock?

Is it possible to create a DataGridColumn that can contain both TextBlock and Checkbox. Let's say for example that I have defined a Datagrid with 7 Columns one of which is a DataGridCheckBoxColumn on the dataGrid and the others have TextBlocks. Then when I check the checkbox in a row I want all the cells on that row from TextBlocks to become Checkboxes while the rest rows remain unaffected (I am using VS2008).
You could expose a property on your bound object and link everything accordingly:
Property ---+<---> CheckBoxCol
|
+----> TemplateCol >> Trigger ----- TextBlock
|
+--------- CheckBox
So the Checkbox in one column affects said property and the templates of the other columns contain DataTriggers which check is the property is true or false and show the according content.

Categories