simple gridview Edit,Update,Delete in asp.net
aspx:
<div align="center">
<asp:GridView ID="grdRegView" runat="server"></asp:GridView>
</div>
AutoFormat -- >:
Select Color Theme
HeaderText and Data TextField : Edit Update Delete Command Select:
aspx:
<asp:GridView ID="grdRegView" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px"
CellPadding="4" GridLines="Horizontal"
onrowediting="grdRegView_RowEditing" // Row Editing
onrowcancelingedit="grdRegView_RowCancelingEdit" // Row CancelEdit
onrowupdating="grdRegView_RowUpdating" AllowPaging="True" //Row Update
onpageindexchanging="grdRegView_PageIndexChanging" // Page Indexing
onrowdeleting="grdRegView_RowDeleting" //Row Deleting
PageSize="5" //Page Size
>
<RowStyle BackColor="White" ForeColor="#333333" />
<Columns>
<asp:TemplateField HeaderText="ID">
<%-- <EditItemTemplate>
<asp:TextBox ID="txtID" runat="server" Text='<%# Bind("ID") %>'></asp:TextBox>
</EditItemTemplate>--%>
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age">
<EditItemTemplate>
<asp:TextBox ID="txtAge" runat="server" Text='<%# Bind("Age") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblAge" runat="server" Text='<%# Bind("Age") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Salary">
<EditItemTemplate>
<asp:TextBox ID="txtSalary" runat="server" Text='<%# Bind("Salary") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblSalary" runat="server" Text='<%# Bind("Salary") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
// Edit
<asp:CommandField HeaderText="Edit" ShowEditButton="True" ShowHeader="True" >
<ControlStyle BorderColor="#FF3300" BorderStyle="Dashed"
BorderWidth="1px" Font-Italic="True" />
</asp:CommandField>
// Delete
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True"
ShowHeader="True" >
<ControlStyle BorderColor="Red" BorderStyle="Dashed" BorderWidth="1px"
Font-Italic="True" />
</asp:CommandField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#333333" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
</asp:GridView>
aspx:cs :
SqlConnection Conn = new SqlConnection(ConfigurationManager.AppSettings["connections"]);
SqlCommand Cmd;
SqlDataAdapter da;
DataSet ds = new DataSet();
DataTable dt = new DataTable();
string select,Update,Delete;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
View();
}
}
public void View()
{
select = "Select * From Register";
da = new SqlDataAdapter(select, Conn);
da.Fill(ds);
grdRegView.DataSource = ds;
grdRegView.DataBind();
}
Edit Update Delete Cancel in GridView :
Edit Data--- > :
protected void grdRegView_RowEditing(object sender,
GridViewEditEventArgs e)
{
grdRegView.EditIndex = e.NewEditIndex; // Edit Click Update and Cancel
View();
}
Update Data--- > :Update Value Text box Type Then Update are Cancel in Value
protected void grdRegView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label lblID = (Label)grdRegView.Rows[e.RowIndex].FindControl("lblID");
// lblID Value are Get Use Find Control
TextBox txtName = (TextBox)grdRegView.Rows[e.RowIndex].FindControl("txtName");
// Textbox Value are Get
TextBox txtAge = (TextBox)grdRegView.Rows[e.RowIndex].FindControl("txtAge");
TextBox txtSalary = (TextBox)grdRegView.Rows[e.RowIndex].FindControl("txtSalary");
//Update The Value
Update = "Update Register Set Name='"+txtName.Text+"',
Age='"+txtAge.Text+"',Salary='"+txtSalary.Text+"' where ID='"+lblID.Text+"'";
Cmd = new SqlCommand(Update, Conn);
Conn.Open();
Cmd.ExecuteNonQuery();
Conn.Close();
View();
}
Cancel Click --- >
protected void grdRegView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grdRegView.EditIndex = -1; // Cancel Edit in -1
View();
}
Delete The Data--- >
protected void grdRegView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Label lblID = (Label)grdRegView.Rows[e.RowIndex].FindControl("lblID");
// lblID Value are Get Use Find Control
Delete = "Delete From Register Where ID='"+lblID.Text+"'";
Cmd = new SqlCommand(Delete , Conn);
Conn.Open();
Cmd.ExecuteNonQuery();
Conn.Close();
View();
}
Paging : F4 Click Allow Paging True and Page Size Fix
protected void grdRegView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdRegView.PageIndex = e.NewPageIndex; //Page Index
View();
}
No comments:
Post a Comment