如何解决form1';s将文本框内容转换为表格2';wpf c#中的数据网格
本文关键字:wpf 表格 转换 网格 数据网 数据 解决 何解决 form1 文本 | 更新日期: 2023-09-27 18:01:09
- 这里是另一个名为Addwindow的表单中的保存按钮
- 另一种名为Machine_List的形式的数据网格
- 我需要一个解决方案,当我点击保存按钮时,文本框值必须同时存储在数据网格中
下面的代码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;
}
如果您使用不同的窗体/窗口。在保存按钮中,您所要做的就是保存数据。
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();
}
在数据网格所在的表单中,创建一个刷新数据网格的方法
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;
}
并且在window_ loaded调用中调用该方法。
private void Window_Loaded(object sender, RoutedEventArgs e)
{
refreshgrid();
}