我有两个datagridview(viewa,viewb)。我通过在 viawb 中单击和编辑将行 viewa 传递给 viewb。我可以从服务器获取项目以查看,但我无法更新数据表。目前我正在用一个 dgv 尝试这个。如果您至少可以指导我,将不胜感激。
public Form1()
{
InitializeComponent();
}
SqlCommand command = new SqlCommand();
DataTable data = new DataTable();
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=server.server.com;user=testuser;pwd=password;database=test";
SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandText = "select * from malzeme";
DataTable data = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(data);
dataGridView1.DataSource = data;
}
private void save_Click(object sender, EventArgs e)
{
try
{
string connectionString= "server=server.server.com;user=testuser;pwd=password;database=test";
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.UpdateCommand = new SqlCommand("UPDATE malzeme SET malzemekodu=@malzemekodu " + "WHERE Id=@Id", conn);
adapter.Update(data);
}
}
catch
{
}
}
}