使用文本框的值绑定Datagrid

问题描述 投票:-1回答:1

我有一个WPF应用程序。我试图通过使用TextBox中显示的值查询我的SQL数据库来绑定数据网格。目前,TextBox正在显示上一个窗口中的选定项目。

我希望从我的付款表中显示日期和金额,其中名称等于我的TextBox中显示的选定项目。

     private void BinddatagridPaid()
     {
         SqlConnection sqlCon2 =
             new SqlConnection(@"Data Source = ODHRAN\SQLEXPRESS; Initial 
         Catalog = FitnessWorks; Integrated Security = True")
         sqlCon2.Open();
         SqlCommand cmd2 = new SqlCommand();
         string Value = textBox1.Text;

         cmd2.CommandText = "SELECT Payments.DatePaid, Payments.Amount FROM 
         Payments Where Payments.Name=@Value";
         cmd2.Parameters.AddWithValue("@Value", Value);

         cmd2.Connection = sqlCon2;
         SqlDataAdapter da = new SqlDataAdapter(cmd2);
         DataTable dt2 = new DataTable("AmountPaid");

         da.Fill(dt2);

         DataGridPaid.ItemsSource = dt2.DefaultView;
     }   

这是我填充文本框的代码:

       private void DataGrid2_OnMouseDoubleClick(object sender, 
       MouseButtonEventArgs e)
        {
          MemberSesh ms = new MemberSesh();
          DataGrid gd1 = (DataGrid)sender;
          DataRowView row_selected = gd1.SelectedItem as DataRowView;
          if (row_selected != null)
           {
            ms.textBox1.Text = row_selected["Name"].ToString();
            ms.textBox2.Text = row_selected["Cost"].ToString();
           }
          ms.ShowDialog();
        }

我无法使用TextBox中的值来查询数据库并填充数据网格。

c# wpf data-binding datagrid
1个回答
0
投票

您需要访问Parent Form textBox1实例以在MemberSesh表单中设置text属性。

您可以使用新表单的this.Parent属性访问父表单。您首先需要施放然后使用textBox1成员。

© www.soinside.com 2019 - 2024. All rights reserved.