我想在特定按钮单击的GridView中更新任何单元格时更新数据库。

数据库连接

    private void databaseBindingToGrid()     {      connetionString = @"initial catalog = Test; integrated security = SSPI; data source = KITS13AUG2019-I\JAGDEESH_SQL;";      conSql = new SqlConnection(connetionString);      conSql.Open();      cmd = new SqlCommand();      cmd.CommandText = "Select * from AGENTS";      cmd.Connection = conSql;      adpt = new SqlDataAdapter(cmd);      dt = new DataTable("AGENTS");      adpt.Fill(dt);      datGridView.ItemsSource = dt.DefaultView;      } 

我这样尝试过,但是没有用

    private void C1ToolbarButton_Click(object sender, RoutedEventArgs e)     {            adpt.Update(dt);            MessageBox.Show("Updated");     } 

===============>>#1 票数:0

您必须声明一个连接类,该类将使您的连接打开。

public SqlConnection Connection()     {       connetionString = @"initial catalog = Test; integrated security = SSPI;           data source = KITS13AUG2019-I\JAGDEESH_SQL;";       conSql = new SqlConnection(connetionString);       conSql.Open();        return conSql;     } 

之后,在表单加载操作中声明Datagridview元素。

public void FillDataGridView() {    cmd = new SqlCommand();    cmd.CommandText = "Select * from AGENTS";    cmd.Connection = Connection();    adpt = new SqlDataAdapter(cmd);    dt = new DataTable("AGENTS");    adpt.Fill(dt);    datGridView.ItemsSource = dt.DefaultView; } 

现在您可以使用

private void C1ToolbarButton_Click(object sender, RoutedEventArgs e)     {            //it is call your data grid view when you click button            FillDataGridView();     } 

  ask by Chary Jagadish translate from so

本文未有回复,本站智能推荐: