下面是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;
}
答案 0 :(得分:0)
如果您使用不同的表单/窗口。在保存按钮中,您所要做的就是保存数据。
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();
}