Merhaba arkadaşlar bu makalemizde GridView nesnesine çalışma esnasında textbox satır ekliyoruz. Eklediğimiz satırdaki textbox a girilen metini kaydediyoruz.Ayrıca GridView satırlarında güncelleme işlemini gerçekleştiriyoruz.
Tablomuzu Sql Managament Server kısmında, System Databases i genişletelim. Bu örnekte Master Database inde Programmability kısmında Stored Procedures e tablomuzu ekliyoruz.
CREATE PROCEDURE UserTbl
@UserId int=0,
@UserName varchar(50)=null,
@UserLastName varchar(50)=null,
@Mail varchar(50)=null,
@Status varchar(50)
AS
BEGIN
SET NOCOUNT ON;
--- Insert New Records
IF @status='INSERT'
BEGIN
INSERT INTO dbo.person(Name,Surname,Contact) VALUES(@UserName,@UserLastName,@Mail)
END
--- Select Records in Table
IF @Status='SELECT'
BEGIN
SELECT Id,Name,Surname,Contact FROM dbo.person
END
--- Update Records in Table
IF @Status='UPDATE'
BEGIN
UPDATE dbo.person SET Name=@UserName,Surname=@UserLastName,Contact=@Mail WHERE Id=@UserId
END
--- Delete Records from Table
IF @status='DELETE'
BEGIN
DELETE FROM dbo.person where Id=@UserId
END
SET NOCOUNT OFF
END
Sekil 1
Sekil 2
Sekil 3
WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
namespace gridview_textbox_insert
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindData();
}
}
protected void bindData()
{
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection("Data Source=sirius\\SQLEXPRESS02;Initial Catalog=master;User ID=sa;Password=2344;Integrated Security=true");
con.Open();
SqlCommand cmd = new SqlCommand("UserTbl", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Status", "SELECT");
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
gvDetails.DataSource = ds;
gvDetails.DataBind();
}
else
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
gvDetails.DataSource = ds;
gvDetails.DataBind();
int columncount = gvDetails.Rows[0].Cells.Count;
gvDetails.Rows[0].Cells.Clear();
gvDetails.Rows[0].Cells.Add(new TableCell());
gvDetails.Rows[0].Cells[0].ColumnSpan = columncount;
gvDetails.Rows[0].Cells[0].Text = "No Records Found. Kayit bulunamadi";
}
}
protected void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox txtname = (TextBox)gvDetails.FooterRow.FindControl("txtuname");
TextBox txtlname = (TextBox)gvDetails.FooterRow.FindControl("txtlname");
TextBox txtmail = (TextBox)gvDetails.FooterRow.FindControl("txtmail");
Users("INSERT", txtname.Text, txtlname.Text, txtmail.Text, 0);
}
}
protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e)
{
gvDetails.EditIndex = e.NewEditIndex;
bindData();
}
protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvDetails.EditIndex = -1;
bindData();
}
protected void gvDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvDetails.PageIndex = e.NewPageIndex;
bindData();
}
protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int UserId = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["Id"].ToString());
TextBox txtname = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtUserName");
TextBox txtlname = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtUserLastName");
TextBox txtmail = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtMail");
Users("UPDATE", txtname.Text, txtlname.Text, txtmail.Text, UserId);
}
protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int UserId = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["Id"].ToString());
string UserName = gvDetails.DataKeys[e.RowIndex].Values["Name"].ToString();
Users("DELETE", UserName, "","", UserId);
}
protected void Users(string status, string username, string userlastname, string mail, int userid)
{
using (SqlConnection con = new SqlConnection("Data Source=sirius\\SQLEXPRESS02;Initial Catalog=master;User ID=sa;Password=2344;Integrated Security=true"))
{
con.Open();
SqlCommand cmd = new SqlCommand("UserTbl", con);
cmd.CommandType = CommandType.StoredProcedure;
if (status == "INSERT")
{
cmd.Parameters.AddWithValue("@Status", status);
cmd.Parameters.AddWithValue("@UserName", username);
cmd.Parameters.AddWithValue("@UserLastName", userlastname);
cmd.Parameters.AddWithValue("@Mail", mail);
}
else if (status == "UPDATE")
{
cmd.Parameters.AddWithValue("@Status", status);
cmd.Parameters.AddWithValue("@UserName", username);
cmd.Parameters.AddWithValue("@UserLastName", userlastname);
cmd.Parameters.AddWithValue("@Mail", mail);
cmd.Parameters.AddWithValue("@UserId", userid);
}
else if (status == "DELETE")
{
cmd.Parameters.AddWithValue("@Status", status);
cmd.Parameters.AddWithValue("@UserId", userid);
}
cmd.ExecuteNonQuery();
lblresult.ForeColor = Color.Green;
lblresult.Text = username + " details " + status.ToLower() + " successfully" +Environment.NewLine +
username + " detaylari " + " basarili bir sekilde eklendi";
gvDetails.EditIndex = -1;
bindData();
}
}
}
}
WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="gridview_textbox_insert.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.GridviewDiv {font-size: 100%; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helevetica, sans-serif; color: #303933;}
.headerstyle
{
color:#FFFFFF;border-right-color:#abb079;border-bottom-color:#abb079;background-color: #0094ff;padding:0.5em 0.5em 0.5em 0.5em;text-align:center;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="GridviewDiv">
<asp:GridView runat="server" ID="gvDetails" ShowFooter="true" AllowPaging="true" PageSize="10" AutoGenerateColumns="false" DataKeyNames="Id,Name" OnPageIndexChanging="gvDetails_PageIndexChanging" OnRowCancelingEdit="gvDetails_RowCancelingEdit"
OnRowEditing="gvDetails_RowEditing" OnRowUpdating="gvDetails_RowUpdating" OnRowDeleting="gvDetails_RowDeleting" OnRowCommand ="gvDetails_RowCommand" >
<HeaderStyle CssClass="headerstyle" />
<Columns>
<asp:BoundField DataField="Id" HeaderText="User Id" ReadOnly="true" />
<asp:TemplateField HeaderText="User Name">
<ItemTemplate>
<asp:Label ID="lblUserName" runat="server" Text='<%# Eval("Name")%>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtUserName" runat="server" Text='<%# Eval("Name")%>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtuname" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="User Last Name">
<ItemTemplate>
<asp:Label ID="lblUserLastName" runat="server" Text='<%# Eval("Surname")%>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtUserLastName" runat="server" Text='<%# Eval("Surname")%>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtlname" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Mail">
<ItemTemplate>
<asp:Label ID="lblMail" runat="server" Text='<%# Eval("Contact")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtMail" runat="server" Text='<%# Eval("Contact")%>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtmail" runat="server" />
<asp:Button ID="btnAdd" CommandName="AddNew" runat="server" Text="Add" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ShowDeleteButton="true" />
</Columns>
</asp:GridView>
<asp:Label ID="lblresult" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN
0 comments:
Yorum Gönder