删除数据WPF C#后如何刷新DataGrid [英] How To Refresh DataGrid After Deleting the Data WPF C#

查看:72
本文介绍了删除数据WPF C#后如何刷新DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在删除数据后刷新DataGrid,这是我的代码

Hi, I want to refresh DataGrid after Deleting the data,  here is my code

try

                                              {

            {

                         SqlConnection con =新的SqlConnection(ConfigurationManager.ConnectionStrings ["DataConnectionString"].ConnectionString);

                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DataConnectionString"].ConnectionString);

    SqlCommand scmd = new SqlCommand("SELECT Name,Lastname FROM MyTable",con);

                SqlCommand scmd = new SqlCommand("SELECT Name,Lastname FROM MyTable", con);

               SqlDataAdapter sda =新的SqlDataAdapter(scmd);

                SqlDataAdapter sda = new SqlDataAdapter(scmd);

         &b DataTable dt = new DataTable("MyTable");

                DataTable dt = new DataTable("MyTable");

             con.Open();

                con.Open();

                       字符串str =((DataRowView)MyDataGrid.SelectedItem).Row ["Name"].ToString();

                string str = ((DataRowView)MyDataGrid.SelectedItem).Row["Name"].ToString();

           ;    字符串q =删除"来自MyTable,其中Name ='" + str +``''';

                string q = "delete  from MyTable where Name='" + str + "'";

              scmd.CommandText = q;

                scmd.CommandText = q;

                        scmd.ExecuteNonQuery();

                scmd.ExecuteNonQuery();

                        sda.Update(dt);

                sda.Update(dt);

                        MyDataGrid.ItemsSource = null;

                MyDataGrid.ItemsSource = null;

                        MyDataGrid.Items.Refresh();

                MyDataGrid.Items.Refresh();

             MyDataGrid.ItemsSource = dt.DefaultView;

                MyDataGrid.ItemsSource = dt.DefaultView;

             con.Close();

                con.Close();

               MessageBox.Show(已成功删除MyDataGrid所选行");

                MessageBox.Show("MyDataGrid selected row is Deleted Successfully");

}

           catch(特例例外)

            catch(Exception ex)

           {

            {

                         MessageBox.Show(您不能删除空行" +例如消息);

                MessageBox.Show("You can't Delete empty row" + ex.Message);

            }

            }

推荐答案


wpfdevstech,您好!


Hi wpfdevstech,

根据您的代码,您的代码首先执行select查询,然后然后执行删除查询.我认为您应该先执行删除查询,然后执行选择查询并重新绑定DataGrid.

According to your code, Your code first execute select query and  then execute delete query. I think you should first execute  delete query then execute select query and rebinding the DataGrid.

我做了一个演示供您参考.希望对您有所帮助.

I made a demo for your reference. Hope it is helpful.

public partial class DataGridWindow : Window
    {
        public static string connectionString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Person;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
        SqlConnection con;
        SqlCommand scmd;
        SqlDataAdapter sda;
        DataTable dt;
        public DataGridWindow()
        {
            InitializeComponent();
            bindingDG();
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            delete();
        }

        public void delete()
        {
            try
            {
                using (con = new SqlConnection(connectionString))
                {
                    string str = ((DataRowView)MyDataGrid.SelectedItem).Row["Name"].ToString();
                    string q = "delete from MyTable where Name='" + str + "'";
                    con.Open();
                    using (scmd = new SqlCommand(q, con))
                    {
                        using(sda = new SqlDataAdapter(scmd))
                        {
                           if(scmd.ExecuteNonQuery() == 1)
                            {
                                MyDataGrid.ItemsSource = null;
                                bindingDG();
                                MessageBox.Show("MyDataGrid selected row is Deleted Successfully");
                            }
                               
                        }
                    }
                    
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("You can't Delete empty row" + ex.Message);
            }
        }
        public void bindingDG()
        {
            using (con = new SqlConnection(connectionString))
            {
                con.Open();
                using (scmd = new SqlCommand("SELECT Name,LastName FROM MyTable", con))
                {
                  dt = new DataTable("MyTable");
                    using (sda = new SqlDataAdapter(scmd))
                    {
                        sda.Fill(dt);
                        MyDataGrid.ItemsSource = dt.DefaultView;
                    }
                }
            }
        }
}



最好的问候,

吕汉楠


这篇关于删除数据WPF C#后如何刷新DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