★★★★★FireGrass★★★★★

※※※※※ΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞジ
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

任意位置选择DataGrid的行

Posted on 2006-08-17 13:45  火草  阅读(299)  评论(0编辑  收藏  举报
在DataGrid中要选种某一行必须要设置个选择列,下面介绍的方法可能在某些情况下有用

.aspx代码:

<%@ Page language="c#" Codebehind="WhList.aspx.cs" AutoEventWireup="false" Inherits="Logease.CarryOn.Web.airIn.bussOperation.WhList" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 
<HEAD>
               
<title>WhList</title>
<meta content="Microsoft Visual Studio .NET 7.1" name=GENERATOR>
<meta content=C# name=CODE_LANGUAGE>
<meta content=JavaScript name=vs_defaultClientScript>
<meta content=http://schemas.microsoft.com/intellisense/ie5 name=vs_targetSchema><base target=_self>
<meta http-equiv=pragma content=no-cache><LINK href="../../Styles/green.css" type=text/css rel=stylesheet >
<script lang=javascript>
               var returnValues; 
//回车返回值
               var backColors;   //目标行的背景色
               var colors;            //目标行的前景色
               var chooseRow;  //前次选中行
               
//双击返回
               function coporationinfo(wh_no,warehouse_no)
               
{
                       
try
                       
{
                               var d
=dialogArguments;
                               d.document.all(
"hid_wh_no").value = wh_no;
                               d.document.all(
"hid_warehouse_no").value = warehouse_no;
                       }

                       
catch(e)
                       
{
                               alert(
"操作错误,无法返回查询结果!");
                       }

                       window.close();
                       
return false;
               }

               
//确认返回
               function doClick(str)
               
{
                       var obj_wh_no 
= document.getElementById("hid_returnwh_no");
                       var obj_warehouse_no 
= document.getElementById("hid_returnwarehouse_no");
                       
if (obj_wh_no.value != "" && obj_warehouse_no.value != "")
                               coporationinfo(obj_wh_no.value,obj_warehouse_no.value);
                       
else
                               alert(
"未选择");
                       
return false;
               }

               function initRow()
{
                       
if (chooseRow == null){
                               chooseRow 
= document.getElementById("DataGrid1").rows[1];
                               
if (chooseRow.cells[1!= null && chooseRow.cells[2!= null){
                                      document.getElementById(
"hid_returnwh_no").value = chooseRow.cells[1].innerText;
                                      document.getElementById(
"hid_returnwarehouse_no").value = chooseRow.cells[2].innerText;
                               }

                               
else
                                      document.getElementById(
"btn_cancel").focus();
                       }

               }

               
//回车事件
               function enterControl(){
                       
if (event.keyCode == 13){
                               
if(event.srcElement.name != "btn_cancel")
                                      document.getElementById(
"btn_Submit").click();
                               
else
                                      document.getElementById(
"btn_cancel").click();
                       }

                       
if(event.keyCode==38)//方向键向上
                       }

                       
if(event.keyCode==40)//方向键向下
                       }

                       
if(event.keyCode==37)//方向键向左
                               document.getElementById("btn_Submit").focus();
                       }

                       
if(event.keyCode==39)//方向键向右
                               document.getElementById("btn_cancel").focus();
                       }

               }

               
//设置原有色
               function setColor(backColor,color)
               
{
                       backColors 
= backColor;
                       colors 
= color;
               }

               function setClickColor(obj,wh_no,warehouse_no)
{
                       
if (chooseRow != null){
                               var index 
= chooseRow.rowIndex;
                               var rows 
= chooseRow.parentElement.rows;
                               var color1 
= rows[index - 1].style.backgroundColor;
                               var color2 
= rows[index + 1].style.backgroundColor;
                               var row 
= rows[index];
                               
if (color1 == "" || color1 == "#ffffff" || color2 == "" || color2 == "#ffffff"){
                                      row.style.backgroundColor 
= "#f5faf5";
                                      row.color
="blank";
                               }

                               
if (color1 == "#f5faf5" || color2 == "#f5faf5"){
                                      row.style.backgroundColor 
= "#ffffff";
                                      row.color
="blank";
                               }

                       }

                       chooseRow 
= obj;
                       document.getElementById(
"hid_returnwh_no").value = wh_no;
                       document.getElementById(
"hid_returnwarehouse_no").value = warehouse_no;
               }

               function doCancel()
               
{
                       window.close();
                       
return false;
               }

               
</script>
</HEAD>
<body onkeydown=enterControl() onload=initRow() 
MS_POSITIONING
="GridLayout">
<form id=Form1 method=post runat="server">
<table width=800 align=center>
 
<tr>
    
<td><asp:datagrid id=DataGrid1 runat="server" AutoGenerateColumns="False" AllowPaging="True" Width="784px" CssClass="DataGrid">
        
