DataGrid1_ItemCommandif(e.CommandName=="Update")
{
TextBox input_id = null;
input_id = (TextBox)e.Item.FindControl("txtId");
if(Int32.Parse(input_id.Text)<=255&&Int32.Parse(input_id.Text)>=0)
....
}

解决方案 »

  1.   

    恩我也想问 是winform还是webform
      

  2.   

    http://blog.csdn.net/langmafeng/archive/2004/11/04/167164.aspx
      

  3.   

    喜欢用 FlexCell ,不喜欢用 DataGird,不方便。
      

  4.   

    借贵宝地问个相关问题:我想让datagrid的cell有这个功能,就是每输入完一个cell的内容,就对此内容进行处理(执行代码),能实现吗?
      

  5.   

    如果是textbox的话,用Validating事件正合适,可是datagrid的Validating事件只有在输入焦点离开整个datagrid的时候才触发,在焦点从一个cell移到另一个cell的时候并不触发。而在焦点从一个cell移到另一个cell的时候,有没有触发的事件?
      

  6.   

    CurrentCellChanged事件我试过了,不行,当焦点从一个cell移到另一个cell的时候,不能校验输入值是否符合要求!
      

  7.   

    我是这样做得:
    private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
    { DataGridCell currentCell =this.dataGrid1.CurrentCell;
    string currentCellText="";
    currentCellText=this.dataGrid1[currentCell.RowNumber,currentCell.ColumnNumber].ToString().Trim();
    //if(byte.Parse(currentCellText,System.Globalization.NumberStyles.AllowHexSpecifier)<
    if(currentCellText=="") 
    {
    MessageBox.Show("不能为空");
    this.dataGrid1[currentCell.RowNumber,currentCell.ColumnNumber]=0;
    }

    }
    但是不行!
      

  8.   

    可以试一下用javascript捕获键盘事件 
    我觉得可能实现机会大点
    DATAGRID好象没有提供输入触发时间 不过或许可以自定义事件
    只是一个思路 呵呵
      

  9.   

    currentcellchanged事件触发时,你已经处于一个新的CELL中,而不是刚才离开的那个焦点,所以这种方法是不行的。
    可是有什么办法,我也在考虑呢。。再找找看。。
      

  10.   

    brucenan999(布鲁斯南) ,说得很对,我也想到这个问题,所以我有一个类似的解决方案:就是currentcellchanged事件触发时,每次对所有的cell都检查一次,而不是去检测刚离开焦点的cell,就可以解决"已经处于一个新的CELL中,而不是刚才离开的那个焦点"的问题!当我始终觉得这种方法不是太好!
      

  11.   


    ----------------------
    www.517yn.net
      

  12.   

    要用绑定到DataGrid上的DataTable的ColumnChanged(或ColumnChanging)事件才可以实现这个功能.
    DataGrid本身可用的事件可的很少.
    也难怪,它本身主要是提供显示数据的
      

  13.   

    哈哈,终于被我搞定了:代码是继承了DATAGRID的自定义控件里写的。先设三个类成员变量:public int nPreX = -1;
    public int nPreY = -1;
    private bool bChangeCell = true;datagrid的下面这个事件里处理:
    private void DataGrid_CurrentCellChanged(object sender, System.EventArgs e)
    {
          if (bChangeCell ==true)
    {
    if(nPreX!=this.CurrentCell.RowNumber||nPreY!=this.CurrentCell.ColumnNumber)
    {
    if( nPreX==-1 && nPreY==-1 )
    {
        nPreX = this.CurrentCell.RowNumber;
        nPreY = this.CurrentCell.ColumnNumber;
        return;
    }
           //下面就是判断条件,可以根据具体情况进行修改。这里是定位每个CELL里的宽度必须不小于5
    if(this[nPreX,nPreY].ToString().Length < 5)
    {
    MessageBox.Show("Error");
    bChangeCell = false;
                      //这个操作会再一次触发本事件,所以在前面加了一个BOOL来判断
    this.CurrentCell = new DataGridCell(nPreX,nPreY);
    return;
    }
    else
    {
          nPreX = this.CurrentCell.RowNumber;
          nPreY = this.CurrentCell.ColumnNumber;
    }
    }
    }
    else
                bChangeCell =true;
    }
      

  14.   

    if(this[nPreX,nPreY].ToString().Length < 5)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    若要数字在0~255之间,就这样改:if(Check(nPreX)==false)
    ...
    bool private Check(int X)
    {
       try{
           int x = int.parse(this[nPreX,2].ToString());
                                   ~~~~~~~第三列
           if (x>0&&x<255)
             return true;
           else return false;
       }
       catch(FormatException e)
        {return false;}
    }
      

  15.   

    老大,我改了一下你的代码,不过有一个问题,你的代码和我的都存在,就是如何判断一个datagrid[i,j]是否存在。如果你的光标移动到了新的一条记录中(记录内容当然是null),不输入任何内容然后移开焦点,你的nPreX和nPreY指向的其实是一个不存在的cell,if(this[nPreX,nPreY].ToString().Length < 5)这一句就会报错。应该加一个判断,就是datagrid[nPreX,nPreY]是否存在,存在的话再执行相关代码。贴上我的代码,大家研究研究if(this.dataGrid1[nPreX,nPreY]!=null)
    {
    MessageBox.Show(this.dataGrid1[nPreX,nPreY].ToString());
    nPreX = this.dataGrid1.CurrentCell.RowNumber;
    nPreY = this.dataGrid1.CurrentCell.ColumnNumber;
    MessageBox.Show(nPreX.ToString(),nPreY.ToString());
    }
    else
    {
    nPreX = this.dataGrid1.CurrentCell.RowNumber;
    nPreY = this.dataGrid1.CurrentCell.ColumnNumber;
    }我的nPreX和nPreY的初始值和大虾不太一样,我的不是-1,是0
      

  16.   

    但是我的同样有问题,因为我要判断datagrid[nPreX,nPreY]是否存在,但是不能用if(this.dataGrid1[nPreX,nPreY]!=null)来判断,我不知道应该怎么判断。搞定了这个问题,应该就可以了。
      

  17.   

    判断存不存在应该和DATATABLE里的最大记录数比较的吧。
    但是我的程序运行时没有出现这种情况啊。
    我在最后一行新添的记录里触发事件,但是没有出现你的说的情况?
      

  18.   

    是这样啊,我的程序里并没有报错,还是一样的MESSAGEBOX提示输入错误。。
      

  19.   

    if(this.dataGrid2[nPreX,nPreY].ToString().Length < 5)
    会报出如下错误
    未处理的“System.IndexOutOfRangeException”类型的异常出现在 system.windows.forms.dll 中。其他信息: 索引 2 处没有值。这时候,我的datagrid里面有两条纪录,焦点在第三条记录上,该纪录默认为(null)。
    而nPreX的值为“2”,但是datagrid根本没有索引为2的纪录,所以报这个错误。