5.21 How can I tell if the current row has changed and whether I am on the AddNew row or not?     The DataGrid's CurrentCellChanged event is hit even if you just change cells in the current row. If you want an event that is only hit when you change rows, then you have to look at the binding manager. This object has both a CurrentChanged event and a PositionChanged event which are hit when you change rows.To decide whether you are on the AddNew row or not, you can again use the binding manager and compare the number of rows it returns with the number of rows in your data table. Below is some code snippets showing how you might get at this information. 
 
private System.Windows.Forms.DataGrid dataGrid1; 
 
private BindingManagerBase bindingManager; 
  
private void Form1_Load(object sender, System.EventArgs e) 
 

 
     // Creating connection and command sting 
 
     string conStr = @"Provider=Microsoft.JET.OLEDB.4.0;data source=C:\northwind.mdb"; 
 
     string sqlStr = "SELECT * FROM Employees"; 
 
     // Create connection object 
 
     OleDbConnection conn = new OleDbConnection(conStr); 
 
     // Create data adapter object 
 
     OleDbDataAdapter da = new OleDbDataAdapter(sqlStr,conn); 
  
     // Create a dataset object and fill with data using data adapter's Fill method 
 
     DataSet ds = new DataSet(); 
 
     da.Fill(ds, "Employees"); 
 
     dataGrid1.DataSource = ds.Tables["Employees"]; 
 
     bindingManager = this.BindingContext[dataGrid1.DataSource]; 
 
     bindingManager.PositionChanged += new System.EventHandler(RowChanged); 
 

  
private void RowChanged(object sender, System.EventArgs e) 
 

 
     Console.WriteLine("RowChanged " + bindingManager.Position.ToString() ); 
 
     bool lastRow = bindingManager.Count > ((DataTable)dataGrid1.DataSource).Rows.Count; 
 
      
 
     if(lastRow) 
 
          Console.WriteLine("lastRow"); 
 
}
 