<SelectedItemStyle CssClass="SelectItemStyle" BackColor="#15B77E"></SelectedItemStyle>
        
<EditItemStyle CssClass="EditItemStyle"></EditItemStyle>
        
<AlternatingItemStyle CssClass="AlterItemStyle"></AlternatingItemStyle>
        
<ItemStyle CssClass="ItemStyle"></ItemStyle>
        
<HeaderStyle CssClass="HeaderStyle"></HeaderStyle>
        
<Columns>
               
<asp:BoundColumn DataField="rownum" HeaderText="序号"></asp:BoundColumn>
               
<asp:BoundColumn DataField="wh_no" HeaderText="单证编号"></asp:BoundColumn>
               
<asp:BoundColumn DataField="warehouse_no" HeaderText="本仓编号"></asp:BoundColumn>
               
<asp:BoundColumn DataField="operation_id" HeaderText="平台编号"></asp:BoundColumn>
               
<asp:BoundColumn DataField="customer_to_name" HeaderText="客户"></asp:BoundColumn>
               
<asp:BoundColumn DataField="MBL" HeaderText="总单"></asp:BoundColumn>
               
<asp:BoundColumn DataField="hbl" HeaderText="分单"></asp:BoundColumn>
               
<asp:BoundColumn DataField="pieces" HeaderText="件数"></asp:BoundColumn>
               
<asp:BoundColumn DataField="weight" HeaderText="重量"></asp:BoundColumn>
        
</Columns>
        
<PagerStyle NextPageText="下一页" PrevPageText="上一页" CssClass="PagerStyle" Mode="NumericPages"></PagerStyle>
        
</asp:datagrid></TD></TR>
 
<tr>
    
<td align=right><INPUT id=hid_returnwh_no type=hidden 
       runat
="server"><INPUT id=hid_returnwarehouse_no 
      type
=hidden runat="server"> <asp:button id=btn_Submit runat="server" Text="确认"></asp:Button>&nbsp;&nbsp; 
<asp:button id=btn_cancel Text="取消" Runat="server"></asp:button>&nbsp;&nbsp; 
    
</TD></TR></TABLE></FORM>
        
</body>
</HTML>

.cs代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
 
namespace Logease.CarryOn.Web.airIn.bussOperation
{
        
/// <summary>
        
/// WhList 的摘要说明。
        
/// </summary>

        public class WhList : System.Web.UI.Page
        
{
               
protected System.Web.UI.WebControls.DataGrid DataGrid1;
               
protected System.Web.UI.HtmlControls.HtmlInputHidden hid_returnwh_no;
               
protected System.Web.UI.HtmlControls.HtmlInputHidden hid_returnwarehouse_no;
               
protected System.Web.UI.WebControls.Button btn_Submit;
               
protected System.Web.UI.WebControls.Button btn_cancel;
        
               
private void Page_Load(object sender, System.EventArgs e)
               
{
                       
// 在此处放置用户代码以初始化页面
                       if(!IsPostBack)
                       
{
                               
this.btn_Submit.Attributes.Add("onclick","return doClick();");
                               
this.btn_cancel.Attributes.Add("onclick","return doCancel();");
                               
this.initBindData();
                       }

               }

 
               
Web 窗体设计器生成的代码
 
               
绑定表格数据 
 
               
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
               
{
                       
string ser_no = e.Item.Cells[0].Text;
                       
try
                       
{
                               
if (ser_no != null && ser_no != "序号")
                               
{
                                      e.Item.Attributes.Add(
"Ondblclick""javascript:coporationinfo('" + e.Item.Cells[1].Text + "','" + e.Item.Cells[2].Text + "')"); 
                                      e.Item.Attributes.Add(
"Onclick""setClickColor(this,'" + e.Item.Cells[1].Text + "','" + e.Item.Cells[2].Text + "');this.style.backgroundColor='#15B77E';this.style.color='white';this.style.cursor='hand';setColor(this.style.backgroundColor,this.style.color);"); 
                                      e.Item.Attributes.Add(
"OnMouseOver""setColor(this.style.backgroundColor,this.style.color);this.style.backgroundColor='#15B77E';this.style.color='white';this.style.cursor='hand';") ;
                                      e.Item.Attributes.Add(
"OnMouseOut""this.style.backgroundColor=backColors;this.style.color=colors") ;
                               }

                       }

                       
catch
                       
{}
               }

 
               
确认事件
 
               
取消事件
 
               
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
               
{
                       
this.DataGrid1.DataSource = (DataTable)ViewState["ASNSource"];
                       
this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
                       
this.DataGrid1.DataBind();
               }

        }

}
 

这个方法有一定的局限性,首先你在后台没办法直接通过DataGrid获取行了,其次如果DataGrid一多..........后面就不用说,不知道谁有更好的方法或想法.