asp.net select or delete data from listview after fill from datatable without database
  • Hi today i am sharing how can will fill listview from datatable without database connectivity and can select or delete record from listview in asp.net c#.
    First of all fill record in datatable.

    private void SetInitialRow()
        {
            DataTable dt = new DataTable();
            DataRow dr = null;
            dt.Columns.Add(new DataColumn("id", typeof(int)));
            dt.Columns.Add(new DataColumn("Name", typeof(string)));
            dt.Columns.Add(new DataColumn("Price", typeof(decimal)));
            dt.Columns.Add(new DataColumn("Stock", typeof(decimal)));

            dt.Columns["id"].AutoIncrement = true;
            dt.Columns["id"].AutoIncrementSeed = 1;
            dt.Columns["id"].AutoIncrementStep = 1;

            DataColumn[] PK_Column = new DataColumn[1];
            PK_Column[0] = dt.Columns["id"];

            dt.PrimaryKey = PK_Column;

            string[] ItemName = { "Item One", "Item Two", "Item Three", "Item Four", "Item Five", "Item Six", "Item Seven", "Item Eight", "Item Nine", "Item Ten" };

            for (int i = 0; i < ItemName.Length; i++)
            {
                dr = dt.NewRow();

                dr["Name"] = ItemName[i];
                dr["Price"] = i * 100;
                dr["Stock"] = i * 5;

                dt.Rows.Add(dr);
            }

            ViewState["CurrentTable"] = dt;

            ListView1.DataSource = dt;
            ListView1.DataBind();
        }


    Now call this function on page load for fill listview.

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetInitialRow();
            }
        }


    Now using below ListView1_ItemEditing event of listview for select record from listview

    protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e)
        {
            if (ViewState["CurrentTable"] != null)
            {
                DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];

                string id = dtCurrentTable.Rows[e.NewEditIndex]["id"].ToString();

                Response.Write("<b>Your Selected Item Is <br>");
                Response.Write("Name : " + dtCurrentTable.Rows[e.NewEditIndex]["Name"].ToString());
                Response.Write("<br>Price : " + dtCurrentTable.Rows[e.NewEditIndex]["Price"].ToString());
                Response.Write("<br>Stock : " + dtCurrentTable.Rows[e.NewEditIndex]["Stock"].ToString() + "</b> <br><br>");
            }
        }


    And use  ListView1_ItemDeleting event of listview for delete record from listview.

    protected void ListView1_ItemDeleting(object sender, ListViewDeleteEventArgs e)
        {
            if (ViewState["CurrentTable"] != null)
            {
                DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                string id = dtCurrentTable.Rows[e.ItemIndex]["id"].ToString();
                dtCurrentTable.Rows.Remove(dtCurrentTable.Rows.Find(id));

                ListView1.DataSource = dtCurrentTable;
                ListView1.DataBind();
            }
        }


    You Can also download attached source code.
    SelectOrDeleteFromListview.zip
    4K

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Tagged

Popular Posts of the Week

    Optimum Creative