Merhaba arkadaşlar bu makalemizde GridView nesnesinde seçtiğimiz satırı güncelleyeceğiz. GridView1_SelectedIndexChanged kısmında seçili satırdaki verileri Textbox ta göstereceğiz. OnRowDataBound a yazacağımız küçük bir kod parçasıyla; seçili satırın arka plan rengini değiştireceğiz.
GridView in AutoGenerateColumns="false" ve ayrıca AutoGenerateSelectButton="True" yapınız.
Screenshot
Şekil 1
Şekil 2
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\kategori.mdf;Integrated Security=True");
SqlDataAdapter adp;
SqlCommand komut;
DataSet ds;
int k;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txt_ID.Text="0";
bindData();
}
}
protected void bindData()
{
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\kategori.mdf;Integrated Security=True");
{
DataTable dt = new DataTable();
con.Open();
SqlCommand cmd = new SqlCommand("select * from [dbo].[Table]", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
protected void GridView1_SelectedIndexChanged(object sender, GridViewSelectEventArgs e)
{
txt_ID.Text = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;
txt_Yazar.Text = GridView1.Rows[e.NewSelectedIndex].Cells[2].Text;
txt_Kitap.Text = GridView1.Rows[e.NewSelectedIndex].Cells[3].Text;
txt_Fiyat.Text = GridView1.Rows[e.NewSelectedIndex].Cells[4].Text;
}
protected void Button1_Click(object sender, EventArgs e)
{
// string ad;
//ad = txt_Yazar.Text;
k = Convert.ToInt32(txt_ID.Text);
/*Update komutuyla güncellestirmemizi yapıyoruz.
Set kısmından sonra güncellestirecegiz alan ve yeni degeri
belirtiliyor.*/
string guncelle = "update [dbo].[Table] set Yazar='" + txt_Yazar.Text + "',Kitap='" +
txt_Kitap.Text + "',Fiyat='" + txt_Fiyat.Text + "' where Id='" + k + "'";
//Burada Yazar Ad , Kitap ve Fiyat güncellestiriyoruz.
adp = new SqlDataAdapter(guncelle, con);
ds = new DataSet();
adp.Fill(ds);
Response.Write("Girilen Bilgiler Güncellendi");
bindData();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
k = Convert.ToInt32(txt_ID.Text);
if (e.Row.RowType.Equals(DataControlRowType.DataRow))
{
DataRowView drv = (DataRowView)e.Row.DataItem;
for (int i = 0; i < drv.DataView.Table.Columns.Count + 1; i++)
{
//Guncelledigimiz satirin arka plan rengini degistiriyoruz.
if ((int)DataBinder.Eval(e.Row.DataItem, "ID") == k)
e.Row.Cells[i].BackColor = System.Drawing.Color.Red;
}
}
}
}
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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" CellPadding="4" AutoGenerateSelectButton="True" OnSelectedIndexChanging="GridView1_SelectedIndexChanged" OnRowDataBound="GridView1_RowDataBound" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" GridLines="Horizontal">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id"/>
<asp:BoundField DataField="Yazar" HeaderText="Yazar"/>
<asp:BoundField DataField="Kitap" HeaderText="Kitap"/>
<asp:BoundField DataField="Fiyat" HeaderText="Fiyat"/>
</Columns>
<FooterStyle BackColor="White" ForeColor="#333333" />
<HeaderStyle BackColor="#336666" ForeColor="#ffffff" Font-Bold="True"/>
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#333333"/>
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#487575" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#275353" />
</asp:GridView>
<br />
<br />
<div style="border:1px solid black; width:336px;">
<table class="auto-style1">
<tr>
<td class="auto-style5">ID</td>
<td class="auto-style3">
<asp:TextBox ID="txt_ID" runat="server" ReadOnly="True"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">Yazar</td>
<td class="auto-style4">
<asp:TextBox ID="txt_Yazar" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style5">Kitap</td>
<td class="auto-style3">
<asp:TextBox ID="txt_Kitap" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">Fiyat</td>
<td class="auto-style4">
<asp:TextBox ID="txt_Fiyat" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style5"> </td>
<td class="auto-style3">
<asp:Button ID="Button1" runat="server" Text="Update" OnClick="Button1_Click" />
</td>
</tr>
</table>
</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