Merhaba arkadaslar bu makalemizde Xml dosyasina yeni bir kayit ekleyecegiz. Ayrica mevcut kayitlarda duzenleme yapacagiz. Secili kayitta silme islemini gerceklestirecegiz.
Sayfamiza GridView nesnesi ekliyoruz ve AutoGenerateColumns="False" yapiyoruz.
Sekil 1
Sekil 2
Sekil 3
Sekil 4
WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace aspnet_gridview_xml_read_edit_delete
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
bindData();
}
private void bindData()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("classical.xml"));
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bindData();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bindData();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int i = GridView1.Rows[e.RowIndex].DataItemIndex;
string adsoyad = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text;
string ogrno = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
string bolum = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3")).Text;
string sinif = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox4")).Text;
GridView1.EditIndex = -1;
bindData();
DataSet ds = (DataSet)GridView1.DataSource;
ds.Tables[0].Rows[i]["Id"] = adsoyad;
ds.Tables[0].Rows[i]["Author"] = ogrno;
ds.Tables[0].Rows[i]["Book"] = bolum;
ds.Tables[0].Rows[i]["Country"] = sinif;
ds.WriteXml(Server.MapPath("classical.xml"));
bindData();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
bindData();
DataSet ds = (DataSet)GridView1.DataSource;
ds.Tables[0].Rows[GridView1.Rows[e.RowIndex].DataItemIndex].Delete();
ds.WriteXml(Server.MapPath("classical.xml"));
bindData();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "insert")
{
string adsoyad = ((TextBox)GridView1.FooterRow.FindControl("txtId")).Text;
string ogrno = ((TextBox)GridView1.FooterRow.FindControl("txtAuthor")).Text;
string bolum = ((TextBox)GridView1.FooterRow.FindControl("txtBook")).Text;
string sinif = ((TextBox)GridView1.FooterRow.FindControl("txtCountry")).Text;
bindData();
DataSet ds = GridView1.DataSource as DataSet;
DataRow dr = ds.Tables[0].NewRow();
dr[0] = adsoyad;
dr[1] = ogrno;
dr[2] = bolum;
dr[3] = sinif;
ds.Tables[0].Rows.Add(dr);
ds.AcceptChanges();
ds.WriteXml(Server.MapPath("classical.xml"));
bindData();
}
}
}
}
WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="aspnet_gridview_xml_read_edit_delete.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDeleting="GridView1_RowDeleting"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCommand="GridView1_RowCommand"
ShowFooter="True" Width="779px"
OnRowCancelingEdit="GridView1_RowCancelingEdit" CellPadding="4"
ForeColor="#333333" GridLines="None" Height="415px">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Id">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Id") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtId" runat="server" Width="100"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Author">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Author") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAuthor" runat="server" Width="100"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Author") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Book">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Book") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtBook" runat="server" Width="100"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Book") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Country") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtCountry" runat="server" Width="100"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Country") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
Text="Güncelle"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
Text="İptal"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="btnInsert" runat="server" CommandName="insert">Ekle</asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
Text="Düzenle"></asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="120px" />
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" DeleteText="Sil" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</div>
</form>
</body>
</html>
Bir makalenin daha sonuna geldik. Bir sonraki makalede gorusmek uzere. Bahadir SAHIN
0 comments:
Yorum Gönder