example of editing in DataGrid and Default Paging

Discussion in 'ASP' started by hanusoft, Oct 4, 2007.

  1. hanusoft

    hanusoft New Member

    Joined:
    Sep 3, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    This is an example of editing in DataGrid and Default Paging


    Html Design Code : -
    HTML:
    <asp:DataGrid id="DataGrid1" DataKeyField="id"	runat="server" Height="224px" AutoGenerateColumns="False" PageSize="5" AllowPaging="True">
    	<Columns>
    		<asp:BoundColumn Visible="False" DataField="id"
    			HeaderText="Category Id">
    		</asp:BoundColumn>
    		<asp:TemplateColumn HeaderText="Category">
    			<ItemTemplate>
    				<asp:Label id=lblName text='<%# DataBinder.Eval(Container.DataItem,"name")%>' Runat="server">
    				</asp:Label>
    			</ItemTemplate>
    			<EditItemTemplate>
    				<asp:TextBox id=txtEdit Runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"name")%>'>
    				</asp:TextBox>
    			</EditItemTemplate>
    		</asp:TemplateColumn>
    		<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" HeaderText="Edit" CancelText="Cancel"
    	EditText="Edit"></asp:EditCommandColumn>
    	</Columns>
    </asp:DataGrid>
    
    Code (EditInDataGrid.aspx.cs) :

    Code:
    private void Page_Load(object sender, System.EventArgs e)
    {
    	// Put user code to initialize the page here
    	if(!IsPostBack)
    	{
    		
    		BindGrid();
    	}
    }
    private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    	DataGrid1.EditItemIndex = e.Item.ItemIndex;
    	BindGrid();
    	
    }
    private void BindGrid()
    {
    	SqlDataAdapter da = new SqlDataAdapter("select id,name from category",con);
    	DataSet objDS = new DataSet();
    	try
    	{
    		da.Fill(objDS,"Cat");
    		if(objDS.Tables[0].Rows.Count != 0)
    		{
    			DataGrid1.DataSource = objDS;
    			DataGrid1.DataBind();
    		}
    		else
    		{
    			DataGrid1.DataSource = null;
    			DataGrid1.DataBind();
    			
    			Response.Write("No record found.");
    		}
    	}
    	catch(Exception ex)
    	{
    		throw ex;
    	}
    }
    
    private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    	try
    	{
    		string strCatName = ((TextBox)e.Item.FindControl("txtEdit")).Text;
    		string strId = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
    		SqlCommand com = new SqlCommand("update category set name ='"+strCatName+"' where id = "+strId,con);
    		con.Open();
    		com.ExecuteNonQuery();
    		DataGrid1.EditItemIndex = -1;
    		BindGrid();
    	}
    	catch(Exception ex)
    	{
    		throw ex;
    	}
    }
    
    private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    	DataGrid1.EditItemIndex = -1;
    	BindGrid();
    }
    
    private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    	DataGrid1.CurrentPageIndex = e.NewPageIndex;
    	BindGrid();
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice