//源代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace DataGrid1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
    public class Form1 : System.Windows.Forms.Form
     {
      private System.Data.SqlClient.SqlConnection sqlConnection1;
      private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
      /// <summary>
      /// 必需的设计器变量。
      /// </summary>
      private System.ComponentModel.Container components = null;
      private System.Windows.Forms.DataGrid dataGrid1;
      private System.Windows.Forms.Button button1;
      private System.Windows.Forms.PictureBox pictureBox1;  
      private DataSet ds=new DataSet ();
      private MemoryStream pms;
      public Form1()
     {
     //
     // Windows 窗体设计器支持所必需的
    //
InitializeComponent();
    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
     }
    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
       {
if (components != null) 
{
      components.Dispose();
}
       }
       base.Dispose( disposing );
     }   #region Windows Form Designer generated code
   /// <summary>
   /// 设计器支持所需的方法 - 不要使用代码编辑器修改
   /// 此方法的内容。
   /// </summary>
   private void InitializeComponent()
   {
    this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
  this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
    this.dataGrid1 = new System.Windows.Forms.DataGrid();
    this.button1 = new System.Windows.Forms.Button();
    this.pictureBox1 = new System.Windows.Forms.PictureBox();
    this.SuspendLayout();
    // 
    // sqlConnection1
    // 
   this.sqlConnection1.ConnectionString = "data source=ANDY;initial catalog=Northwind;persist security info=False;user id=sa" +
";workstation id=ANDY;packet size=4096";
  this.sqlDataAdapter1.Connection=this.sqlConnection1;
   this.sqlDataAdapter1.SelectCommand = "SELECT EmployeeID, LastName, FirstName, Title, TitleOfCourtesy, BirthDate, HireDa" +
"te, Address, City, Region, PostalCode, Country, HomePhone, Extension, Photo, Not" +
"es, ReportsTo, PhotoPath FROM Employees";
;
    // 
    // dataGrid1
    // 
    this.dataGrid1.DataMember = "";
    this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
  this.dataGrid1.Location = new System.Drawing.Point(8, 8);
   this.dataGrid1.Name = "dataGrid1";
   this.dataGrid1.Size = new System.Drawing.Size(408, 256);
   this.dataGrid1.TabIndex = 0;
   // 
   // button1
   // 
   this.button1.Location = new System.Drawing.Point(440, 56);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(88, 24);
   this.button1.TabIndex = 2;
   this.button1.Text = "button1";
  this.button1.Click += new System.EventHandler(this.button1_Click);
  // 
  // pictureBox1
  // 
  this.pictureBox1.Location = new System.Drawing.Point(432, 112);
  this.pictureBox1.Name = "pictureBox1";
  this.pictureBox1.Size = new System.Drawing.Size(176, 144);
  this.pictureBox1.TabIndex = 3;
  this.pictureBox1.TabStop = false;
  // 
  // Form1
  // 
  this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  this.ClientSize = new System.Drawing.Size(616, 273);
  this.Controls.AddRange(new System.Windows.Forms.Control[] {   this.pictureBox1,
  this.button1,
  this.dataGrid1});
  this.Name = "Form1";
  this.Text = "Form1";
  this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);
}
#endregion  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
[STAThread]
 static void Main() 
 {
    Application.Run(new Form1());
  }  private void Form1_Load(object sender, System.EventArgs e)
  {
    this.sqlDataAdapter1 .Fill(ds,"Employees");
    this.dataGrid1.DataSource=ds;
    this.dataGrid1.DataMember="Employees";
  }
  private void button1_Click(object sender, System.EventArgs e)
   {
     byte[] buff;
     DataRow dr=ds.Tables [0].Rows[dataGrid1.CurrentCell.RowNumber];
     buff=(byte[])dr["Photo"];
     pms=new MemoryStream(buff);
     Image im=Image.FromStream(pms);
     this.pictureBox1.Image=im;
   }
 }
}

