Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more

Go Back   Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more > Web Programming > Other Programming - Perl, C++, Java, ASP, .NET Development
User Name
Password

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-08-2007, 01:19 AM   #1 (permalink)
hanusoft
Junior Member
 
Join Date: Sep 2007
Posts: 11
Default example of editing in DataGrid and Default Paging

This is an example of editing in DataGrid and Default Paging

Offshore Software Product Development


Html Design Code : -

<aspataGrid 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>
</aspataGrid>

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;
4:22 PM 10/6/20074:22 PM 10/6/2007 }

private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEvent Args e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
BindGrid();
__________________

Software Development Company
hanusoft is offline   Reply With Quote
Sponsored Links
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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

Points Per Thread View: 1.00
Points Per Thread: 11.00
Points Per Reply: 5.00



» Sponsors

» Links

» Affiliates
Web Hosting
Marketing Find
Merchant Select
SiteMap Builder
Host Compare
Dedicated Servers

» Links

» Sports Network
Paintball Forum
Football Forum
Hockey Forum
Golf Forum
Boxing Forum
Lacrosse Forum
Baseball Forum
SnowBoarding Forum
Soccer Forum
MMA Forum


All times are GMT -4. The time now is 12:55 PM.



LinkBacks Enabled by vBSEO 3.0.0 RC8
Webmaster Forums