简体   繁体   中英

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

  1. Here the save button in another form named Addwindow
  2. The Datagrid in another form named Machine_List
  3. I need the solution for,when i click on save button,the textbox values must stored in datagrid at the same time.

Here the below code for WPF C# am using but i cant get data.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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