none
gridview RRS feed

Yanıtlar

  • Merhaba

    Aşağıdaki bilgilerin işinize yarayabileceğini düşündüm.

    Buradan örnek projeyi indirebilir, buradan da konuyu daha detaylı inceleyebilirsiniz.

    istediğinizin karşılığı gibi duruyor.

    iyi çalışmalar.

    aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head id="Head1" runat="server">

        <title>GridView Row Selection</title>

        <script type="text/javascript">

            var SelectedRow = null;

            var SelectedRowIndex = null;

            var UpperBound = null;

            var LowerBound = null;

            window.onload = function () {

                UpperBound = parseInt('<%= this.gridView.Rows.Count %>') - 1;

                LowerBound = 0;

                SelectedRowIndex = -1;

            }

            function SelectRow(CurrentRow, RowIndex) {

                if (SelectedRow == CurrentRow || RowIndex > UpperBound || RowIndex < LowerBound) return;

                if (SelectedRow != null) {

                    SelectedRow.style.backgroundColor = SelectedRow.originalBackgroundColor;

                    SelectedRow.style.color = SelectedRow.originalForeColor;

                }

                if (CurrentRow != null) {

                    CurrentRow.originalBackgroundColor = CurrentRow.style.backgroundColor;

                    CurrentRow.originalForeColor = CurrentRow.style.color;

                    CurrentRow.style.backgroundColor = '#DCFC5C';

                    CurrentRow.style.color = 'Black';

                }

                SelectedRow = CurrentRow;

                SelectedRowIndex = RowIndex;

                setTimeout("SelectedRow.focus();", 0);

            }

            function SelectSibling(e) {

                var e = e ? e : window.event;

                var KeyCode = e.which ? e.which : e.keyCode;

                if (KeyCode == 40)

                    SelectRow(SelectedRow.nextSibling, SelectedRowIndex + 1);

                else if (KeyCode == 38)

                    SelectRow(SelectedRow.previousSibling, SelectedRowIndex - 1);

                return false;

            }

        </script>

    </head>

    <body>

        <form id="frmGridView" runat="server">

            <asp:GridView ID="gridView" runat="server" AutoGenerateColumns="False" OnRowCreated="gridView_RowCreated"

                TabIndex="0" GridLines="Horizontal">

                <Columns>

                    <asp:BoundField HeaderText="S#" DataField="sno">

                        <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />

                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />

                    </asp:BoundField>

                    <asp:BoundField HeaderText="Random No" DataField="rndno">

                        <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="150px" />

                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="150px" />

                    </asp:BoundField>

                    <asp:BoundField HeaderText="Date" DataField="date">

                        <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="100px" />

                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="100px" />

                    </asp:BoundField>

                    <asp:BoundField HeaderText="Time" DataField="time">

                        <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="100px" />

                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="100px" />

                    </asp:BoundField>

                </Columns>

                <RowStyle BackColor="#FFE0C0" />

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

                <AlternatingRowStyle BackColor="#FFC080" />

            </asp:GridView>

        </form>

    </body>

    </html>

    cs:

    using System;

    using System.Data;

    using System.Configuration;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Web.UI.HtmlControls;

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

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                gridView.DataSource = GetDataSource();

                gridView.DataBind();

            }

        }

        private DataTable GetDataSource()

        {

            DataTable dTable = new DataTable();

            DataRow dRow = null;

            Random rnd = new Random();

            dTable.Columns.Add("sno");

            dTable.Columns.Add("rndno");

            dTable.Columns.Add("date");

            dTable.Columns.Add("time");

            for (int n = 0; n < 10; ++n)

            {

                dRow = dTable.NewRow();

                dRow["sno"] = n + ".";

                dRow["rndno"] = rnd.Next().ToString();

                dRow["date"] = DateTime.Now.ToShortDateString();

                dRow["time"] = DateTime.Now.ToShortTimeString();

                dTable.Rows.Add(dRow);

                dTable.AcceptChanges();

            }

            return dTable;

        }

        protected void gridView_RowCreated(object sender, GridViewRowEventArgs e)

        {

            if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))

            {

                e.Row.TabIndex = -1;

                e.Row.Attributes["onclick"] = string.Format("javascript:SelectRow(this, {0});", e.Row.RowIndex);

                e.Row.Attributes["onkeydown"] = "javascript:return SelectSibling(event);";

                e.Row.Attributes["onselectstart"] = "javascript:return false;";

            }

        }

    }


    levent turgut

    14 Mayıs 2012 Pazartesi 22:13

