Edit,Update,Delete and Insert in ListView


This snippet deals with insert,edit,update and delete in a listview

List view has got many similar features as that of gridview and i ve tried to use listview instead on grid view,firstly drag and drop a listview from the tool box and inside the layout template add a table and specify the items to be displayed in the list view and in the item template place a button to edit and delete and label to bind the datas and in the edit item template place the button for update and cancel


Binding Data to the Listview



Binding data to the list view is similar to binding the data in the gridview and is as follows


private void Bindlist()
{
SqlConnection Con = new SqlConnection(Connection);
Con.Open();
Da = new SqlDataAdapter("select * from listview", Con);
DataTable dt = new DataTable();
Da.Fill(dt);
ListView1.DataSource = dt;
ListView1.DataBind();
Con.Close();
}


To insert Data



To insert data into the listview is the same as that of the grid and is as follows


protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(Connection);
con.Open();
SqlCommand cmd = new SqlCommand("insert into Listview values ('"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"')",con);
cmd.ExecuteNonQuery();
con.Close();
Bindlist();

}


To Edit



To edit use the item editing event in the listview


protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e)
{
ListView1.EditIndex = e.NewEditIndex;
SqlConnection Con = new SqlConnection(Connection);
Da = new SqlDataAdapter("select * from ListView", Con);
DataTable dt = new DataTable();
ListView1.DataSource = dt;
ListView1.DataBind();
}


Item Updating,Item databound and deleting




protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
int Id = 0;
string FirstName = string.Empty;
string LastName = string.Empty;
string ContactType = string.Empty;
Label lbl = (ListView1.Items[e.ItemIndex].FindControl("IDLabel")) as Label;

if (lbl != null)

Id = Convert.ToInt32(lbl.Text);


ListViewItem Item = (ListViewItem)ListView1.Items[e.ItemIndex];
TextBox Txt = (ListView1.Items[e.ItemIndex].FindControl("FirstNameTextBox")) as TextBox;
TextBox txt2 = (ListView1.Items[e.ItemIndex].FindControl("LastNameTextBox")) as TextBox;
TextBox txt3 = (ListView1.Items[e.ItemIndex].FindControl("ContactType")) as TextBox;
SqlConnection con = new SqlConnection(Connection);
con.Open();
SqlCommand cmd = new SqlCommand("UPDATE [ListView] SET [FirstName] = '" + FirstName + "', [LastName] = '" + LastName + "', [ContactType] = '" + ContactType + "' WHERE [ID] = '" + ID + "'");
ListView1.EditIndex = -1;
con.Close();
}


ItemDatabound



ItemdataBound occurs when a data item is bound to data in a ListView control


protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
if (dataItem.DisplayIndex == ListView1.EditIndex)
{
TextBox tb = e.Item.FindControl("FirstNameTextBox") as TextBox;
TextBox TB = e.Item.FindControl("LastNameTextBox") as TextBox;
}
}
}


Deleting




protected void ListView1_ItemDeleting(object sender, ListViewDeleteEventArgs e)
{
int id = 0;
Label lbl = (ListView1.Items[e.ItemIndex].FindControl("IDLabel")) as Label;
if (lbl.Text != "0")
id = Convert.ToInt32(lbl.Text);
string DeleteQuery = "Delete from ListView WHERE [ID] = '" + ID + "'";
SqlConnection con = new SqlConnection(Connection);
con.Open();
SqlCommand com = new SqlCommand(DeleteQuery, con);
com.ExecuteNonQuery();
con.Close();
ListView1.EditIndex = -1;
Da = new SqlDataAdapter("select * from listview", con);
DataTable dt = new DataTable();
Da.Fill(dt);
ListView1.DataSource = dt;
ListView1.DataBind();
}


Comments

Author: praveen27 Sep 2010 Member Level: Gold   Points : 0

Thanks..could you please share the attachment file..



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: