我試圖在C#Windows窗體應用程序。我試圖刪除的GridView特定行和變更推到數據庫中。該刪除確實會刪除該行。我想要做的就是將更新後的gridview推入數據庫。如何推動更新GridView控件數據庫?
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
string ind = e.RowIndex.ToString();
int myint = int.Parse(ind);
dataGridView1.Rows.Remove(dataGridView1.Rows[myint]);
}
這是我用來顯示數據庫的代碼。
using (SqlConnection MyConn = new SqlConnection("Data Source=LENOVO\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=SampleData"))
{
string query = "SELECT * from dbo. userpass";
SqlCommand cmd = new SqlCommand(query, MyConn);
try
{
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bSource = new BindingSource();
bSource.DataSource = dbdataset;
dataGridView1.DataSource = bSource;
sda.Update(dbdataset);
}
catch
{
}
}