none
gridview 的 row 如何绑定相邻行的数据 RRS feed

  • 问题

  • 我有一个很独特的需求,就是希望gridview 只显示一个dataTable的奇数行(1,3,5。。。)但是希望偶数行的一个特定的列的值绑定到奇数行当中,就是跨行绑定数据。这个有什么非常简单的方法去做么。如果是当前行一般用EvalBind 函数。有没有什么方法直接bind current row -1行的数据阿,这样省去了自己写code的时间。
    铅笔小新
    2009年6月5日 3:02

答案

  • 你好,写个存储过程检索出你要的数据就不可以了吗?
    邹俊才
    2009年6月7日 16:46
    版主
  • 你好,这个可以使用 ItemDataBond 事件来做
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="X200906051109.aspx.cs" Inherits="Grid_X200906051109" %>
    <!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 runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            正常显示<br />
            <asp:GridView ID="GridView1" runat="server"></asp:GridView>
            EmployeeKey 交替显示<br />
            <asp:GridView DataKeyNames="EmployeeKey" ID="GridView2" runat="server" 
                onrowdatabound="GridView2_RowDataBound"></asp:GridView>
        </form>
    </body>
    </html>
    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    
    public partial class Grid_X200906051109 : System.Web.UI.Page
    {
        #region " Employee "
    
        private DataTable Employee
        {
            get
            {
                if (Session["Employee"] == null)
                {
                    DataTable fEmployee = new DataTable();
                    fEmployee.Columns.Add(new DataColumn("EmployeeKey", typeof(int))); // 员工 Id
                    fEmployee.Columns.Add(new DataColumn("Name", typeof(string))); // 员工名称
                    fEmployee.Columns.Add(new DataColumn("ModifyDate", typeof(string))); // 员工名称
    
                    fEmployee.Rows.Add(new object[] { 0, "X.X.Y", DateTime.Now });
                    fEmployee.Rows.Add(new object[] { 1, "Chole", DateTime.Now });
                    fEmployee.Rows.Add(new object[] { 2, "Tony", DateTime.Now });
                    fEmployee.Rows.Add(new object[] { 3, "John", DateTime.Now });
                    fEmployee.Rows.Add(new object[] { 4, "Chirs", DateTime.Now });
                    fEmployee.Rows.Add(new object[] { 5, "Li", DateTime.Now });
    
                    Session["Employee"] = fEmployee;
                }
    
                return (DataTable)Session["Employee"];
            }
        }
    
        #endregion
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.GridView1.DataSource = this.Employee;
                this.GridView2.DataSource = this.Employee;
                this.GridView1.DataBind();
                this.GridView2.DataBind();
            }
        }
        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow &&
                e.Row.RowIndex > 0 &&
                this.GridView2.Rows[e.Row.RowIndex - 1].RowType == DataControlRowType.DataRow)
            {
                foreach (DataRow fDataRow in this.Employee.Select(String.Format("EmployeeKey = {0}", this.GridView2.DataKeys[e.Row.RowIndex - 1].Value)))
                {
                    // 同时显示本行及上一行的 EmployeeKey
                    e.Row.Cells[0].Text = string.Format("{0} - {1}", e.Row.Cells[0].Text, fDataRow["EmployeeKey"]);
                }
            }
        }
    }
    
    


    知识改变命运,奋斗成就人生!
    2009年6月5日 3:30
    版主

全部回复

  • 你好,这个可以使用 ItemDataBond 事件来做
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="X200906051109.aspx.cs" Inherits="Grid_X200906051109" %>
    <!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 runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            正常显示<br />
            <asp:GridView ID="GridView1" runat="server"></asp:GridView>
            EmployeeKey 交替显示<br />
            <asp:GridView DataKeyNames="EmployeeKey" ID="GridView2" runat="server" 
                onrowdatabound="GridView2_RowDataBound"></asp:GridView>
        </form>
    </body>
    </html>
    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    
    public partial class Grid_X200906051109 : System.Web.UI.Page
    {
        #region " Employee "
    
        private DataTable Employee
        {
            get
            {
                if (Session["Employee"] == null)
                {
                    DataTable fEmployee = new DataTable();
                    fEmployee.Columns.Add(new DataColumn("EmployeeKey", typeof(int))); // 员工 Id
                    fEmployee.Columns.Add(new DataColumn("Name", typeof(string))); // 员工名称
                    fEmployee.Columns.Add(new DataColumn("ModifyDate", typeof(string))); // 员工名称
    
                    fEmployee.Rows.Add(new object[] { 0, "X.X.Y", DateTime.Now });
                    fEmployee.Rows.Add(new object[] { 1, "Chole", DateTime.Now });
                    fEmployee.Rows.Add(new object[] { 2, "Tony", DateTime.Now });
                    fEmployee.Rows.Add(new object[] { 3, "John", DateTime.Now });
                    fEmployee.Rows.Add(new object[] { 4, "Chirs", DateTime.Now });
                    fEmployee.Rows.Add(new object[] { 5, "Li", DateTime.Now });
    
                    Session["Employee"] = fEmployee;
                }
    
                return (DataTable)Session["Employee"];
            }
        }
    
        #endregion
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.GridView1.DataSource = this.Employee;
                this.GridView2.DataSource = this.Employee;
                this.GridView1.DataBind();
                this.GridView2.DataBind();
            }
        }
        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow &&
                e.Row.RowIndex > 0 &&
                this.GridView2.Rows[e.Row.RowIndex - 1].RowType == DataControlRowType.DataRow)
            {
                foreach (DataRow fDataRow in this.Employee.Select(String.Format("EmployeeKey = {0}", this.GridView2.DataKeys[e.Row.RowIndex - 1].Value)))
                {
                    // 同时显示本行及上一行的 EmployeeKey
                    e.Row.Cells[0].Text = string.Format("{0} - {1}", e.Row.Cells[0].Text, fDataRow["EmployeeKey"]);
                }
            }
        }
    }
    
    


    知识改变命运,奋斗成就人生!
    2009年6月5日 3:30
    版主
  • 使用DataList不是正合适吗?
    【孟子E章】
    2009年6月5日 4:51
    版主
  • 你好,写个存储过程检索出你要的数据就不可以了吗?
    邹俊才
    2009年6月7日 16:46
    版主