469,930 Members | 2,092 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,930 developers. It's quick & easy.

example of editing in DataGrid and Default Paging

This is an example of editing in DataGrid and Default Paging

http://www.hanusoftware.com

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();
This is an example of editing in DataGrid and Default Paging

http://www.hanusoftware.com

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();

Software Development Company
Oct 4 '07 #1
0 1160

Post your reply

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

Similar topics

3 posts views Thread by Joseph D. DeJohn | last post: by
1 post views Thread by Maziar Aflatoun | last post: by
1 post views Thread by Jeremy | last post: by
2 posts views Thread by Ben | last post: by
1 post views Thread by Danny Ni | last post: by
2 posts views Thread by Dave | last post: by
6 posts views Thread by Shawn | last post: by
6 posts views Thread by coleenholley | last post: by
4 posts views Thread by Helen Trim | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.