I have a grid with two columns and multiple rows, each cell containing a number of controls. Among these controls I have a button which should, when I press it, delete all the controls in the current grid cell. How can I get the index of the grid cell my button is in and how can I delete all controls in this cell?
Does this work for you? you'll need to add a using statement for System.Linq
//get the row and column of the button that was pressed.
var row = (int)myButton.GetValue(Grid.RowProperty);
var col = (int)myButton.GetValue(Grid.ColumnProperty);
//go through each child in the grid.
foreach (var uiElement in myGrid.Children)
{ //if the row and col match, then delete the item.
if (uiElement.GetValue(Grid.ColumnProperty) == col && uiElement.GetValue(Grid.RowProperty) == row)
myGrid.Children.Remove(uiElement);
}
using linq and extending the previous answer, notice the ToList() so you can immediately remove the element
//get the row and column of the button that was pressed.
var row = (int)myButton.GetValue(Grid.RowProperty);
var col = (int)myButton.GetValue(Grid.ColumnProperty);
//go through each child in the grid.
//if the row and col match, then delete the item.
foreach (var uiElement in myGrid.Children.Where(uiElement => (int)uiElement.GetValue(Grid.ColumnProperty) == col && (int)uiElement.GetValue(Grid.RowProperty) == row).ToList())
{
myGrid.Children.Remove(uiElement);
}
Related
I created datatable and fill it with columns and rows then i "add" this datatable to datagrid and my question is:
How to hide one row? In datatable or datagrid it doesnt matter for me. Its possible to do that?
In Windows Forms was something like CurrencyManager and that do all the job.
Okay, I found a wayto hide rows:
for (int i = 0; i < dataGrid.Items.Count; i++)
{
DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator
.ContainerFromIndex(i);
Console.WriteLine(row.GetIndex());
// Console.WriteLine(row.Item);
Console.WriteLine((row.Item as DataRowView).Row.ItemArray[3].ToString());
string plec = (row.Item as DataRowView).Row.ItemArray[3].ToString();
if (plec == "M")
{
row.Focus();
row.Visibility = System.Windows.Visibility.Collapsed;
}
}
But when rows are Collapsed after click on column header to asceding or descending my datagrid show all rows(collapsed rows too).
How to block this and use filter only on visble rows?
I have problem related to datagridview in Winform.
I have a list of table names in my left panel. When I click on Table I show the table content in right panel. I am showing data in a datagridview by fetching data and assigning datasource to dgv.
I am setting following property to dgv.
dgTemp.Dock = DockStyle.Fill;
dgTemp.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
dgTemp.AutoSize = true;
dgTemp.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dgTemp.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
dgTemp.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
dgTemp.ReadOnly = true;
dgTemp.AutoGenerateColumns = true;
dgTemp.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dgTemp.AllowUserToAddRows = false;
My problem is there can be any number of columns in Datasource I am assigning to dgv. So if there are very few number of columns (say 1 or 2) the dgv size stands very small and the empty space on right side give very ugly look.
I can't use auto autosizecolumnmode to fill since when there is more columns all columns get shrink and expanding columns dont give me Scroll at bottom
so My requirement is
All space in datagridview should be filled. (should cover all area)
When there is more column, there should appear scroll, so it give better look
are there any events or properties which I can use ??
Thanks in anticipation.
Try this :
dgTemp.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
Update :
dataGridView1.FirstDisplayedScrollingRowIndex = 5; //use 5 because you want to start from 5
//you can have a horizontal scroll bar with this code :
dataGridView1.FirstDisplayedScrollingColumnIndex = 10; //you can choose every column you wanna start with that column
Update 2 :
int rows = dataGridView1.Rows.Count;
int columns = dataGridView1.Columns.Count;
if (rows < 5 && columns < 10)
{
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}
else
{
dataGridView1.FirstDisplayedScrollingRowIndex = 5; //use 5 because you want to start from 5
//you can have a horizontal scroll bar with this code :
dataGridView1.FirstDisplayedScrollingColumnIndex = 10; //you can choose every column you wanna start with that column
}
I bound a DataTable to a datagrid in wpf. The first column has unique values. Now I would like to autoselect a cell in the second column whose first column cell has the given value. How should I achieve that? For example,here is my datagrid:
Name | Age
cat | 2
dog | 3
When user input 'dog', I will need the '3' to be selected.
I tried the method show here:
How to select a row or a cell in WPF DataGrid programmatically?
However, I cannot figure out the displaying row number. Even though I know the row number of the dataTable, the display number can be different since I allow users to sort the table.Thanks a lot.
Set your grid's SelectionUnit property to "Cell", and assuming you feed the DataGrid with the table's DefaultView:
private void button1_Click(object sender, RoutedEventArgs e)
{
// Search for the source-row.
var Element = MyDataTable.AsEnumerable()
.FirstOrDefault(x => x.Field<string>("Name") == "horse");
if (Element == null) return;
// Found the row number in the DataGrid
var RowOnGrid = MyGrid.Items.OfType<DataRowView>()
.Select((a, Index) => new { data=a.Row, index = Index })
.Where(x=> x.data == Element)
.Select(x => x.index)
.FirstOrDefault();
// Assuming the desired column is the second one.
MyGrid.SelectedCells.Clear();
MyGrid.SelectedCells.Add(new DataGridCellInfo(MyGrid.Items[RowOnGrid], MyGrid.Columns[1]));
}
It should work even if you re-sort the rows.
I have a DataGrid bound to a matrix.
The main logic of the application is that any cell having a value of 0 will just display nothing (0 = no value in my referential).
Now what I want is that, when the user presses the Delete button, all the cells selected will be set to 0.
I can easily do that with one cell (because I keep track of the current row/column selected), but I can't get, let's say, a 2x2 square correctly.
The best way I figured out, using Binding magic, would be to get the row/column indexes of each cell selected, and then set the Binding's source to 0. But still, I can't have all of the row numbers (basically because grid.SelectedCells is an IEnumerable<DataGridCellInfo>, and I can only get the column with this object, I have no way to get the current row.)
It works when I only select one cell(or one row), by using the following:
DataGridRow dataGridRow = (DataGridRow)this.ItemContainerGenerator.ContainerFromItem(grid.CurrentItem);
How about multiple selection?
Any ideas here? Thanks!
Try this to get to the selected cells value:
DataGridCellInfo cell = dataGrid1.SelectedCells[0];
((TextBlock) cell.Column.GetCellContent(cell.Item)).Text = "";
Counting that the cells are textblocks.
foreach (DataGridCellInfo cellInfo in grid.SelectedCells)
{
DataGridColumn column = cellInfo.Column;
string propertyName = ((Binding)column.ClipboardContentBinding).Path.Path;
PropertyInfo pi = cellInfo.Item.GetType().GetProperty(propertyName);
if (pi != null)
{
pi.SetValue(cellInfo.Item, null, null);
}
}
ICollectionView cv = CollectionViewSource.GetDefaultView(grid.Items);
cv.Refresh();
I want to change the row selection from one row to next row and the focus from one cell to the another cell on the the same column.
that is, 2nd row is selected in my datagrid and the focus is currently on the 2nd row 3rd column.
when i click a button on my screen, 3rd must get selected and the focus must be on the 3rd row 3rd column.
now if i type something then it must automatically reflect on the 3rd row 3rd column.
Thanks in advance!
Try this little helper method for selecting a cell based on a column and a row item
private static void SelectCell(DataGrid dataGrid, DataGridColumn column, Object rowItem) {
if (rowItem != null) {
//scroll the item into view
dataGrid.ScrollIntoView(rowItem);
dataGrid.ScrollIntoView(rowItem, column);
//get the cell info
DataGridCellInfo cellInfo = new DataGridCellInfo(rowItem, column);
if (dataGrid.CurrentCell.Item == cellInfo.Item && dataGrid.CurrentCell.Column == cellInfo.Column) { }
else {
dataGrid.Focus();
dataGrid.CurrentCell = cellInfo;
//set the cell to be selected
dataGrid.SelectedCells.Clear();
dataGrid.SelectedCells.Add(cellInfo);
}
}
}
The following code will solve the issue.
_dgTemp.CommitEdit();
object SelectedItem = _dgTemp.SelectedItem;
DataGridRow _dgRow = DataGridHelper.GetRow(_dgTemp, _dgTemp.Items.IndexOf(SelectedItem));
DataGridCell _dgCell = DataGridHelper.GetCell(_dgTemp, _dgRow, _dgTemp.Columns.IndexOf(_dgTemp.CurrentColumn));
_dgCell.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
_dgTemp.ScrollIntoView(_dgTemp.Items.IndexOf(_dgTemp.CurrentItem));
_dgTemp.UpdateLayout();
_dgTemp.SelectedIndex = _dgTemp.Items.IndexOf(_dgTemp.CurrentItem);