这是我的第一个项目,请保持柔和
我知道如何使用以下代码获取当前行的索引值及其[ID]值:
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());
}
}
我在两个不同的位置显示同步的SQL表值-TabItem 1中的一个DataGrid和TabItem 2中的几个可编辑的TextBoxes。TabItem2中还具有“上一个/上一个”按钮来上下移动DataGrid索引,刷新TextBoxes的内容。一切正常,但是当我在SQL表中添加或修改条目时,索引会发生变化,这是因为分组的原因,并且“下一个/上一个”按钮重置为默认的索引0。
我知道在使用此方法删除条目时如何绕过此操作:
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;
}
}
我的添加方法:
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();
}
}
我需要一个解决方案,允许我根据行的ID值选择索引,以便在添加或修改SQL表中的条目时,“下一个/上一个”按钮从新添加/修改的索引继续。基本上是这样的:
sql_address.SelectedIndex = "sql_address.SelectedIndex where sql_address[ID] = Public_Strings.currentIndex" - Paraphrasing
视觉参考:
答案 0 :(得分:0)
我使用以下代码组合解决了此问题:
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();
SaveID();
sql_address.SelectedIndex = 0;
SearchIndex();
address.Content = ulica.Text.ToString() + " " + hisna_st.Text.ToString() + id_hise.Text.ToString();
search.Text = string.Empty;
}
}
public void SaveID()
{
DatagridIndex();
sql_address.SelectedIndex = sql_address.Items.Count - 1;
Public_Strings.saveID3 = Public_Strings.saveID1;
Datagrid();
}
public void SearchIndex()
{
if (Public_Strings.saveID3 == Public_Strings.saveID1) { }
else
{
sql_address.SelectedIndex++;
SearchIndex();
}
}
public void DatagridIndex()
{
SqlCommand cmd = new SqlCommand
{
CommandText = "SELECT * FROM [cbu_naslovi] ORDER BY [ID] ASC",
Connection = con
};
SqlDataAdapter da = new SqlDataAdapter(cmd);
dataGrid1 = new DataTable("cbu_naslovi");
da.Fill(dataGrid1);
sql_address.ItemsSource = dataGrid1.DefaultView;
}
public void Datagrid()
{
SqlCommand cmd = new SqlCommand
{
CommandText = "SELECT * FROM [cbu_naslovi] ORDER BY [ULICA] ASC, LEN ([HS]) ASC, [HS] ASC, [HID] ASC",
Connection = con
};
SqlDataAdapter da = new SqlDataAdapter(cmd);
dataGrid1 = new DataTable("cbu_naslovi");
da.Fill(dataGrid1);
sql_address.ItemsSource = dataGrid1.DefaultView;
}
TLDR版本,添加一个条目并创建一个按ID ASC排序的“新” Datagrid,只要您使用自动递增,列表中的最后一个ID始终将是新创建的。将该ID保存在新字符串中。现在,调用“正确的” Datagrid,以所需的方式对其进行排序,并将从索引0开始的ID与保存的ID进行比较,直到找到正确的ID,然后停止代码。现在,您的添加按钮应该将您重定向到正确的索引,下一个/上一个按钮应该可以正常工作。
Edit_v1: 可能有一个更简单的解决方案,但我太傻了找不到它(理论上可以用一种方法,每次按下“添加”按钮时,将+1加到计数器上,但要注意要从正确的数字开始导入CVS文件时的ID)。
Edit_v2:或者仅通过SQL查找最后一个ID: Safest way to get last record ID from a table 然后使用以下方法将其转换为字符串: How can I get SQL result into a STRING variable?
Edit_v3:另一种方法:
public void Add(object sender, RoutedEventArgs e)
{
if (ulica.Text != "" && hisna_st.Text != "" && postna_st.Text != "" && obmocje.Text != "")
{
MessageBoxResult messageBoxResault = MessageBoxEx.Show(this, "Ali se prepričani?", "Potrditev vnosa", MessageBoxButton.YesNo);
if (messageBoxResault == MessageBoxResult.Yes)
{
SqlCommand cmd = new SqlCommand
{
CommandText = "INSERT INTO cbu_naslovi VALUES ('" + ulica.Text + " " + hisna_st.Text + id_hise.Text + "','" + 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 + "','" + st_parcele_8.Text + "','" + st_parcele_9.Text + "','" + st_parcele_10.Text + "','" + st_parcele_11.Text + "','" + st_parcele_12.Text + "','" + st_parcele_13.Text + "','" + st_parcele_14.Text + "','" + st_parcele_15.Text + "','" + st_parcele_16.Text + "','" + st_parcele_17.Text + "'); SELECT SCOPE_IDENTITY();",
Connection = con
};
int lastId = Convert.ToInt32(cmd.ExecuteScalar());
InvokeDataGridAddress();
SetToRow(lastId);
address.Content = ulica.Text.ToString() + " " + hisna_st.Text.ToString() + id_hise.Text.ToString();
}
}
else
{
MessageBoxEx.Show(this, "Vpisati je potrebno podatke!");
}
}
public int CurrentID
{
get
{
int tmp = 0;
if (dg_address.SelectedIndex >= 0)
{
int.TryParse(dtAddress.Rows[dg_address.SelectedIndex].ItemArray[0].ToString(), out tmp);
}
return tmp;
}
}
public void SetToRow(int Id)
{
Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
dg_address.SelectionChanged -= DG_Address_SelectionChanged;
while (CurrentID != Id && dg_address.SelectedIndex < dtAddress.Rows.Count - 1)
{
dg_address.SelectedIndex++;
}
dg_address.SelectionChanged += DG_Address_SelectionChanged;
Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow;
}