简体   繁体   English

如何在WPF C#中将Form1的文本框内容解析为Form2的DataGrid

[英]How to solve form1's Textbox contents to form2's datagrid in wpf c#

  1. Here the save button in another form named Addwindow 在这里,保存按钮以另一种名为Addwindow的形式出现
  2. The Datagrid in another form named Machine_List 另一种名为Machine_List的形式的Datagrid
  3. I need the solution for,when i click on save button,the textbox values must stored in datagrid at the same time. 我需要解决方案,当我单击“保存”按钮时,文本框值必须同时存储在datagrid中。

Here the below code for WPF C# am using but i cant get data. 在这里下面的WPF C#代码正在使用,但我无法获取数据。

private void SaveButton_Click(object sender, RoutedEventArgs e)//Save Button
{
    Machine_List m = new Machine_List();//the datagrid present here Another form named Machine_List
    if (this.Edit)
        this.db.Datastore("UPDATE [databasename].[dbo].[Machinedup] SET [Name] = '" + textBox1.Text   + "',[Type] = '" + comboBox1.SelectedItem + "',[AETitle] = '" + this.textBox2.Text + "',[IPAddress] = '" + textBox3.Text + "',[Port]='" + textBox4.Text + "' WHERE [ID] = '" + (string)(object)this.ID + "'");
    else
        this.db.Datastore("INSERT INTO [databasename].[dbo].[Machinedup] ([Name],[Type],[AETitle],[IPAddress],[Port]) VALUES('" + textBox1.Text + "','" + comboBox1.SelectedIndex.ToString() + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')");
    this.Close();
    DataTable dataTable = db.DataTab("SELECT [Name],[Type] AS Type,[AETitle] AS AET,[IPAddress] AS IP,[Port] AS PO FROM [databasename].[dbo].[Machinedup] WHERE ID ='" + (object)this.ID + "'");
    DataRow row = dt.NewRow();
    row[0] = textBox1.Text;
    row[1] = textBox2.Text;
    dt.Rows.Add(row);
    m.dataGrid1.ItemsSource = dt.DefaultView;
}

if you're using different form/window. 如果您使用的是其他表单/窗口。 In your save button, all you have to do is to save your data. 在保存按钮中,您要做的就是保存数据。

private void SaveButton_Click(object sender, RoutedEventArgs e)//Save Button
{


    this.db.Datastore("UPDATE [databasename].[dbo].[Machinedup] SET [Name] = '" + textBox1.Text   + "',[Type] = '" + comboBox1.SelectedItem + "',[AETitle] = '" + this.textBox2.Text + "',[IPAddress] = '" + textBox3.Text + "',[Port]='" + textBox4.Text + "' WHERE [ID] = '" + (string)(object)this.ID + "'");
else
    this.db.Datastore("INSERT INTO [databasename].[dbo].[Machinedup] ([Name],[Type],[AETitle],[IPAddress],[Port]) VALUES('" + textBox1.Text + "','" + comboBox1.SelectedIndex.ToString() + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')");
this.Close();

}

in the form where your datagrid is located, create a method that will refresh your datagrid 在您的数据网格所位于的表单中,创建一个将刷新您的数据网格的方法

 public void refreshgrid()
{


 cmd = new SqlCommand("SELECT [Name],[Type] AS Type,[AETitle] AS AET,    [IPAddress] AS IP,[Port] AS PO FROM [databasename].[dbo].[Machinedup] WHERE ID ='" + (object)this.ID + "'",conn); 
        da = new SqlDataAdapter(cmd);

        dt = new DataTable("Machineup");
        da.Fill(dt);
        dataGrid1.ItemsSource = dt.DefaultView;

}

and in the window_loaded call the method. 并在window_loaded中调用该方法。

  private void Window_Loaded(object sender, RoutedEventArgs e)
 {
   refreshgrid();
 }  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM