0

My problem is, when I create a button that refreshes data grid when the new auction or delete auction is clicked, nothing happens at all, or even some syntax error shows up. So I need:

Refresh button, which I made through XAML. So when the delete button is clicked I need a row deleted, that I made too. But, my refresh doesn't work. I need to log out from the application and log in again, to make some changes. I don't really know how to do this.

I'll provide some pictures down below, and also some code.

Default log in: 1

Adding a sale or deleting a sale, then logging out and logging back in: 2

I tried making a data grid that auto-updates. I tried making this button, I got a few syntax errors, I also tried adding dgGridView.Items.Refresh(), didn't help either.

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();

        }
    }

}

I expect to make a refreshment when the sale is added or deleted, on a refresh button or without it, doesn't matter. Just to refresh this grid. Thank you all.

slava
  • 791
  • 1
  • 11
  • 26
xVenum.dll
  • 73
  • 5
  • https://stackoverflow.com/questions/21299016/how-to-refresh-or-show-immediately-in-datagridview-after-inserting – Dortimer Apr 09 '19 at 15:40
  • what are you deleting ? "delete from tblAuction" it'll not delete a record. – Zain Arshad Apr 09 '19 at 15:41
  • Where is the code for Refresh button? – Zain Arshad Apr 09 '19 at 15:42
  • @ZainArshad I'm a beginner, I don't know so much. I'm trying to experiment this. It deletes it in some way. When I press the "delete button", it somehow deletes it from a database I don't know what should I get? Btw, I removed Refresh button, I thought it couldn't help me anyways. – xVenum.dll Apr 09 '19 at 15:44
  • Refresh the data, datasource or query not the control. But you are missing rags indicating the technology used to be much help. – Ňɏssa Pøngjǣrdenlarp Apr 09 '19 at 15:47
  • Run my code, it works? – Zain Arshad Apr 09 '19 at 15:49
  • If you dont stick to MVVM or a databinding, you would need to do manually the grid refreshing when its data changes. Call you refresh code after a CRUD operation – Cleptus Apr 09 '19 at 15:54

2 Answers2

1

You have to update the table, just like you update t when you load the form. Replace these methods in your code:

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();
}

UPDATE: For deleting the sale that you have clicked on follow these steps:

  • make a global variable "int IDdelete".
  • Go to the Designer.cs.
  • Click on the dataGridView.
  • You will see Properties on right side.
  • click on events.
  • Scroll and find CellClick and double-click on it.
  • It will make a method named like this private void dgGrid_CellClick(object sender, DataGridViewCellEventArgs e)
  • Now paste the paste the code below in that method.
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";
Zain Arshad
  • 1,885
  • 1
  • 11
  • 26
  • Yes man, this works. For adding at least. When I select one row and delete it, it deletes all rows? – xVenum.dll Apr 09 '19 at 15:50
  • What it delete all records ? Thats what i have mention in comment in the code – Zain Arshad Apr 09 '19 at 15:50
  • you have to make some changes get the `Id` of the selected row then use a `Where` clause – Zain Arshad Apr 09 '19 at 15:52
  • I included where clause for id like this: "delete from AuctionTbl2 where id=@Id". It says that I need to declare a scalar variable? Sorry guys, I really don't know, I appreciate the time you take for explaining me. – xVenum.dll Apr 09 '19 at 16:00
  • See you have to make a variable `IDdelete` and make a `event` that when you click on a record then the `ID` of the record get saved in the `IDdelete` variable and the give that `IDdelete` to the `where clause. You understood ? – Zain Arshad Apr 09 '19 at 16:03
  • @ZainArshad You mean like I need private field for id, property and constructor? One more question. Is it better to start a new project or to try to fix this one? – xVenum.dll Apr 09 '19 at 16:09
  • Wait I am updating my answer follow those instructions and you'll get what you want, If it helps you then do mark it as "answer" and "upvote" it .. wait a moment :) – Zain Arshad Apr 09 '19 at 16:11
  • @ZainArshad I see only CellEditEnding? I'm using WPF application. – xVenum.dll Apr 09 '19 at 16:30
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/191550/discussion-between-xvenum-dll-and-zain-arshad). – xVenum.dll Apr 09 '19 at 16:32
0

Try this after Add and delete blocks.

DataTable dt = dal.select();
dgGrid.DataSource = dt;

If not resolve, ping me on skype: vipinweb2

Pavel Smirnov
  • 4,611
  • 3
  • 18
  • 28