前台:
<DataGrid HorizontalAlignment=”Left”
Name=”dg_datagrid” Height=”117″
Margin=”209,32,0,0″
VerticalAlignment=”Top” Width=”232″
AutoGenerateColumns=”False”>
<DataGrid.Columns>
<DataGridTextColumn Binding=”{Binding Path=ShipperID}”
Header=”编号”/>
<DataGridTextColumn Binding=”{Binding Path=CompanyName}”
Header=”名称”/>
<DataGridTextColumn Binding=”{Binding Path=Phone}”
Header=”号码”/>
</DataGrid.Columns>
</DataGrid>
后台:
private void save_Click(object sender, RoutedEventArgs e)
{
dt = (dg_datagrid.ItemsSource as DataView).Table;
SqlConnection conn = new SqlConnection(@”Data Source=.\SQLExpress;Initial Catalog=WPF_for_ado;Integrated Security=True”);
SqlCommand cmd = new SqlCommand(sql_load,conn);
sda = new SqlDataAdapter(cmd);
SqlCommandBuilder cb = new SqlCommandBuilder(sda);
cb.RefreshSchema();
sda.Update(dt); –>运行的时候,报错
MessageBox.Show(“完成保存!”, “提示”, MessageBoxButton.OK);
}
求大牛指点,怎么样只实现保存修改过后的 DataGrid
<DataGrid HorizontalAlignment=”Left”
Name=”dg_datagrid” Height=”117″
Margin=”209,32,0,0″
VerticalAlignment=”Top” Width=”232″
AutoGenerateColumns=”False”>
<DataGrid.Columns>
<DataGridTextColumn Binding=”{Binding Path=ShipperID}”
Header=”编号”/>
<DataGridTextColumn Binding=”{Binding Path=CompanyName}”
Header=”名称”/>
<DataGridTextColumn Binding=”{Binding Path=Phone}”
Header=”号码”/>
</DataGrid.Columns>
</DataGrid>
后台:
private void save_Click(object sender, RoutedEventArgs e)
{
dt = (dg_datagrid.ItemsSource as DataView).Table;
SqlConnection conn = new SqlConnection(@”Data Source=.\SQLExpress;Initial Catalog=WPF_for_ado;Integrated Security=True”);
SqlCommand cmd = new SqlCommand(sql_load,conn);
sda = new SqlDataAdapter(cmd);
SqlCommandBuilder cb = new SqlCommandBuilder(sda);
cb.RefreshSchema();
sda.Update(dt); –>运行的时候,报错
MessageBox.Show(“完成保存!”, “提示”, MessageBoxButton.OK);
}
求大牛指点,怎么样只实现保存修改过后的 DataGrid
解决方案
30
public partial class MainWindow : Window
{
SqlDataAdapter sda;
DataTable dt;
public MainWindow()
{
InitializeComponent();
getData();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
try
{
sda.Update(dt);
MessageBox.Show(“修改成功”,”提示”);
getData();
}
catch (Exception ee)
{
MessageBox.Show(“错误原因是” + ee.Message, “提示”);
}
}
void getData()
{
string connectionString = string.Format(“Data Source=192.168.0.250;Initial Catalog=IBMS;user=sa;password=password;timeout=30;Max Pool Size=75;Min Pool Size=5”);
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
sda = new SqlDataAdapter(“select yhm,mm,bmdm,id from yhxxb”, connection);
SqlCommandBuilder commbuilder = new SqlCommandBuilder(sda);
sda.UpdateCommand = commbuilder.GetUpdateCommand();
dt = new DataTable();
sda.Fill(dt);
listView1.ItemsSource = dt.DefaultView;
connection.Close();
}
}
{
SqlDataAdapter sda;
DataTable dt;
public MainWindow()
{
InitializeComponent();
getData();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
try
{
sda.Update(dt);
MessageBox.Show(“修改成功”,”提示”);
getData();
}
catch (Exception ee)
{
MessageBox.Show(“错误原因是” + ee.Message, “提示”);
}
}
void getData()
{
string connectionString = string.Format(“Data Source=192.168.0.250;Initial Catalog=IBMS;user=sa;password=password;timeout=30;Max Pool Size=75;Min Pool Size=5”);
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
sda = new SqlDataAdapter(“select yhm,mm,bmdm,id from yhxxb”, connection);
SqlCommandBuilder commbuilder = new SqlCommandBuilder(sda);
sda.UpdateCommand = commbuilder.GetUpdateCommand();
dt = new DataTable();
sda.Fill(dt);
listView1.ItemsSource = dt.DefaultView;
connection.Close();
}
}
10
sda.Update(dt); –>修改过后的前台 datagrid //这个是更新 下面的是重新加载一次 第一个是加载。