Go4Expert

Go4Expert (http://www.go4expert.com/)
-   ASP (http://www.go4expert.com/forums/asp/)
-   -   example of editing in DataGrid and Default Paging (http://www.go4expert.com/forums/example-editing-datagrid-default-paging-t6673/)

hanusoft 4Oct2007 16:11

Example of editing in DataGrid and Default Paging
 
This is an example of editing in DataGrid and Default Paging


Html Design Code : -
HTML 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) :

Code: ASP

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



All times are GMT +5.5. The time now is 02:39.