This is my first project, so be gentle
I know how to get the current row Index value and its [ID] value using the following code:
public void Sql_Address_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataGrid gd = (DataGrid)sender;
if (gd.SelectedItem is DataRowView row_selected)
{
Public_Strings.selectedID = Int32.Parse(row_selected["ID"].ToString());
Public_Strings.currentIndex = Int32.Parse(sql_address.SelectedIndex.ToString());
}
}
I am displaying synchronized SQL table values in two different places - a DataGrid in TabItem 1 and a couple of editable TextBoxes in TabItem 2. I also have Next/Previous buttons in TabItem 2 to move up and down the DataGrid Index that also refresh the content of the TextBoxes. Everything works fine, but when I add or modify an entry in the SQL table, the Index shifts, because of the grouping and the Next/Previous buttons reset to the default Index 0.
I know how to bypass this when Deleting an entry by using this method:
public void Delete(object sender, RoutedEventArgs e)
{
MessageBoxResult messageBoxResault = System.Windows.MessageBox.Show("Ali se prepričani?", "Potrditev izbrisa", System.Windows.MessageBoxButton.YesNo);
if (messageBoxResault == MessageBoxResult.Yes)
{
Public_Strings.currentIndex= sql_address.SelectedIndex-1;
SqlCommand cmd = new SqlCommand
{
CommandText = "DELETE FROM cbu_naslovi WHERE [ID]='" + Public_Strings.selectedID + "'",
Connection = con
};
cmd.ExecuteNonQuery();
Datagrid();
sql_address.SelectedIndex = Public_Strings.currentIndex;
}
}
My Add method:
public void Add(object sender, RoutedEventArgs e)
{
MessageBoxResult messageBoxResault = System.Windows.MessageBox.Show("Ali se prepričani?", "Potrditev vnosa", System.Windows.MessageBoxButton.YesNo);
if (messageBoxResault == MessageBoxResult.Yes)
{
SqlCommand cmd = new SqlCommand
{
CommandText = "INSERT INTO cbu_naslovi VALUES ('" + ulica.Text + "','" + hisna_st.Text + "','" + id_hise.Text + "','" + postna_st.Text + "','" + obmocje.Text + "','" + katastrska_obcina.Text + "','" + st_objekta.Text + "','" + st_delov.Text + "','" + st_parcele_1.Text + "','" + st_parcele_2.Text + "','" + st_parcele_3.Text + "','" + st_parcele_4.Text + "','" + st_parcele_5.Text + "','" + st_parcele_6.Text + "','" + st_parcele_7.Text + "')",
Connection = con
};
cmd.ExecuteNonQuery();
Datagrid();
address.Content = ulica.Text.ToString() + " " + hisna_st.Text.ToString() + id_hise.Text.ToString();
}
}
I need a solution that allows me to select the index based on the ID value of row, so that when I add or modify and entry in the SQL table the Next/Previous buttons continue from the newly added/modified index. Basically something in the lines of:
sql_address.SelectedIndex = "sql_address.SelectedIndex where sql_address[ID] = Public_Strings.currentIndex" - Paraphrasing
Visual refference: