473,769 Members | 3,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

example of editing in DataGrid and Default Paging

4 New Member
This is an example of editing in DataGrid and Default Paging

http://www.hanusoftwar e.com

Html Design Code : -

<asp:DataGrid id="DataGrid1" DataKeyField="i d" runat="server" Height="224px" AutoGenerateCol umns="False" PageSize="5" AllowPaging="Tr ue">
<Columns>
<asp:BoundColum n Visible="False" DataField="id"
HeaderText="Cat egory Id"></asp:BoundColumn >
<asp:TemplateCo lumn HeaderText="Cat egory">
<ItemTemplate >
<asp:Label id=lblName text='<%# DataBinder.Eval (Container.Data Item,"name")%>' Runat="server">
</asp:Label>
</ItemTemplate>
<EditItemTempla te>
<asp:TextBox id=txtEdit Runat="server" Text='<%# DataBinder.Eval (Container.Data Item,"name")%>' >
</asp:TextBox>
</EditItemTemplat e>
</asp:TemplateCol umn>
<asp:EditComman dColumn ButtonType="Lin kButton" UpdateText="Upd ate" HeaderText="Edi t" CancelText="Can cel"
EditText="Edit" ></asp:EditCommand Column>
</Columns>
</asp:DataGrid>

Code (EditInDataGrid .aspx.cs) :

private void Page_Load(objec t sender, System.EventArg s e)
{
// Put user code to initialize the page here
if(!IsPostBack)
{

BindGrid();
}
}
private void DataGrid1_EditC ommand(object source, System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
{
DataGrid1.EditI temIndex = e.Item.ItemInde x;
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.DataS ource = objDS;
DataGrid1.DataB ind();
}
else
{
DataGrid1.DataS ource = null;
DataGrid1.DataB ind();

Response.Write( "No record found.");
}
}
catch(Exception ex)
{
throw ex;
}
}

