![]() |
Web Development »
ASP.NET Controls »
Grid Controls
License: The Code Project Open License (CPOL)
Datagrid OperationsBy Patel Vinaythis is to illustrate grid update operation |
C# (C# 1.0, C# 2.0, C# 3.0), .NET (.NET 1.1, .NET 2.0), ASP.NET
|
||||||||
|
Advanced Search |
|
|
|
||||||||||||||||
This is article/code snippet for datagrid update operation and delete operation with custome error message and enableing disabling grid operation depands on various conditions.
Lets Begin Step by step
Step 1: Add Datagrid to webform
Step 2: Disable auto generate column false
Step 3: Now go to column property and create columns that you need
Step 4: Add Edit Button and Delete button, and convert them as template columns (if you need to enable and disable them depands on conditions)
Now Generating Events for grid
we need Edit and Delete Grid Event and first we need to add the event handlers
private void InitializeComponent() { this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged); this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand); this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand); this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand); }
Now we will add the events as follows
public void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { DataGrid1.EditItemIndex = e.Item.ItemIndex; BindData(); }
public void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { // All we do in the cancel method is to assign '-1' to the datagrid editItemIndex // Once the edititemindex is set to '-1' the datagrid returns back to its original condition DataGrid1.EditItemIndex = -1; BindData(); }
public void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { //you need this code if you need to retrive existing cell for verification System.Web.UI.WebControls.TableCell Id = new System.Web.UI.WebControls.TableCell(); System.Web.UI.WebControls.TableCell Name = new System.Web.UI.WebControls.TableCell(); System.Web.UI.WebControls.TableCell City = new System.Web.UI.WebControls.TableCell(); System.Web.UI.WebControls.TextBox NameVer = new System.Web.UI.WebControls.TextBox(); System.Web.UI.WebControls.TextBox CityVer = new System.Web.UI.WebControls.TextBox(); Id = (System.Web.UI.WebControls.TableCell)e.Item.Cells[0]; Name = (System.Web.UI.WebControls.TableCell)e.Item.Cells[1]; NameVer = (System.Web.UI.WebControls.TextBox)e.Item.Cells[2].Controls[1]; City = (System.Web.UI.WebControls.TableCell)e.Item.Cells[3]; CityVer = (System.Web.UI.WebControls.TextBox)e.Item.Cells[4].Controls[1]; if (Name.Text.Trim() == NameVer.Text.Trim() && City.Text.Trim() == CityVer.Text.Trim()) { string Qry = "UPDATE User_Info SET Name =' " + Name.Text + "',City ='" + City.Text + "',Status = 1 WHERE Id= " + Id.Text; SqlCommand myCommand = new SqlCommand(Qry, conn); myCommand.CommandType = CommandType.Text; conn.Open(); myCommand.ExecuteNonQuery(); conn.Close(); DataGrid1.EditItemIndex = -1; BindData(); ShowMessage("Value Updated Sucessfully"); } ShowMessage("Value Not Valid"); }
public void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) { DataGrid1.CurrentPageIndex = e.NewPageIndex; BindData(); }
protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e) { System.Web.UI.WebControls.TableCell Id = new System.Web.UI.WebControls.TableCell(); Id = (System.Web.UI.WebControls.TableCell)e.Item.Cells[0]; string Qry = "Delete from User_Info WHERE Id=" + Id.Text + "and status =1 "; SqlCommand myCommand = new SqlCommand(Qry, conn); myCommand.CommandType = CommandType.Text; conn.Open(); int ans = myCommand.ExecuteNonQuery(); conn.Close(); DataGrid1.EditItemIndex = -1; BindData(); if (ans ==1 ) ShowMessage("Value Deleted Sucessfully"); else ShowMessage("Not allowed to Deleted non verified Records"); }
Now in page load we need to load the data on the grid as follows
protected void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if (!Page.IsPostBack) { BindData(); } }
public void BindData() { SqlCommand myCommand = new SqlCommand("select Id , Name , City , Phone , Status from User_Info", conn); myCommand.CommandType = CommandType.Text; SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand); DataSet ds = new DataSet(); myAdapter.Fill(ds, "tblPerson"); conn.Open(); myCommand.ExecuteNonQuery(); DataGrid1.DataSource = ds; DataGrid1.DataBind(); conn.Close(); }
Javascript is used to show custom message as below
private void ShowMessage(string msgtext) { String msgString = "<script language="JavaScript">"; msgString += "window.alert('" + msgtext + "');"; msgString += "</script>"; Page.RegisterStartupScript("showmessage", msgString); }
Follwing code is use to enable/disable the edit/delete links
public bool checkStatus(object data) { int val = Int32.Parse(data.ToString()); if (val == 1) return false; else return true; }
public string SetText(object data, object refcol, string title) { switch (title) { case "Status": if (Convert.ToInt16(refcol.ToString()) == 1) return "Already Verified"; else return "Verify"; case "Delete": if (Convert.ToInt16(refcol.ToString()) == 1) return "Delete"; else return "Verify First"; //case "PER" : if (Convert.ToInt16(refcol.ToString()) == 1) // { // DateTime dt = Convert.ToDateTime(data.ToString()); // return dt.ToShortDateString() ; // } // else // return ""; default: if (Convert.ToInt16(refcol.ToString()) == 1) return data.ToString(); else return ""; } return ""; }
you need some code in html page as well refere to the code for that Download DataGrid.zip - 9.79 KB
| You must Sign In to use this message board. | |||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 25 Oct 2008 Editor: |
Copyright 2008 by Patel Vinay Everything else Copyright © CodeProject, 1999-2009 Web17 | Advertise on the Code Project |