解决方案 »

  1.   

    感谢您使用微软产品。可以通过DataGrid控件对象的CurrentCellChanged事件来实现对Cell数据的验证。
    如下提供部分示例代码:
    其中IsValidValue()函数用来判断Cell中的数据是否符合要求,根据实际需要进行编写相应的代码。
    变量okToValidate用来限定当前Cell的改变,其初值为true,并且只有在dataGrid1_CurrentCellChanged()方法内okToValidate才可设置为false。如果IsValidValue返回true,则表示Cell数据符合条件,相应地改变当前Cell的位置。
    private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
    {
    newCurrentRow = dataGrid1.CurrentCell.RowNumber;
    newCurrentCol = dataGrid1.CurrentCell.ColumnNumber;
    string newText = dataGrid1[oldCurrentRow, oldCurrentCol].ToString();
    if( okToValidate && !IsValidValue(oldCurrentRow, oldCurrentCol, newText))
    {
    MessageBox.Show("Entry Error");
    okToValidate = false;
    dataGrid1.CurrentCell = new DataGridCell(oldCurrentRow, oldCurrentCol);
    okToValidate = true;
    }
    oldCurrentRow = newCurrentRow;
    oldCurrentCol = newCurrentCol;
    } — 微软全球技术中心 VB支持中心本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
    为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
      

  2.   

    感谢您使用微软产品。可以通过DataGrid控件对象的CurrentCellChanged事件来实现对Cell数据的验证。
    如下提供部分示例代码:
    其中IsValidValue()函数用来判断Cell中的数据是否符合要求,根据实际需要进行编写相应的代码。
    变量okToValidate用来限定当前Cell的改变,其初值为true,并且只有在dataGrid1_CurrentCellChanged()方法内okToValidate才可设置为false。如果IsValidValue返回true,则表示Cell数据符合条件,相应地改变当前Cell的位置。
    private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
    {
    newCurrentRow = dataGrid1.CurrentCell.RowNumber;
    newCurrentCol = dataGrid1.CurrentCell.ColumnNumber;
    string newText = dataGrid1[oldCurrentRow, oldCurrentCol].ToString();
    if( okToValidate && !IsValidValue(oldCurrentRow, oldCurrentCol, newText))
    {
    MessageBox.Show("Entry Error");
    okToValidate = false;
    dataGrid1.CurrentCell = new DataGridCell(oldCurrentRow, oldCurrentCol);
    okToValidate = true;
    }
    oldCurrentRow = newCurrentRow;
    oldCurrentCol = newCurrentCol;
    } — 微软全球技术中心 VB支持中心本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
    为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
      

  3.   

    感谢您使用微软产品。可以通过DataGrid控件对象的CurrentCellChanged事件来实现对Cell数据的验证。
    如下提供部分示例代码:
    其中IsValidValue()函数用来判断Cell中的数据是否符合要求,根据实际需要进行编写相应的代码。
    变量okToValidate用来限定当前Cell的改变,其初值为true,并且只有在dataGrid1_CurrentCellChanged()方法内okToValidate才可设置为false。如果IsValidValue返回true,则表示Cell数据符合条件,相应地改变当前Cell的位置。
    private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
    {
    newCurrentRow = dataGrid1.CurrentCell.RowNumber;
    newCurrentCol = dataGrid1.CurrentCell.ColumnNumber;
    string newText = dataGrid1[oldCurrentRow, oldCurrentCol].ToString();
    if( okToValidate && !IsValidValue(oldCurrentRow, oldCurrentCol, newText))
    {
    MessageBox.Show("Entry Error");
    okToValidate = false;
    dataGrid1.CurrentCell = new DataGridCell(oldCurrentRow, oldCurrentCol);
    okToValidate = true;
    }
    oldCurrentRow = newCurrentRow;
    oldCurrentCol = newCurrentCol;
    } — 微软全球技术中心 VB支持中心本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
    为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
      

  4.   

    微软大哥说的当然是对的,不过我还有一个办法,就是使用数据库自身的约束条件来做,优点是比较方便,缺点要执行SQL语句后才知道是否有错