private void DataGrid1_Updat eCommand(object source, System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
{
try
{
string strCatName = ((TextBox)e.Ite m.FindControl(" txtEdit")).Text ;
string strId = DataGrid1.DataK eys[e.Item.ItemInde x].ToString();
SqlCommand com = new SqlCommand("upd ate category set name ='"+strCatName+ "' where id = "+strId,con );
con.Open();
com.ExecuteNonQ uery();
DataGrid1.EditI temIndex = -1;
BindGrid();
}
catch(Exception ex)
{
throw ex;
}
}

private void DataGrid1_Cance lCommand(object source, System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
{
DataGrid1.EditI temIndex = -1;
BindGrid();
}

private void DataGrid1_PageI ndexChanged(obj ect source, System.Web.UI.W ebControls.Data GridPageChanged EventArgs e)
{
DataGrid1.Curre ntPageIndex = e.NewPageIndex;
BindGrid();
This is an example of editing in DataGrid and Default Paging

http://www.hanusoftwar e.com

Html Design Code : -

<asp:DataGrid id="DataGrid1" DataKeyField="i d" runat="server" Height="224px" AutoGenerateCol umns="False" PageSize="5" AllowPaging="Tr ue">
<Columns>
<asp:BoundColum n Visible="False" DataField="id"
HeaderText="Cat egory Id"></asp:BoundColumn >
<asp:TemplateCo lumn HeaderText="Cat egory">
<ItemTemplate >
<asp:Label id=lblName text='<%# DataBinder.Eval (Container.Data Item,"name")%>' Runat="server">
</asp:Label>
</ItemTemplate>
<EditItemTempla te>
<asp:TextBox id=txtEdit Runat="server" Text='<%# DataBinder.Eval (Container.Data Item,"name")%>' >
</asp:TextBox>
</EditItemTemplat e>
</asp:TemplateCol umn>
<asp:EditComman dColumn ButtonType="Lin kButton" UpdateText="Upd ate" HeaderText="Edi t" CancelText="Can cel"
EditText="Edit" ></asp:EditCommand Column>
</Columns>
</asp:DataGrid>

Code (EditInDataGrid .aspx.cs) :

private void Page_Load(objec t sender, System.EventArg s e)
{
// Put user code to initialize the page here
if(!IsPostBack)
{

BindGrid();
}
}
private void DataGrid1_EditC ommand(object source, System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
{
DataGrid1.EditI temIndex = e.Item.ItemInde x;
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.DataS ource = objDS;
DataGrid1.DataB ind();
}
else
{
DataGrid1.DataS ource = null;
DataGrid1.DataB ind();

Response.Write( "No record found.");
}
}
catch(Exception ex)
{
throw ex;
}
}

private void DataGrid1_Updat eCommand(object source, System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
{
try
{
string strCatName = ((TextBox)e.Ite m.FindControl(" txtEdit")).Text ;
string strId = DataGrid1.DataK eys[e.Item.ItemInde x].ToString();
SqlCommand com = new SqlCommand("upd ate category set name ='"+strCatName+ "' where id = "+strId,con );
con.Open();
com.ExecuteNonQ uery();
DataGrid1.EditI temIndex = -1;
BindGrid();
}
catch(Exception ex)
{
throw ex;
}
}

private void DataGrid1_Cance lCommand(object source, System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
{
DataGrid1.EditI temIndex = -1;
BindGrid();
}

private void DataGrid1_PageI ndexChanged(obj ect source, System.Web.UI.W ebControls.Data GridPageChanged EventArgs e)
{
DataGrid1.Curre ntPageIndex = e.NewPageIndex;
BindGrid();

Software Development Company
Oct 4 '07 #1
0 1308

Sign in to post your reply or Sign up for a free account.

Similar topics

3
8689
by: Joseph D. DeJohn | last post by:
I am trying to get pagination working on a datagrid. Can anyone point me to a resource for help on this? I'm not sure if custom paging is the best option or not.
1
1987
by: Maziar Aflatoun | last post by:
Hi, I have a Datagrid with AllowPaing property set so that the two arrows for the next and previous set of rows are showing. This is working fine. Now is there a way to instead of the 2 default previous/next arrows to use images to represent this previous and next property of the DataGrid? Thank you Maziar A.
1
2352
by: Jeremy | last post by:
I want my gird to sort only the items on the current page when I click on a column header. I wrote a little test app, but when I sort it pulls in items from other pages and places them on the current page. i.e. If I have: IntegerValue StringValue CurrencyValue 0 Item 0 0 1 Item 1 1.23
2
4000
by: Ben | last post by:
Hi, I'd like to have a datagrid that has a dropdownlist in the pager control for setting the page size. I can get the control into the pager inside the datagrid itemcreated event by checking for ListItemType.Pager. The problem I'm having is subscribing to the selectedindex changed event. The datagrid doesn't even seem fire an itemcommand, but that's ok, just the selectedindex changed event would do... Any advice is greatly appreciated!
1
1414
by: Danny Ni | last post by:
Hi, We use ASP.Net datagrid paging a lot and we have been experiencing "Index out of bound" error messages when we click on rows of datagrid on differnet pages every now and then. The problems do not apear to be consistent, they could happen anywhere, anytime. We only use datagrid's default paging ability. That is, set AllowPaging property to true and write a handler for PageIndexChanged. Somebody told me this is datagrid's...
2
1652
by: Dave | last post by:
Hi, When binding a dataset to a DataGrid and paging 10 records at a time, is the entire dataset still encoded and sent to viewstate as well OR just the current page and what's visible to the user stored in the viewstate on the browser? I've added some excerpts from some articles so I'm not sure. Any other MS links verifying this would be great.
6
1815
by: Shawn | last post by:
Hi. I already have a datagrid where I'm using paging. I have a stored procedure that fills a temp table with 200-500 rows and then sends back 10 records at the time. When I go to page 2 the SP fills the temp table again and returns rows 10-19. The temp table is dropped after each call to the SP, so it has to be created and filled every time the user changes page in the datagrid. My question is this: Would it be more efficient to...
6
1533
by: coleenholley | last post by:
Hi All :-) I need to populate a table I created as a web form. Are there any links to show me how to do this? I CANNOT use a dtagrid for this, the table has to be laid out as follows: Header Row Header Row Static data Dynamic (read only) data <Break> Header Row Header Row Header Row Header Row
4
1070
by: Helen Trim | last post by:
I have a datagrid on a webform that will allow the user to edit data in a list. I have added the edit column in as a link, but when it is clicked nothing happens. Do I need to set something else in the Columns property or is it a property of the dataset that needs changing? Thanks Helen
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7403
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.