Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > Other ASP.NET > ASP.NET 1.x and 2.0 Application Design
|
ASP.NET 1.x and 2.0 Application Design Application design with ASP.NET 1.0, 1.1, and 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.x and 2.0 Application Design section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 4th, 2007, 08:13 AM
hanusoft
Guest
 
Posts: n/a
Default example of editing in DataGrid and Default Paging

This is an example of editing in DataGrid and Default Paging


Html Design Code : -

<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) :

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.DataGridPageChangedEvent Args e)
        {
            DataGrid1.CurrentPageIndex = e.NewPageIndex;
            BindGrid();





Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom paging in Datagrid with datagrid page count madhusrp ASP.NET 1.0 and 1.1 Professional 12 June 2nd, 2008 01:15 PM
Editing Datagrid toddw607 ASP.NET 2.0 Basics 5 April 26th, 2007 03:06 PM
Editing in DataGrid abdul_owiusa ASP.NET 1.0 and 1.1 Professional 0 May 1st, 2006 07:56 AM
Default Paging Issue Jeff1218 Classic ASP Databases 2 March 4th, 2005 05:27 AM
text boxes / data grids/ editing and paging Barb Lehman Classic ASP Basics 0 December 4th, 2003 12:55 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.