By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,222 Members | 1,310 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,222 IT Pros & Developers. It's quick & easy.

example of editing in DataGrid and Default Paging

P: 4
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
Share this question for a faster answer!
Share on Google+

Post your reply

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