This code sample has been given to devlope datagrid which can be used to access data into it.
public partial class pgDatagrid1 : System.Web.UI.Page { string strConnectionstring; clsBindgrid1 objGrid1 = new clsBindgrid1(); DataSet ds = new DataSet(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ds = objGrid1.bindgrid1(); dgTest1.DataSource = ds; dgTest1.DataBind(); } }
protected void dgTest1_CancelCommand(object source, DataGridCommandEventArgs e) { dgTest1.EditItemIndex = -1; ds = objGrid1.bindgrid1(); dgTest1.DataSource = ds; dgTest1.DataBind(); } protected void dgTest1_EditCommand(object source, DataGridCommandEventArgs e) { dgTest1.EditItemIndex = int.Parse(e.Item.ItemIndex.ToString()); ds = objGrid1.bindgrid1(); dgTest1.DataSource = ds; dgTest1.DataBind(); } protected void dgTest1_DeleteCommand(object source, DataGridCommandEventArgs e) {
if (e.CommandName.ToString() == "delete") { int intBookid = int.Parse(e.Item.Cells[0].Text.ToString());
SqlConnection conDelete; strConnectionstring = ConfigurationManager.AppSettings["strConstr"]; conDelete = new SqlConnection(strConnectionstring); conDelete.Open(); string strQuery; strQuery = "DELETE FROM BOOK WHERE BOOKID=" + intBookid; SqlCommand cmdDelete = new SqlCommand(); cmdDelete.CommandText = strQuery; cmdDelete.Connection = conDelete; cmdDelete.CommandType = CommandType.Text; cmdDelete.ExecuteNonQuery(); conDelete.Close(); dgTest1.EditItemIndex = int.Parse(e.Item.Cells[0].Text.ToString()); ds = objGrid1.bindgrid1(); dgTest1.DataSource = ds; dgTest1.DataBind(); } } protected void btnInsert_Click(object sender, EventArgs e) {
SqlConnection conInsert; strConnectionstring = ConfigurationManager.AppSettings["strConstr"]; conInsert = new SqlConnection(strConnectionstring); conInsert.Open(); string strInsquery; strInsquery = "INSERT INTO BOOK(BOOKNAME,BOOKPRICE)VALUES('" + tbBookname.Text.ToString() + "'," + int.Parse(tbBookprice.Text.ToString()) + ")"; SqlCommand cmdInsert = new SqlCommand(); cmdInsert.CommandText = strInsquery; cmdInsert.Connection = conInsert; cmdInsert.CommandType = CommandType.Text; cmdInsert.ExecuteNonQuery(); conInsert.Close(); dgTest1.EditItemIndex = -1; ds = objGrid1.bindgrid1(); dgTest1.DataSource = ds; dgTest1.DataBind();
} protected void dgTest1_UpdateCommand(object source, DataGridCommandEventArgs e) { int intBookid; string strBookname; int intBookprice;
intBookid = int.Parse(e.Item.Cells[0].Text.ToString()); strBookname = ((TextBox)e.Item.Cells[1].FindControl("txtbookname")).Text.ToString(); intBookprice = int.Parse(((TextBox)e.Item.Cells[2].FindControl("txtbookprice")).Text.ToString());
SqlConnection conUpdate; strConnectionstring = ConfigurationManager.AppSettings["strConstr"]; conUpdate = new SqlConnection(strConnectionstring); conUpdate.Open(); string strUpdatequery; strUpdatequery = "UPDATE BOOK SET BOOKNAME='" + strBookname + "',BOOKPRICE=" + intBookprice + " WHERE BOOKID=" + intBookid; SqlCommand cmdUpdate = new SqlCommand(); cmdUpdate.CommandText = strUpdatequery; cmdUpdate.Connection = conUpdate; cmdUpdate.CommandType = CommandType.Text; cmdUpdate.ExecuteNonQuery(); dgTest1.EditItemIndex = -1; ds = objGrid1.bindgrid1(); dgTest1.DataSource = ds; dgTest1.DataBind(); } }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|