이 양식을 사용하여 ID를 지정하고 강조 표시된 버튼을 클릭하면 데이터베이스에서 가져온 DataGridview에 일부 정보가 표시됩니다. First Formdatagridview의 변경 사항이 데이터베이스에 저장되지 않았습니다.
버튼의 코드는 여기에 있습니다 : 우리가 데이터베이스에서 무언가를 얻을
private void kerkoButton_Click(object sender, EventArgs e)
{
try
{
Konektimi();
string query = "SELECT * FROM Vizita WHERE [email protected]";
using (SqlCommand command = new SqlCommand(query, conn))
{
command.Parameters.AddWithValue("@pacientiId",pacientiTextBox.Text);
conn.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
DataTable dt = new DataTable();
dt.Load(reader);
forma.vizitaDataGridView.DataSource = dt;
forma.ShowDialog();
}
else
{
MessageBox.Show("Nuk ka të dhëna për këtë pacient");
}
}
conn.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Suposing 우리가이 얻을 : Second Form,with the results
이 클릭 한 후 열 수있는 새로운 형태의 kerkoButton.
지금 문제는 다음과 같습니다. 행 (또는 일부 행)을 편집 할 때 저장을 클릭하면 변경 사항이 데이터베이스에 저장되지 않습니다. 어떻게해야합니까? 이 DataGridform에 SaveButton을 만들 수 있습니까? 그렇다면 어떻게됩니까?
DataRowView dataRowView = yourDataGridView.CurrentRow.DataBoundItem as DataRowView;
DataRow[] rowsToUpdate = new DataRow[] { dataRowView.Row };
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Vizita", con);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.Update(rowsToUpdate);
또는, 당신은 모든 편집 행을 업데이트 할 경우 :
데이터 바인딩을 올바르게 사용하면 DataTable에서 자체 변경 내용을 추적하므로 필요한 경우 어댑터에서 Update를 호출하면됩니다. 격자 행을 반복하는 경우 잘못 처리하고 있습니다. – Crowcoder