解决方案 »

  1.   

    建议把你的检查方法写到
    dataGridTextBoxColumn.TextBox 的leave 或textchange事件中去!
    如果你不是自定义的,请用下面方法:
    DataView tv = (DataView) dataGrid1.DataSource;
    DataGridTableStyle ts = new DataGridTableStyle();  
    ts.MappingName = tv.Table.TableName;
    int numCols = tv.Table.Columns.Count;
    int i=0;
    DataGridTextBoxColumn aColumnTextColumn;
    while (i < numCols) //重绘所有的列
    {
    aColumnTextColumn = new DataGridTextBoxColumn();
    if(i==0)
          aColumnTextColumn.TextBox.Leave += new System.EventHandler(Column1_Leave);
    aColumnTextColumn.MappingName = tv.Table.Columns[i].ColumnName;
    ts.GridColumnStyles.Add(aColumnTextColumn);  //增加一种自定义的column风格
    i ++;
    } dataGrid1.TableStyles.Add(ts);
      

  2.   

    private void Column1_Leave(object sender, System.EventArgs e)
    {
    MessageBox.Show(((TextBox)sender).Text);
    }
      

  3.   

    to wd_318(初学者.net) :因为比较急,我的代码全部贴出来了,希望给我指点一下
    private void MakeDataSet()
    {
    myDataSet = new DataSet("OrderDetail");
          
    DataTable OrderDetail = new DataTable("OrderDetail");

    DataColumn BarCode = new DataColumn("BarCode");
    DataColumn StyleID = new DataColumn("StyleID");
    DataColumn StyleName = new DataColumn("StyleName");
    DataColumn ColorID = new DataColumn("ColorID");
    DataColumn ColorName = new DataColumn("ColorName");
    DataColumn SpecID = new DataColumn("SpecID");
    DataColumn Qty = new DataColumn("Qty");

    OrderDetail.Columns.Add(BarCode);
    OrderDetail.Columns.Add(StyleID);
    OrderDetail.Columns.Add(StyleName);
    OrderDetail.Columns.Add(ColorID);
    OrderDetail.Columns.Add(ColorName);
    OrderDetail.Columns.Add(SpecID);
    OrderDetail.Columns.Add(Qty); myDataSet.Tables.Add(OrderDetail);    DataRow newRow;
    for(int i = 0; i < this.m_Order.m_arrOrderDetail.Count; i++)
    {
    newRow = OrderDetail.NewRow();
    Sytech.Sungod.PlanSystem.PlanSystemLib.BaseInfo.OrderDetail m_OrderDetail=(Sytech.Sungod.PlanSystem.PlanSystemLib.BaseInfo.OrderDetail)this.m_Order.m_arrOrderDetail[i];

    newRow["BarCode"]=m_OrderDetail.getAttribute("BarCode").propertyValue.ToString(); Sytech.Sungod.PlanSystem.PlanSystemLib.BaseInfo.Clothes m_Clothes=new Sytech.Sungod.PlanSystem.PlanSystemLib.BaseInfo.Clothes(frmPortal.m_DataManage);
    m_Clothes.Open(m_OrderDetail.getAttribute("BarCode").propertyValue.ToString()); newRow["StyleID"]=m_Clothes.StyleID;
    newRow["StyleName"]=m_Clothes.StyleName;
    newRow["ColorID"]=m_Clothes.ColorID;
    newRow["ColorName"]=m_Clothes.ColorName;
    newRow["SpecID"]=m_Clothes.SpecID;
    newRow["Qty"]=m_OrderDetail.getAttribute("Qty").propertyValue.ToString(); OrderDetail.Rows.Add(newRow);
    }
    }
      

  4.   

    private void BindDataGird()
    {
    try
    {
    DataGridTableStyle dts=new DataGridTableStyle();
    dts.MappingName="OrderDetail"; DataGridTextBoxColumn txtColumn=new DataGridTextBoxColumn();
    txtColumn.MappingName="BarCode";
    txtColumn.HeaderText="条码";
    txtColumn.Width=100;
    txtColumn.TextBox.Leave+=new System.EventHandler(BarCodeCheck);//此处监测条码的有效性
    dts.GridColumnStyles.Add(txtColumn); txtColumn = new DataGridTextBoxColumn();
    txtColumn.MappingName = "StyleID";
    txtColumn.HeaderText = "款号";
    txtColumn.Width = 100;
    txtColumn.ReadOnly=true;
    dts.GridColumnStyles.Add(txtColumn); txtColumn = new DataGridTextBoxColumn();
    txtColumn.MappingName = "StyleName";
    txtColumn.HeaderText = "款名";
    txtColumn.Width = 100;
    txtColumn.ReadOnly=true;
    dts.GridColumnStyles.Add(txtColumn); txtColumn = new DataGridTextBoxColumn();
    txtColumn.MappingName = "ColorID";
    txtColumn.HeaderText = "色号";
    txtColumn.Width = 100;
    txtColumn.ReadOnly=true;
    dts.GridColumnStyles.Add(txtColumn); txtColumn = new DataGridTextBoxColumn();
    txtColumn.MappingName = "ColorName";
    txtColumn.HeaderText = "色名";
    txtColumn.Width = 100;
    txtColumn.ReadOnly=true;
    dts.GridColumnStyles.Add(txtColumn); txtColumn = new DataGridTextBoxColumn();
    txtColumn.MappingName = "SpecID";
    txtColumn.HeaderText = "尺码";
    txtColumn.Width = 100;
    txtColumn.ReadOnly=true;
    dts.GridColumnStyles.Add(txtColumn); txtColumn = new DataGridTextBoxColumn();
    txtColumn.MappingName = "Qty";
    txtColumn.HeaderText = "数量";
    txtColumn.Width = 100;
    dts.GridColumnStyles.Add(txtColumn);
    this.dgDetail.TableStyles.Clear();
    this.dgDetail.TableStyles.Add(dts);
    }
    catch(Exception ex)
    {
    throw ex;
    }
    }private void SetUp()
    {
    MakeDataSet();
    this.dgDetail.SetDataBinding(myDataSet, "OrderDetail");
    BindDataGird();
    }
      

  5.   

    private void BarCodeCheck(object sender,System.EventArgs e)
    {
    try
    {
    int CurrentColumnIndex=this.dgDetail.CurrentCell.ColumnNumber;
    int CurrentRowIndex=this.dgDetail.CurrentCell.RowNumber; if(CurrentColumnIndex==0)
    {
    try
    {
    string m_BarCode=this.dgDetail[this.dgDetail.CurrentCell].ToString();
    if((m_BarCode=="")||(m_BarCode==null)) return ;
    if(Sytech.Sungod.PlanSystem.PlanSystemLib.Util.CheckValidate.CheckClothes(frmPortal.m_DataManage,m_BarCode)==true)
    {
    Sytech.Sungod.PlanSystem.PlanSystemLib.BaseInfo.Clothes m_Clothes=new Sytech.Sungod.PlanSystem.PlanSystemLib.BaseInfo.Clothes(frmPortal.m_DataManage);
    m_Clothes.Open(m_BarCode);
    this.dgDetail[CurrentRowIndex,1]=m_Clothes.StyleID;
    this.dgDetail[CurrentRowIndex,2]=m_Clothes.StyleName;
    this.dgDetail[CurrentRowIndex,3]=m_Clothes.ColorID;
    this.dgDetail[CurrentRowIndex,4]=m_Clothes.ColorName;
    this.dgDetail[CurrentRowIndex,5]=m_Clothes.SpecID;
    }
    else
    {
    ((DataGridTextBoxColumn)dgDetail.TableStyles[0].GridColumnStyles[dgDetail.CurrentCell.ColumnNumber]).TextBox.SelectAll();
    throw new Exception("输入的条码不正确!");
    }
    }
    catch(Exception ex)
    {
    throw ex;
    }
    }
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message,"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
    }
    }现在的问题是:用BarCodeCheck()检测时,数据总是不能最后输入的数据,而是在最后输入之前的数据,所以还是有问题,比如说我第一次输入112,其实是无效的,再次输入111,此时是有效的,所以现在触发时的数据,第一次为null,第二次为112,而111根本就没有检测,并且两次检测时的数据,都不是我要检测的数据,帮忙呀
      

  6.   

    string m_BarCode=this.dgDetail[this.dgDetail.CurrentCell].ToString();
    改成:
    ((TextBox)sender).Text;
      

  7.   

    因为textBox leave时,数据还没存入this.dgDetail.CurrentCell呢
    你试试吧
      

  8.   

    to wd_318(初学者.net,一时疏忽,谢谢!
    to cbspy,现在在哪?