データベースアプリケーションを作成しました。現時点では、追加および削除機能がありますが、現在、更新機能のヘルプを探しています。私は答えを求めてこのウェブサイト全体を検索しましたが、運がなかったと思います. だから私が欲しいのは、ListViewのレコードをクリックすると、テキストボックスに自動的に表示されて変更できるようになり、「更新」ボタンをクリックするとレコードを更新する必要があるということです。
ヘルプは非常に高く評価されます!
Form1の私のコード
public SqlCeConnection conn = new SqlCeConnection(@"Data Source=E:\Users\Ali\Documents\automail.sdf");
////////////////////////////////////Methodes////////////////////////////////////
private void Populate()
{
SqlCeCommand cm = new SqlCeCommand("SELECT * FROM Emails ORDER BY principalID", conn);
listView1.Items.Clear();
try
{
SqlCeDataReader dr = cm.ExecuteReader();
while (dr.Read())
{
ListViewItem it = new ListViewItem(dr["principalID"].ToString());
it.SubItems.Add(dr["email"].ToString());
it.SubItems.Add(dr["query"].ToString());
it.SubItems.Add(dr["subject"].ToString());
listView1.Items.Add(it);
}
dr.Close();
dr.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Application.ExitThread();
}
}
////////////////////////////////////Methodes////////////////////////////////////
// Insert button (for data insert)
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
{
MessageBox.Show("Fill in all the information first!");
}
else
{
SqlCeCommand cmd = new SqlCeCommand("INSERT INTO Emails(principalID, email, query, subject) VALUES(@principalID, @email, @query, @subject)", conn);
cmd.Connection = conn;
cmd.Parameters.AddWithValue("@principalID", textBox1.Text);
cmd.Parameters.AddWithValue("@query", textBox2.Text);
cmd.Parameters.AddWithValue("@email", textBox3.Text);
cmd.Parameters.AddWithValue("@subject", textBox4.Text);
try
{
int affectedrows = cmd.ExecuteNonQuery();
if (affectedrows > 0)
{
Populate();
MessageBox.Show("Email added successfully!");
}
else
{
MessageBox.Show("Failed to add email!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
//if form shown update listview
private void Form1_Shown(object sender, EventArgs e)
{
try
{
conn.Open();
Populate();
}
catch (SqlCeException ex)
{
MessageBox.Show(ex.Message);
Application.ExitThread();
}
}
//open form 2 and hide this
private void button2_Click(object sender, EventArgs e)
{
this.Hide();
Form2 form2 = new Form2();
form2.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//if listview selected enable button, else disable
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
button3.Enabled = true;
}
else
{
button3.Enabled = false;
}
}
private void button3_Click(object sender, EventArgs e)
{
}
//delete record button
private void button3_Click_1(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count <= 0)
{
MessageBox.Show("Select a record!");
}
else
{
for (int i = 0; i < listView1.SelectedItems.Count; i++)
{
SqlCeCommand cm = new SqlCeCommand("DELETE FROM Emails WHERE principalID = @principalID", conn);
cm.Parameters.AddWithValue("@principalID", listView1.SelectedItems[0].Text);
try
{
cm.ExecuteNonQuery();
Populate();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
private void button2_Click_1(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
ListViewItem itm = listView1.SelectedItems[0];
string principalid = itm.SubItems[0].Text;
string query = itm.SubItems[1].Text;
string email = itm.SubItems[2].Text;
Form2 form2 = new Form2();
form2.Show();
form2.PrincipalID = principalid;
form2.Query = query;
form2.Email = email;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
}
}
}