Every body talks about this decision.
Int32 columnIndex = dataGridScannedFiles.SelectedCells[0].Column.DisplayIndex;
and yes, it works, but no one says that we should set the Display index first for each column, maybe for experts it is clear, but for beginners this is an unfamiliar thing
There are two ways to install it: -
1) You can install it in the XAML part.
<DataGridTextColumn Header="Serial No." Width="60" IsReadOnly="True" Binding="{Binding Path=Sno}" DisplayIndex="1"></DataGridTextColumn>
I don't know how to set it for custom columns e.g.
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="ChkItem" IsChecked="{Binding Path=Sno}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
so I preferred a different way
2) Created Function
private void SetDisplayIndexforGridViewColumns()
{
Int32 ColumnCount = dt.Columns.Count;
for (int i = 0; i < ColumnCount; i++)
{
dataGridScannedFiles.Columns[i].DisplayIndex = i;
}
}
dt is a data table
and I assign him index indices
Now if you use
Int32 columnIndex = dataGridScannedFiles.SelectedCells[0].Column.DisplayIndex;
then you will definitely get an index
source
share