netInfo

学习的最好方式就是写下来,不积跬步,无以致千里

博客园 首页 新随笔 联系 订阅 管理

一个简单的例子
<%@ Page language="c#" Codebehind="DataGridselect.aspx.cs" AutoEventWireup="false" Inherits="WebApplication3.DataGridselect" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm1</title>
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <table style="Z-INDEX: 101; LEFT: 64px; WIDTH: 640px; POSITION: absolute; TOP: 32px">
    <tr>
     <td style="HEIGHT: 22px"></FONT></td>
    </tr>
    <tr>
     <td><asp:datagrid id="DataGrid1" runat="server" Width="656px" PagerStyle-HorizontalAlign="Right" AllowPaging="True"
       DataKeyField="employeeid" ShowFooter="True" AutoGenerateColumns="False" Height="176px" OnItemCommand="DataGrid1_ItemCommand">
       <Columns>
        <asp:BoundColumn DataField="employeeid" ReadOnly="True" HeaderText="employeeid"></asp:BoundColumn>
        <asp:BoundColumn DataField="lastname" HeaderText="lastname"></asp:BoundColumn>
        <asp:BoundColumn DataField="firstname" HeaderText="fistname"></asp:BoundColumn>
        <asp:BoundColumn DataField="city" HeaderText="city"></asp:BoundColumn>
        <asp:ButtonColumn Text="选择" CommandName="Select"></asp:ButtonColumn>
       </Columns>
       <PagerStyle HorizontalAlign="Right"></PagerStyle>
      </asp:datagrid></td>
    </tr>
   </table>
  </form>
 </body>
</HTML>

DataGridselect.aspx.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;
using System.Data.SqlClient;

namespace WebApplication3
{
 /// <summary>
 /// DataGridselect 的摘要说明。
 /// </summary>
 public class DataGridselect : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.DataGrid DataGrid1;
  SqlConnection conn=null;
  DataSet ds=new DataSet();

  private const String connString="DataBase=northwind;server=localhost;uid=sa;pwd=;";
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   conn=new SqlConnection(connString);
   conn.Open ();
   SqlDataAdapter adapter=new SqlDataAdapter("select employeeid,lastname,firstname,city from employees",conn);
   
   adapter.Fill(ds,"employees");

   DataGrid1.DataSource =ds;
   DataGrid1.DataBind();
 
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  protected void DataGrid1_ItemCommand(object sender,DataGridCommandEventArgs e)
  {
   if (e.CommandName=="Select")
   {
//得到所有的行列数据
    foreach(DataRow myrow in ds.Tables[0].Rows)
    {
     foreach(DataColumn mycolumn in ds.Tables["employees"].Columns)
     {
      Response.Write(myrow[mycolumn]+" ");
     }
    }
 //得到当前datagrid的行列数据   
    for (int i=0;i<this.DataGrid1.Columns.Count-1;i++)
    {
     Response.Write(ds.Tables[0].Rows[e.Item.ItemIndex][i]+" ");
    }

   } 
  }
 }
}

posted on 2005-09-13 13:01  边城浪子  阅读(2687)  评论(1编辑  收藏  举报