| Welcome to Webforumz.com. |
|
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
|
|
|
Oct 4th, 2007, 13:41
|
#2 (permalink)
|
|
Section Manager - Website Critique
Join Date: May 2007
Location: inside the outside
Posts: 1,094
|
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.
|
|
|
| Thread Tools |
|
|
| 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
HTML code is Off
|
|
|
|
|
|