我的问题是,当我创建一个单击新拍卖或删除拍卖时刷新数据网格的按钮时,什么也没有发生,甚至出现一些语法错误。所以我需要:
刷新按钮,该按钮是我通过XAML
创建的。
因此,当单击删除按钮时,我也需要删除一行。
但是,我的刷新无效。我需要从应用程序注销并再次登录,以进行一些更改。我真的不知道该怎么做。
我将在下面提供一些图片以及一些代码。
默认登录:
添加销售或删除销售,然后注销并重新登录:
我尝试制作一个可以自动更新的数据网格。我尝试制作此按钮,遇到一些语法错误,也尝试添加dgGridView.Items.Refresh()
,但也无济于事。
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
BindDataGrid();
}
private void BindDataGrid()
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnString"].ToString();
conn.Open();
SqlCommand comm = new SqlCommand("SELECT * FROM AuctionTbl2", conn);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(comm);
da.Fill(ds);
dgGrid.ItemsSource = ds.Tables[0].DefaultView;
dgGrid.Items.Refresh();
}
private void LogoutBtn_Click(object sender, RoutedEventArgs e)
{
MainWindowGuest mwg = new MainWindowGuest();
mwg.Show();
this.Close();
MessageBox.Show("You have been logged out.");
}
private void DeleteBtn_Click(object sender, RoutedEventArgs e)
{
try
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString =
ConfigurationManager.ConnectionStrings["ConnString"].ToString();
conn.Open();
string query = "delete from AuctionTbl2";
SqlCommand comm = new SqlCommand(query, conn);
comm.ExecuteNonQuery();
MessageBox.Show("Deleted");
conn.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void AddNew_Click(object sender, RoutedEventArgs e)
{
NewDeleteWindow newDeleteWindow = new NewDeleteWindow();
newDeleteWindow.DataContext = new NewSaveButton();
newDeleteWindow.ShowDialog();
}
}
}
我希望在添加或删除销售,使用刷新按钮或没有刷新时进行刷新。只是为了刷新此网格。谢谢大家。
答案 0 :(得分:1)
您必须更新table
,就像在加载form
时更新t一样。在代码中替换以下方法:
private void DeleteBtn_Click(object sender, RoutedEventArgs e)
{
try
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString =
ConfigurationManager.ConnectionStrings["ConnString"].ToString();
conn.Open();
string query = "delete from AuctionTbl2"; // i think you are not aaully deleting the record you want to delete
SqlCommand comm = new SqlCommand(query, conn);
comm.ExecuteNonQuery();
MessageBox.Show("Deleted");
conn.Close();
BindDataGrid();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void AddNew_Click(object sender, RoutedEventArgs e)
{
NewDeleteWindow newDeleteWindow = new NewDeleteWindow();
newDeleteWindow.DataContext = new NewSaveButton();
newDeleteWindow.ShowDialog();
BindDataGrid();
}
更新:要删除您单击的sale
,请按照以下步骤操作:
global variable
“ int IDdelete”。Designer.cs
。dataGridView
。Properties
。events
。CellClick
,然后双击它。private void dgGrid_CellClick(object sender, DataGridViewCellEventArgs e)
private void dgGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
IDdelete = Convert.InTo32(dgGrid.Rows[e.RowIndex].Cells[0].Value);
}
Now, in your `SQL query` use this query.
"DELETE from AuctionTbl2 where id=@IDelete";
答案 1 :(得分:0)
在Add
和delete
块之后尝试。
DataTable dt = dal.select();
dgGrid.DataSource = dt;
如果无法解决,请通过 skype vipinweb2