Wrong index after re-ordering datagrid rows - wpf

I have a DataTable bound to a DataGridView. When I re-order the rows, the column index of the string I'm looking for gets messed up.
I get the index of the Serial Num column... this works, even after I've moved it.
Dim snIndex As Integer = asset_MasterDataGrid.Columns.[Single](Function(c) c.Header.ToString() = "Serial Num").DisplayIndex
Then I direct cast the row i'm clicking
Dim item As DataRowView = DirectCast(asset_MasterDataGrid.SelectedItem, DataRowView)
Then I grab the string of the cell in the Serial Num column.
Dim sn As String = item.Row(snIndex)
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/f4EqC.png
This of course doesn't work, and when I re-order my serial number column to lets say... index 0, it grabs the asset ID cell string. I'm trying to find the Serial number of whatever row I click on, no matter where the serial number column is.
Any ideas on how to correct this issue?

You can access a column of a DataRowView by its name rather than the index:
Dim item As DataRowView = DirectCast(asset_MasterDataGrid.SelectedItem, DataRowView)
Dim sn As String = item("Serial Num")
This should work provided that you know the name of the serial number column.

If you are clicking a datagrid header to reorder your selection, the linked data will not be reordered, the index will still be the same, the selected item will be what you select, but its index in the DataTable has not changed
to seach column index try this (C#)
var index = 0;
for (var i = 0, i < asset_MasterDataGrid.Columns.Count; i++)
if (((DataColumn)asset_MasterDataGrid).ColumnName == "Serial Num")
{
index = i;
break;
}
obvious this is your column with your serial.
Now to get your value with this serial number you need to seach in your DataGrid for it (not in your dataTable)

Related

getdatasourcerowindex nullreference after filter using gridview (Devexpress Winform)?

I'm using gridview devexpress. After i use filter on that component, there is error nullreference. If i don't using filter. There isn't error on my code.
for (int i = 0; i < GridView1.RowCount; i++)
{
DataRow dr;
dr = GridView1.GetDataRow(GridView1.GetDataSourceRowIndex(i));
MessageBox.Show(dr[0].ToString());
}
That is my code. Any solution to get datarow value from gridview after filter?
Maybe the problem is that you are using RowCount. The DataSourceRowIndex is based on the index your underlying datasource holds. Thats not the same. If you are grouping or sorting the rowhandle of gridview is changing. And the RowCount only shows you how much rows are in the GridView. But this must not be equivalent to your datasourceindex.
try the following to get your datarow:
1.
foreach (DataRow row in ((DataTable)GridControl1.DataSource).Rows)
{
//Here you can access your row via row variable
}
You have to know that the RowCount Property cant be used for accessing rows safely!
If you need further help dont hesitate for asking.
You must use BaseView.DataRowCount property instead of BaseView.RowCount property:
for (int i = 0; i < GridView1.DataRowCount; i++)
{
DataRow dr;
dr = GridView1.GetDataRow(GridView1.GetDataSourceRowIndex(i));
MessageBox.Show(dr[i].ToString());
}

Grid layout with visible row numbers and column nimbers

How do i display the row number and column number for a grid layout in silverlight?I do not find any property in the grid which does it for me.
From this SO:
One way is to add them in the LoadingRow event for the DataGrid
<DataGrid Name="DataGrid" LoadingRow="DataGrid_LoadingRow" ...
void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Header = (e.Row.GetIndex()).ToString();
}
Or another approach using multibinding and a converter is detailed here.
This code will create a grid with 20 columns and 20 rows...
Dim newGrid As New Grid 'Or use existing grid
For x = 1 To 20
'Create row numbers
Dim rowNum As New TextBlock With {.Text = x}
Grid.SetColumn(rowNum, 0)
Grid.SetRow(rowNum, x)
newGrid.Children.Add(rowNum)
'Create column numbers
Dim colNum As New TextBlock With {.Text = x}
Grid.SetColumn(colNum, x)
Grid.SetRow(colNum, 0)
newGrid.Children.Add(colNum)
Next
'Create 20 columns and 20 rows (plus one each for numbers)
For x = 0 To 20
newGrid.RowDefinitions.Add(New RowDefinition With {.Height = New GridLength(20)})
newGrid.ColumnDefinitions.Add(New ColumnDefinition With {.Width = New GridLength(20)})
Next
Of course you would have to play with things like alignments and maybe you want to put some borders in there or something so you can see the rows and columns... numbers on the top and left are hard to line up without the lines there.
You can add row and column numbers to existing grids, you just have to make sure you don't have any of your actual content in column 0 or row 0, which you'll be using for the numbers.

How to get the selected column count in datagrid

I want selected column's index of the DataGrid. For example, if I select the first column, I want the first column's index (index = 0).
I tried it in the DataGrid SelectionChanged event, but I can't seem to get the particular column index. If any one knows how to do it, help me with some sample code.
The DataGrid.Items property returns a DataGridItemCollection representing the DataGridItems in the DataGrid.
Each DataGridItem is representative of a single row in the rendered table. Also, the DataGridItem exposes a Cells property which represents the no. of tablecells (in other words, the columns) in the rendered table.
// Get the Row Count
int rowCount = myGrid.Items.Count;
// Get the no. of columns in the first row.
int colCount = myGrid.Items[0].Cells.Count;
I am assuming that you want the indexes of any columns that are selected. Here is the code I've come up with:
List<int> selectedColumnIndexes = new List<int>(dataGrid.SelectedCells.Count);
for (int i = 0; i < dataGrid.SelectedCells.Count; i++)
{
foreach (DataGridColumn column in dataGrid.Columns)
{
if (column.DisplayIndex == dataGrid.SelectedCells[i].Column.DisplayIndex)
{
if (!selectedColumnIndexes.Contains(column.DisplayIndex))
{
selectedColumnIndexes.Add(column.DisplayIndex);
}
}
}
}
Thus you will have a list of all the indexes of the columns currently selected. This question gives some nice clues in what direction to head here.
Obviously, if you want just the number of columns actually selected, then that value is simply selectedColumnIndexes.Count after the for loops have run through.

enter key behaviour of syncfusion's Data bound grid doesn't work if any column is hidden

I am using Synfusion's Data Bound Grid.
I have set two properties as
this.mygrid.TableModel.Options.EnterKeyBehavior = GridDirectionType.Right;
this.mygrid.TableModel.Options.WrapCellBehavior = GridWrapCellBehavior.WrapGrid;
If no column is hidden this works fine. If i hide any column then it doesn't works.
Please help me to achieve enter key behaviour if columns are hidden.
Thanks in Advance
I have handled the following code for hiding columns :
GridColHidden[] hiddenCols = new GridColHidden[ 3];
for (int i = 0; i < 3; i++)
{
hiddenCols[i] = new GridColHidden(i + 1);
}
this.gridDataBoundGrid1.Model.ColHiddenEntries.AddRange(hiddenCols);
I have now added the following code.
this.mygrid.TableModel.Options.EnterKeyBehavior = GridDirectionType.Right;
this.mygrid.TableModel.Options.WrapCellBehavior = GridWrapCellBehavior.WrapGrid;
I was able to see the first cell is set as current cell , when Enter key is pressed with the current cell's position placed in last cell in Grid.

Delete selected cells content in WPF DataGrid

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();

Resources