How do you bind the data of the database table to the DataGrid? If I am not misunderstood, you want to bind the selected index to the TextBlock. I use the following code to display the selected index in the TextBlock. And getter can get the changed value. Could you try to use the following code in your project. If I misunderstood, please let me know.
The code of MainWindow. Xaml:
<StackPanel >
<DataGrid Name="dataGrid" SelectedCellsChanged="dataGrid_SelectedCellsChanged" AutoGenerateColumns="False" Height="380">
<DataGrid.Columns>
<DataGridTextColumn Header="CustomerID" Width="80" Binding="{Binding CustomerID}"></DataGridTextColumn>
<DataGridTextColumn Header="CompanyName" Width="100" Binding="{Binding CompanyName}"></DataGridTextColumn>
<DataGridTextColumn Header="ContactName" Width="90" Binding="{Binding ContactName}"></DataGridTextColumn>
<DataGridTextColumn Header="ContactTitle" Width="80" Binding="{Binding ContactTitle}"></DataGridTextColumn>
<DataGridTextColumn Header="Address" Width="100" Binding="{Binding Address}"></DataGridTextColumn>
<DataGridTextColumn Header="City" Width="50" Binding="{Binding City}"></DataGridTextColumn>
<DataGridTextColumn Header="Region" Width="50" Binding="{Binding Region}"></DataGridTextColumn>
<DataGridTextColumn Header="PostalCode" Width="80" Binding="{Binding PostalCode}"></DataGridTextColumn>
<DataGridTextColumn Header="Country" Width="100" Binding="{Binding Country}"></DataGridTextColumn>
<DataGridTextColumn Header="Phone" Width="80" Binding="{Binding Phone}"></DataGridTextColumn>
<DataGridTextColumn Header="Fax" Width="80" Binding="{Binding Fax}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<TextBlock Width="1000" Height="50" Name="tb" Text="{Binding Source=CustomerCurrent}"/>
<TextBlock Width="1000" Height="100" Name="myTxt" TextWrapping="Wrap" Background="AliceBlue" HorizontalAlignment="Left"></TextBlock>
</StackPanel>
The code of MainWindow. Xaml.cs:
public partial class MainWindow : Window,INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
FillGrid();
}
private void FillGrid()
{
string constr = @"constr ";
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand cmd = new SqlCommand("Select * From [dbo].[Customers]", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
var dt = new DataTable();
da.Fill(dt);
dataGrid.ItemsSource = dt.DefaultView;
con.Close();
}
private int customerCurrent;
public int CustomerCurrent
{
get
{
return customerCurrent;
}
set
{
customerCurrent = value;
OnPropertyChange("CustomerCurrent");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChange(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
private void dataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
int select= dataGrid.SelectedIndex;
customerCurrent = select;
tb.Text = customerCurrent.ToString();
}
}
The result picture:
Update:
Your CustomerCurrent is actually binding the SelectedIndex of the DataGrid to the Textblock. You could bind SelectedIndex directly in xaml. The code here is used to bind TextBlock directly to SelectedIndex.
The code of MainWindow. Xaml:
<StackPanel >
<DataGrid Name="dataGrid" AutoGenerateColumns="False" Height="380">
<DataGrid.Columns>
<DataGridTextColumn Header="CustomerID" Width="80" Binding="{Binding CustomerID}"></DataGridTextColumn>
<DataGridTextColumn Header="CompanyName" Width="100" Binding="{Binding CompanyName}"></DataGridTextColumn>
<DataGridTextColumn Header="ContactName" Width="90" Binding="{Binding ContactName}"></DataGridTextColumn>
<DataGridTextColumn Header="ContactTitle" Width="80" Binding="{Binding ContactTitle}"></DataGridTextColumn>
<DataGridTextColumn Header="Address" Width="100" Binding="{Binding Address}"></DataGridTextColumn>
<DataGridTextColumn Header="City" Width="50" Binding="{Binding City}"></DataGridTextColumn>
<DataGridTextColumn Header="Region" Width="50" Binding="{Binding Region}"></DataGridTextColumn>
<DataGridTextColumn Header="PostalCode" Width="80" Binding="{Binding PostalCode}"></DataGridTextColumn>
<DataGridTextColumn Header="Country" Width="100" Binding="{Binding Country}"></DataGridTextColumn>
<DataGridTextColumn Header="Phone" Width="80" Binding="{Binding Phone}"></DataGridTextColumn>
<DataGridTextColumn Header="Fax" Width="80" Binding="{Binding Fax}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<TextBlock Width="1000" Height="50" Name="tb" Background="AliceBlue" HorizontalAlignment="Left" Text="{Binding SelectedIndex, ElementName=dataGrid}" />
</StackPanel>
The code of MainWindow. Xaml.cs:
public partial class MainWindow : Window,INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
FillGrid();
}
private void FillGrid()
{
string constr = @"constr ";
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand cmd = new SqlCommand("Select * From [dbo].[Customers]", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
var dt = new DataTable();
da.Fill(dt);
dataGrid.ItemsSource = dt.DefaultView;
List<int> SelectedIndexs = dataGrid.SelectedItems
.Cast<DataRowView>()
.Select(view => dt.Rows.IndexOf(view.Row))
.ToList();
con.Close();
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChange(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
}
The result picture: