c#


c# wpf datagrid row index


I have tried several methods of returning a current row index value but all suggestions so far are not accepted as valid code in my environment. I would like to get an index value of the row in focus or through the row button I have inserted. Here is my test code -
xaml
<DataGrid x:Name="dataGrid1" Loaded="WhenLoaded" AutoGenerateColumns="False" Margin="0,0,478,274" SelectionMode="Single" SelectionChanged="dataGrid1_SelectionChanged" >
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Name="Select" Click="Row_Click" Content="Select" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="node" Binding="{Binding Path=NODE}" />
<DataGridTextColumn Header="name" Binding="{Binding Path=NAME}" />
<DataGridTextColumn Header="s/n" Binding="{Binding Path=SERIAL_NO}" />
</DataGrid.Columns>
C#
myDataSet.Tables.Add(myTable);
myDataSet.Tables.Add(myTable2);
myTable.Columns.Add("NODE", typeof(string));
myTable.Columns.Add("NAME", typeof(string));
myTable.Columns.Add("SERIAL_NO", typeof(string));
myTable.Rows.Add(new string[] { "99", "Pressure", "1234" });
dataGrid1.ItemsSource = myTable.DefaultView;
I have tried using this method to access the indexes but RowIndex and ColumnIndex are not recognised -
private void Row_Click(object sender, RoutedEventArgs e)
{
int row = dataGrid1.CurrentCell.RowIndex;
int col = dataGrid1.CurrentCell.ColumnIndex;
}
private void Row_Click(object sender, RoutedEventArgs e)
{
DataRowView dataRow = (DataRowView)dataGrid1.SelectedItem;
string cellValue_Method1 = dataRow.Row.ItemArray[3].ToString();
string cellValue_Method2 = dataRow[0].ToString();
int index = dataGrid1.CurrentCell.Column.DisplayIndex;
MessageBox.Show("index "+index + " cell value frist from method: "+cellValue_Method1 + " cell value from second method: "+cellValue_Method2);
}
Use SelectedIndex property:
private void Row_Click(object sender, RoutedEventArgs e)
{
var selectedRowIndex = dataGrid1.SelectedIndex
}
Alternatively, you can get row index from the current cell (a bit redundant though):
var row = (DataGridRow)dataGrid1.ItemContainerGenerator
.ContainerFromItem(dataGrid1.CurrentCell.Item);
Console.WriteLine(row.GetIndex());
There is no column index on wpf DataGridCell, but you may get DisplayIndex instead (beware it may change when columns get rearranged)
Console.WriteLine(dataGrid1.CurrentCell.Column.DisplayIndex);
If you want to know which column/property in the data you're accessing, you will need to find it out based on something else than DisplayIndex (for example on CurrentCell.Column.Header)
If you want to get indexes without placing a button in your column, you can use for example DataGrid.MouseUp or DataGrid.MouseLeftButtonUp events.
You can find a more in-depth explanation here.

Related Links

Attendance application useing Qrcode c# winform
How do I use string interpolation with string literals?
Interface implementation and generics compile error
Unit test an internal class in ASP.NET Core
“SQL Server doesn't exist or access denied” while importing Excel sheet into SQL Server using C#
Problems Distributing Visual Studio Application
Neo4j- how to use WITH clause using c# client cypher
C# How can I clear the memory usage?
How many async concurrent requests can ASP.NET handle? [closed]
OData controller doesn't return derived object
ToolStripMenuItem: Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on
How to make right click Button Context menu in WPF?
ASP LinkButton Does not work
Trying to Add API Item. Error reading string. Unexpected token: StartObject
Asynchronous Programming with c# - async , await and Task.Run
OpenXml - Find the index of the first Run element in a paragraph

Categories

HOME
max
autohotkey
ibm
dynamic
boxplot
jquery-plugins
app-store
google-docs-api
ebay
schema
meson-build
cdn
digital
adroitlogic
tracking
mbed
tag-cloud
civicrm
spring-webflow
apdu
pixels
javapns
jmsserializerbundle
slim-lang
voyager
bootcamp
mootools
sampling
device-driver
scip
anypoint-studio
libzip
smooth-streaming
line-endings
serverless-architecture
rivets.js
multivalue
overhead
node.js-client
group
gevent
firebase-job-dispatcher
strtol
syslog4j
falcon
php-java-bridge
django-celery
nd4j
paypal-subscriptions
sqlxml
sqldataadapter
videoview
wonderware
magnet-uri
windows-phone-8-emulator
sparkcore
web-standards
android-networking
nfa
bungeecord
beagleboard
nexusdb
method-overloading
javadb
kss
jsonschema2pojo
system.io.packaging
windows-search
jsmpp
sqldatasource
android-jack-and-jill
.net-reflector
vbaccelerator
netbeans-plugins
facebook-likebox
ie-developer-tools
ojb
pudb
gmail-imap
pymel
linqdatasource
hypertable
nserror
joomla1.7
code-organization
moss2007-security
meego-harmattan
rollover
oaw
file-storage
bootstrap-protocol

Resources

Encrypt Message