通过列表视图和文本框更新数据
本文关键字:更新 数据 文本 列表 视图 | 更新日期: 2023-09-27 18:02:38
我做了一个数据库应用程序。目前我有一个添加和删除功能,但现在我正在寻找更新功能的帮助。我在这个网站上搜索了所有的答案,但我猜没有运气。所以我想要的是,如果我点击ListView中的记录,它会自动显示在文本框中,这样我就可以改变的东西,当我点击按钮"更新"它必须更新的记录。
帮助将非常感激!
my code of 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)
{
}
}
}
为什么使用listview ?你可以使用datagridview。您可以将数据库文件与datagridview绑定,datagridview将自己创建更新,选择和插入命令。
我不明白为什么你不能这样做。在listview中更新条目相对容易。
您需要为您的ListView
创建EditItemTemplate
,以便文本框出现在要编辑的行中。
您可以在ItemEditing
事件中处理此问题。然后通过ListView
的ItemUpdating
事件处理更新。
下面我发布了一个示例:它可能会帮助你:
像这样创建ListView标记:
<asp:ListView ID="lvwTest" runat="server" OnItemDeleting="lvwTest_ItemDeleting" OnItemEditing="lvwTest_ItemEditing"
OnItemUpdating="lvwTest_ItemUpdating">
<LayoutTemplate>
<table>
<tr>
<td>
Column1
</td>
<td>
Column2
</td>
<td>
Column3
</td>
<td>
Action
</td>
</tr>
<tr id="itemplaceholder" runat="server">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lbl1" runat="server" Text='<%#Eval("col1") %>'></asp:Label>
</td>
<td>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("col2") %>'></asp:Label>
</td>
<td>
<asp:Label ID="Label2" runat="server" Text='<%#Eval("col3") %>'></asp:Label>
</td>
<td>
<asp:Button ID="btnEdit" CommandName="Edit" runat="server" Text="Edit" />
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td>
<asp:TextBox ID="txt1" runat="server" Text='<%#Bind("col1") %>'></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txt2" runat="server" Text='<%#Bind("col2") %>'></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txt3" runat="server" Text='<%#Bind("col3") %>'></asp:TextBox>
</td>
<td>
<asp:Button ID="btnUpdate" CommandName="Update" Text="Update" runat="server" />
<asp:Button ID="btnCancel" CommandName="Cancel" Text="Cancel" runat="server" />
</td>
</tr>
</EditItemTemplate>
</asp:ListView>
基本上你已经创建了EditItemTemplate
。
看看ItemTemplate
在最后一列有一个按钮"btnEdit
",EditItemTemplate
有两个按钮"btnUpdate
"answers"btnCancel
",而不是适当的CommandName
。
现在这是你的事件:
ItemEditing:
protected void lvwTest_ItemEditing(object sender, ListViewEditEventArgs e)
{
lvwTest.EditIndex = e.NewEditIndex;
}
ItemUpdating:
protected void lvwTest_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
TextBox txt = (lvwTest.Items[e.ItemIndex].FindControl("txt1")) as TextBox;
//as above find othe textboxes as well/
//create your update query
//SqlCeCommand cmd = new SqlCeCommand(updateQuery, cn);
//and rest of the syntax
lvwTest.EditIndex = -1;
BindGrid();
}
你数据源:public void BindGrid()
{
DataTable db = new DataTable();
db.Columns.Add("col1");
db.Columns.Add("col2");
db.Columns.Add("col3");
db.Rows.Add("1", "2", "3");
db.Rows.Add("1", "2", "3");
db.Rows.Add("1", "2", "3");
lvwTest.DataSource = db;
lvwTest.DataBind();
}
内页Page_Load
if (!IsPostBack)
BindGrid();