Web Design and Development Forums

example of editing in DataGrid and Default Paging

This is a discussion on "example of editing in DataGrid and Default Paging" within the ASP.NET Forum section. This forum, and the thread "example of editing in DataGrid and Default Paging are both part of the Program Your Website category.


Go Back   Webforumz.com > Program Your Website > ASP.NET Forum

Welcome to Webforumz.com.
Register Now Register now!

Reply
 
LinkBack Thread Tools Rate Thread
Old Oct 4th, 2007, 13:34   #1 (permalink)
New Member
 
Join Date: Sep 2007
Location: gurgaon
Age: 21
Posts: 4
example of editing in DataGrid and Default Paging

This is an example of editing in DataGrid and Default Paging

Html Design Code : -
HTML: Select all
<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: Select all
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();
This is an example of editing in DataGrid and Default Paging

Html Design Code : -
HTML: Select all
<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: Select all
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();

Last edited by karinne; Oct 4th, 2007 at 13:42. Reason: Add vBcode and removed the links
hanusoft is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Oct 4th, 2007, 13:41   #2 (permalink)
Section Manager -
Website Critique
 
welshstew's Avatar
 
Join Date: May 2007
Location: inside the outside
Posts: 1,094
Blog Entries: 10
Re: example of editing in DataGrid and Default Paging

thanks, but what is the issue you are facing?

If this is just a tutorial, then please can you comment your code so that we can see what is going on and what you are trying to achieve.
__________________
WelshStew
Section Manager

tierney rides tboard - uk site : xtreme wales - extreme clothing
welshstew is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pager Style in Datagrid danasegarane ASP.NET Forum 2 Jan 14th, 2008 10:40
Datagrid and update command Mitsuki ASP.NET Forum 0 May 16th, 2006 05:06
CheckBox problem with ASP.Net DataGrid frmsasp ASP.NET Forum 2 Sep 28th, 2005 09:04
Passing values from Datagrid to another form caryma ASP.NET Forum 4 Aug 17th, 2005 08:06
VS.NET, TextBox, overlay, DataGrid Smokie ASP.NET Forum 10 Jul 27th, 2004 12:17



Latest Updates

All Points SEO Security Advisory - CHECK YOUR SITE NOW!

Creative Coding :: February 2008

Webforumz is sponsored by: WESH UK Web Hosting
All times are GMT. The time now is 14:13.

Sleep Study Scoring :: Free Bet :: Website Templates :: Online Betting :: Bookmakers :: Funny Quotes :: Internet Recruitment Software :: Microsoft CRM Experts :: Online Casino :: Decorated Christmas Trees :: Midwife Forums :: Football Betting :: Ecommerce Software :: Web Hosting :: Football Stats :: Dry Cleaning Collection :: xtreme wales - extreme clothing :: Apuestas :: Sharepoint Consultants :: Website Optimisation :: Office Clearance London :: Sharepoint Experts :: Sports Betting :: Casino :: Website Templates :: Web Design Development India :: Online Gambling

Powered by: vBulletin Version 3.7, Copyright ©2000 - 2008, Jelsoft Enterprises Limited.
© 2003-2008 Webforumz.com : All Rights Reserved
Search Engine Friendly URLs by vBSEO 3.2.0 RC6


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59