GridView da Compute Islemleri

 

Merhaba arkadaslar bu makalemizde seçtigimiz iki Id no arasindaki satirlarin degerini  hesaplayacagiz. Ayrica bu araliktaki satirlarin arka plan rengini degistirecegiz. Bu islemin yapilmasini GridView in OnRowDataBound = "OnRowDataBound" kismina yazilacak bir kod ile saglayacagiz. Ayrica GridView nesnesinin  AutoGenerateColumns="False"  yapiniz.



 

 

 

 

 

 

 

 

 

 

Sekil 1

Default.aspx.cs

using System;

using System.Collections.Generic;

using System.Data;

using System.Data.SqlClient;

using System.Drawing;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        Label3.Text = "";

 

        if (!IsPostBack)

        {

            BindData();

        }

 

    }

 

    SqlConnection con;

    string constr;

    string sql;

    SqlCommand cmd;

    SqlDataAdapter da;

    DataSet ds;

    DataTable dt;

    int id1, id2;

    protected void BindData()

    {

 

        {

            constr = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\kategori.mdf;Integrated Security=True";

            sql = "select * from [dbo].[Table]";

            con = new SqlConnection(constr);

            con.Open();

            cmd = new SqlCommand(sql, con);

            da = new SqlDataAdapter(cmd);

            ds = new DataSet();

            da.Fill(ds);

            con.Close();

            GridView1.DataSource = ds;

            GridView1.DataBind();

        }

    }

 

 

  

 

    protected void Button1_Click(object sender, EventArgs e)

    {

        constr = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\kategori.mdf;Integrated Security=True";

        sql = "select * from [dbo].[Table]";

        con = new SqlConnection(constr);

 

        cmd = new SqlCommand(sql, con);

        da = new SqlDataAdapter(cmd);

      

        dt = new DataTable("Table");

        con.Open();

        da.Fill(dt);

 

        con.Close();

 

        GridView1.DataSource = dt;

        GridView1.DataBind();

 

        string tutar = dt.Compute("Sum(Fiyat)", "Id >=" + TextBox1.Text + "And " + "Id <=" + TextBox2.Text).ToString();

        Label3.Text = "Toplam Tutar " + "(Id1 =" + TextBox1.Text + "-" +   "Id2 = " + TextBox2.Text + " arasi ) = " + tutar.ToString();

    }

  

    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)

    {

        if (e.Row.RowType == DataControlRowType.DataRow)

        {

          

            TableCell cell1 = e.Row.Cells[0];

            TableCell cell2 = e.Row.Cells[1];

            TableCell cell3 = e.Row.Cells[2];

            TableCell cell4 = e.Row.Cells[3];

 

            int id = int.Parse(cell1.Text);

 

            id1 = Convert.ToInt32(TextBox1.Text);

            id2 = Convert.ToInt32(TextBox2.Text);

            if (id >= id1 && id <= id2)

            {

                cell1.BackColor = Color.AliceBlue;

                cell2.BackColor = Color.AliceBlue;

                cell3.BackColor = Color.AliceBlue;

                cell4.BackColor = Color.AliceBlue;

            }

            else

            {

                cell1.BackColor = Color.White;

                cell2.BackColor = Color.White;

                cell3.BackColor = Color.White;

                cell4.BackColor = Color.White;

            }

 

        }

    }

}

 

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"  AutoGenerateColumns="False" OnRowDataBound = "OnRowDataBound" runat="server" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" Font-Names="Tahoma" Font-Size="Large" GridLines="Horizontal">

                <Columns>

                   

                    <asp:BoundField DataField="id" ReadOnly="true" HeaderText="Id" />

                    <asp:BoundField DataField="Yazar" ReadOnly="true" HeaderText="Yazar" />

                    <asp:BoundField DataField="Kitap" ReadOnly="true" HeaderText="Kitap" />

                   <asp:BoundField DataField="Fiyat" ReadOnly="true" HeaderText="Fiyat" />

                </Columns>

                <FooterStyle BackColor="White" ForeColor="#333333" />

                <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />

                <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 />

            <asp:Label ID="Label1" runat="server" Text="Id1" Font-Names="Tahoma" Font-Size="Large"></asp:Label>

            <asp:TextBox ID="TextBox1" runat="server" Font-Names="Tahoma" Font-Size="Large"></asp:TextBox>

&nbsp;<asp:Label ID="Label2" runat="server" Font-Names="Tahoma" Font-Size="Large" Text="Id2"></asp:Label>

&nbsp;<asp:TextBox ID="TextBox2" runat="server" Font-Names="Tahoma" Font-Size="Large"></asp:TextBox>

            <br />

            <br />

            <asp:Label ID="Label3" runat="server" Font-Names="Tahoma" Font-Size="Large" Text="Label"></asp:Label>

            <br />

            <asp:Button ID="Button1" runat="server" Font-Names="Tahoma" Font-Size="Large" Height="41px" Text="Hesapla" Width="171px" OnClick="Button1_Click" />

            <br />

        </div>

    </form>

</body>

</html>

 

Bir makalenin daha sonuna geldik. Bir sonraki makalede görüsmek üzere. Bahadir SAHIN

About Bahadır Şahin

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Öne Çıkan Yayın

GridView da Seçili Satırı DetailsView da Göstermek

Merhaba arkadaşlar bu makalemizde GridView nesnesi ile birlikte DetailsView nesanesini birlikte kullanacağız. GridView da seçili satırın de...