editing in datagrid

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • geeteshss
    New Member
    • Jun 2007
    • 16

    #1

    editing in datagrid

    hello guys thanks for the previous reply
    now my problem is that i want to display the data in the datagrid from database also i want to edit the data in the datagrid but it is also not providing an update and cancel button if anybody could help me out i am posting my code below

    Code:
    private void Page_Load(object sender, System.EventArgs e)
    		{
    			if (!Page.IsPostBack)
    			{
    				BindData();
    			}
    
    
    			// Put user code to initialize the page here
    			OleDbDataAdapter da = new OleDbDataAdapter("SELECT *FROM CMRS_BUSINESSAREA_MASTER","Provider=MSDAORA.1;User ID=meter_reading;Password=colony;Data Source=windev;OLEDB.NET=true;" );
    			DataSet ds = new DataSet();
    			da.Fill(ds);
    			DataGrid1.DataSource = ds.Tables[0].DefaultView ;
    			DataGrid1.DataBind();
    
    }
    	public void BindData()
    		{
    			OleDbDataAdapter da = new OleDbDataAdapter("SELECT *FROM CMRS_BUSINESSAREA_MASTER","Provider=MSDAORA.1;User ID=meter_reading;Password=colony;Data Source=windev;OLEDB.NET=true;" ); 
    			DataSet ds = new DataSet(); 
    			da.Fill(ds);
    			
    			DataGrid1.DataSource = ds.Tables[0].DefaultView ;
    			DataGrid1.DataBind();
    			
    
    		}
    		private void Edit_DataGrid1(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) 
    		{ 
    			// We use CommandEventArgs e to get the row which is being clicked 
    			// This also changes the DataGrid labels into Textboxes so user can edit them 
    			DataGrid1.EditItemIndex = e.Item.ItemIndex; 
    			// Always bind the data so the datagrid can be displayed. 
    			BindData(); 
    		} 
    		private void Cancel_DataGrid1(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(); 
    		} 
    		private void Update_DataGrid1(object Source, DataGridCommandEventArgs E)
    		{
    			
    			
    			OleDbCommand cmd;
    			
    			TextBox txtAREADESCRIPTION =(TextBox) E.Item.Cells[1].Controls[0];
    			string strUpdateStmt;
    
    			strUpdateStmt = "UPDATE CMRS_BUSINESSAREA_MASTER SET  " + "AREADESCRIPTION = '" + txtAREADESCRIPTION.Text + "' " + "WHERE AREACODE = " + E.Item.Cells[0].Text;
    
    			OleDbConnection con1 = new OleDbConnection("Provider=MSDAORA.1;User ID=meter_reading;Password=colony;Data Source=windev;OLEDB.NET=true;");
    			cmd = new OleDbCommand(strUpdateStmt, con1);
    			con1.Open();
    			cmd.ExecuteNonQuery();
    
    			DataGrid1.EditItemIndex = -1;
    			BindData();
    		}
    private void addbtn_Click(object sender, System.EventArgs e)
    		{
    			string str = ConfigurationSettings.AppSettings["ConnectionString"].ToString();  
    
    			OleDbConnection con1 = new OleDbConnection();
    			con1.ConnectionString = str;
    			OleDbCommand cmd = new OleDbCommand("Insert into cmrs_businessarea_master values('"+TextBox1.Text+"','"+TextBox2.Text+"')",con1);		 
    			con1.Open();
    			cmd.ExecuteNonQuery();
    			con1.Close();
    
    
    	
    		}
    		
    
    		private void updtbtn_Click(object sender, System.EventArgs e)
    		{
    			// binddata();
    
    		}
    
    		private void delbtn_Click(object sender, System.EventArgs e)
    		{
    		
    		}
    
    
    
    	}
    		
    		
    	
    }
    Last edited by geeteshss; Jul 19 '07, 06:41 AM. Reason: due to certain errors
Working...