private void BindGrid(int rowcount) { DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add(new System.Data.DataColumn("ItemID", typeof(String))); dt.Columns.Add(new System.Data.DataColumn("CategoryName", typeof(String))); dt.Columns.Add(new System.Data.DataColumn("Description", typeof(String))); dt.Columns.Add(new System.Data.DataColumn("ReorderLevel", typeof(String))); dt.Columns.Add(new System.Data.DataColumn("ReorderQuantity", typeof(String))); dt.Columns.Add(new System.Data.DataColumn("UnitOfMeasurement", typeof(String))); dt.Columns.Add(new System.Data.DataColumn("Balance", typeof(String))); if (ViewState["CurrentData"] != null) { for (int i = 0; i < rowcount + 1; i++) { dt = (DataTable)ViewState["CurrentData"]; if (dt.Rows.Count > 0) { dr = dt.NewRow(); dr[0] = dt.Rows[0][0].ToString(); } } dr = dt.NewRow(); dr[1] = ddlCategory.SelectedValue; dr[2] = txtDescription.Text; dr[3] = txtReorderLevel.Text; dr[4] = txtReorderQty.Text; dr[5] = txtUOM.Text; dr[6] = txtBalance.Text; dt.Rows.Add(dr); } else { dr = dt.NewRow(); dr[1] = ddlCategory.Text; dr[2] = txtDescription.Text; dr[3] = txtReorderLevel.Text; dr[4] = txtReorderQty.Text; dr[5] = txtUOM.Text; dr[6] = txtBalance.Text; dt.Rows.Add(dr); } // If ViewState has a data then use the value as the DataSource if (ViewState["CurrentData"] != null) { CatagoryGridView.DataSource = (DataTable)ViewState["CurrentData"]; CatagoryGridView.DataBind(); } else { // Bind GridView with the initial data assocaited in the DataTable CatagoryGridView.DataSource = dt; CatagoryGridView.DataBind(); } // Store the DataTable in ViewState to retain the values ViewState["CurrentData"] = dt; }
protected void CatagoryGridView_RowDeleting(object sender, GridViewDeleteEventArgs e) { DataTable dt = (DataTable)ViewState["CurrentData"]; int r = Convert.ToInt16(e.RowIndex); string itNo = dt.Rows[r][0].ToString(); cateController.deleteItem(itNo); dt.Rows[r].Delete(); CatagoryGridView.DataSource = dt; CatagoryGridView.DataBind(); ViewState["CurrentData"] = dt; }
private void LoadCatagory() { string connStr = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString; string sql = "SELECT * FROM Catagories"; SqlConnection con = new SqlConnection(connStr); SqlCommand cmd = new SqlCommand(sql, con); con.Open(); SqlDataReader reader = cmd.ExecuteReader(); CatagoryGridView.DataSource = reader; CatagoryGridView.DataBind(); con.Close(); }
protected void btnAdd_Click(object sender, EventArgs e) { //TO GRIDVIEW FROM DROPDOWN DataTable dt1 = new DataTable(); DataTable dt2 = new DataTable(); string category = ddlCategory.SelectedItem.ToString(); string description = txtDescription.Text; int reorderLevel = Convert.ToInt32(txtReorderLevel.Text); int reorderQty = Convert.ToInt32(txtReorderQty.Text); string unitOfMeasurement = txtUOM.Text; int balanceQty = Convert.ToInt32(txtBalance.Text); string prefix = category.Substring(0, 1); string s_lastNo = cateController.getLastRow(category); int number = Convert.ToInt32(s_lastNo.Substring(1, 3)); number++; if (number < 10) { itemNo = string.Concat(prefix, 0, 0, number); } else if (number < 100) { itemNo = string.Concat(prefix, 0, number); } else { itemNo = string.Concat(prefix, number); } //same with database column name dt1.Columns.Add("ItemID"); dt1.Columns.Add("CategoryName"); dt1.Columns.Add("Description"); dt1.Columns.Add("ReorderLevel"); dt1.Columns.Add("ReorderQuantity"); dt1.Columns.Add("UnitOfMeasurement"); dt1.Columns.Add("Balance"); DataRow dr = dt1.NewRow(); dr["ItemID"] = itemNo; dr["CategoryName"] = category; dr["Description"] = description; dr["ReorderLevel"] = reorderLevel; dr["ReorderQuantity"] = reorderQty; dr["UnitOfMeasurement"] = unitOfMeasurement; dr["Balance"] = balanceQty; dt1.Rows.Add(dr); CatagoryGridView.DataSource = dt1; CatagoryGridView.DataBind(); if (ViewState["CurrentData"] != null) { DataTable dt = (DataTable)ViewState["CurrentData"]; int count = dt.Rows.Count; BindGrid(count); } else { BindGrid(1); } txtDescription.Text = string.Empty; txtReorderLevel.Text = string.Empty; txtReorderQty.Text = string.Empty; txtUOM.Text = string.Empty; txtBalance.Text = string.Empty; if (cateController.insertAllCategory(itemNo, category, description, reorderLevel, reorderQty, unitOfMeasurement, balanceQty)) { // Response.Write("<script>alert('Ok')</script>"); lblCatelogueStatus.InnerHtml = "<span class='glyphicon glyphicon-ok-circle'></span> " + "Successfully added!"; loadData(); } else { btnAdd.CssClass = "btn btn-danger btn-block"; lblCatelogueStatus.InnerHtml = "<span class='glyphicon glyphicon-warning-sign'></span> " + "Adding Failed! Please try again later!"; //Response.Write("<script>alert('no')</script>"); loadData(); } }
protected void CatagoryGridView_PageIndexChanging(object sender, GridViewPageEventArgs e) { CatagoryGridView.DataSource = ViewState["CurrentData"]; CatagoryGridView.PageIndex = e.NewPageIndex; CatagoryGridView.DataBind(); }
public void bindData() { CatagoryGridView.DataSource = ViewState["CurrentData"]; CatagoryGridView.DataBind(); }
public void loadData() { CatagoryGridView.DataSource = cateController.getAllCategory(); CatagoryGridView.DataBind(); ViewState["CurrentData"] = cateController.getAllCategory(); }