Tüm Yanıtlar

  • Merhaba

    Aşağıdaki bilgilerin işinize yarayabileceğini düşündüm.

    Buradan örnek projeyi indirebilir, buradan da konuyu daha detaylı inceleyebilirsiniz.

    istediğinizin karşılığı gibi duruyor.

    iyi çalışmalar.

    aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head id="Head1" runat="server">

        <title>GridView Row Selection</title>

        <script type="text/javascript">

            var SelectedRow = null;

            var SelectedRowIndex = null;

            var UpperBound = null;

            var LowerBound = null;

            window.onload = function () {

                UpperBound = parseInt('<%= this.gridView.Rows.Count %>') - 1;

                LowerBound = 0;

                SelectedRowIndex = -1;

            }

            function SelectRow(CurrentRow, RowIndex) {

                if (SelectedRow == CurrentRow || RowIndex > UpperBound || RowIndex < LowerBound) return;

                if (SelectedRow != null) {

                    SelectedRow.style.backgroundColor = SelectedRow.originalBackgroundColor;

                    SelectedRow.style.color = SelectedRow.originalForeColor;

                }

                if (CurrentRow != null) {

                    CurrentRow.originalBackgroundColor = CurrentRow.style.backgroundColor;

                    CurrentRow.originalForeColor = CurrentRow.style.color;

                    CurrentRow.style.backgroundColor = '#DCFC5C';

                    CurrentRow.style.color = 'Black';

                }

                SelectedRow = CurrentRow;

                SelectedRowIndex = RowIndex;

                setTimeout("SelectedRow.focus();", 0);

            }

            function SelectSibling(e) {

                var e = e ? e : window.event;

                var KeyCode = e.which ? e.which : e.keyCode;

                if (KeyCode == 40)

                    SelectRow(SelectedRow.nextSibling, SelectedRowIndex + 1);

                else if (KeyCode == 38)

                    SelectRow(SelectedRow.previousSibling, SelectedRowIndex - 1);

                return false;

            }

        </script>

    </head>

    <body>

        <form id="frmGridView" runat="server">

            <asp:GridView ID="gridView" runat="server" AutoGenerateColumns="False" OnRowCreated="gridView_RowCreated"

                TabIndex="0" GridLines="Horizontal">

                <Columns>

                    <asp:BoundField HeaderText="S#" DataField="sno">

                        <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />

                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />

                    </asp:BoundField>

                    <asp:BoundField HeaderText="Random No" DataField="rndno">

                        <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="150px" />

                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="150px" />

                    </asp:BoundField>

                    <asp:BoundField HeaderText="Date" DataField="date">

                        <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="100px" />

                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="100px" />

                    </asp:BoundField>

                    <asp:BoundField HeaderText="Time" DataField="time">

                        <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="100px" />

                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="100px" />

                    </asp:BoundField>

                </Columns>

                <RowStyle BackColor="#FFE0C0" />

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

                <AlternatingRowStyle BackColor="#FFC080" />

            </asp:GridView>

        </form>

    </body>

    </html>

    cs:

    using System;

    using System.Data;

    using System.Configuration;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Web.UI.HtmlControls;

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

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                gridView.DataSource = GetDataSource();

                gridView.DataBind();

            }

        }

        private DataTable GetDataSource()

        {

            DataTable dTable = new DataTable();

            DataRow dRow = null;

            Random rnd = new Random();

            dTable.Columns.Add("sno");

            dTable.Columns.Add("rndno");

            dTable.Columns.Add("date");

            dTable.Columns.Add("time");

            for (int n = 0; n < 10; ++n)

            {

                dRow = dTable.NewRow();

                dRow["sno"] = n + ".";

                dRow["rndno"] = rnd.Next().ToString();

                dRow["date"] = DateTime.Now.ToShortDateString();

                dRow["time"] = DateTime.Now.ToShortTimeString();

                dTable.Rows.Add(dRow);

                dTable.AcceptChanges();

            }

            return dTable;

        }

        protected void gridView_RowCreated(object sender, GridViewRowEventArgs e)

        {

            if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))

            {

                e.Row.TabIndex = -1;

                e.Row.Attributes["onclick"] = string.Format("javascript:SelectRow(this, {0});", e.Row.RowIndex);

                e.Row.Attributes["onkeydown"] = "javascript:return SelectSibling(event);";

                e.Row.Attributes["onselectstart"] = "javascript:return false;";

            }

        }

    }


    levent turgut

    14 Mayıs 2012 Pazartesi 22:13
  • Mustafa Bey selamlar,

    Yukarıdaki öneri faydalı oldu mu acaba?

    Kolay gelsin.


    Microsoft bu servisi kullanıcılarına yardım etme, Microsoft ürünleri ve teknolojileriyle ilgili bilgi bankasını genişletme amacıyla ücretsiz sunmaktadır.
    Bu içerik olduğu gibi benim tarafımdan hazırlanmış olup Microsoft tarafından herhangi bir sorumluluk üstlenildiği anlamına gelmez.
    Facebook Üzerinden Takip Et!
    Twitter'da Takip Et!

    18 Mayıs 2012 Cuma 09:55
    Moderatör