首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#中 DataGridView的单击表头的一个异常?

2012-12-14 
C#中 DataGridView的单击表头的一个错误???DataGridView的单击表头的时候出现了一个错误:开始的时候以为是

C#中 DataGridView的单击表头的一个错误???


DataGridView的单击表头的时候出现了一个错误:


开始的时候以为是 Dgv中没有数据项 所以有 NullReferenceException 异常:
函数:
private void DataGViewDetails_CellClick(object sender, DataGridViewCellEventArgs e)
        {
           
                m_iSWId =          Convert.ToInt32(DataGViewStation.CurrentRow.Cells[8].Value.ToString());--------(异常处  NullReferenceException)
           
        }


添加判断后:
        private void DataGViewDetails_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (DataGViewStation.RowCount != 0) // --- 修改了 ;为空的Dgv 时单击表头error 
            {
                m_iSWId = Convert.ToInt32(DataGViewStation.CurrentRow.Cells[8].Value.ToString());
            }
        }
后问题不出现了。



后来;在另一Dgv中出现同样的问题:这次Dgv中有显示数据:




      private void DataGViewDetails_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (DataGViewStation.RowCount != 0) // --- 修改了 ;为空的Dgv 时单击表头error 
            {
                m_iSWId = Convert.ToInt32(DataGViewStation.CurrentRow.Cells[8].Value.ToString());------error
            }
        }

现在不清楚 原因的所在了/

  想把单元表头的事件处理去掉,不知道怎么弄。????



    
[最优解释]


private void DataGViewDetails_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
    {
        m_iSWId = Convert.ToInt32(DataGViewStation.CurrentRow.Cells[8].Value.ToString());
    }
}

[其他解释]
Cells[8]这个如果是NULL,他tostring()就会出错啊~~~
[其他解释]
出现这个问题就是空值造成的,,你调试看下吧。。。

[其他解释]
   private void DataGViewDetails_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (DataGViewStation.RowCount != 0) // --- 修改了 ;为空的Dgv 时单击表头error 


            {
                m_iSWId = Convert.ToInt32(DataGViewStation.CurrentRow.Cells[8].Value.ToString());------error
            }
        }
==》 就是 cells[8]这个值为null 造成的,
在使用之前判断下
  

 private void DataGViewDetails_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if ((DataGViewStation.RowCount != 0)&&(DataGViewStation.CurrentRow.Cells[8].Value !=null)) // --- 修改了 ;为空的Dgv 时单击表头error 
            {
                m_iSWId = Convert.ToInt32(DataGViewStation.CurrentRow.Cells[8].Value.ToString());------error
            }
        }

[其他解释]
判断下是不是表头就好了,rowIndex > -1
[其他解释]
引用:
DataGridView的单击表头的时候出现了一个错误:


开始的时候以为是 Dgv中没有数据项 所以有 NullReferenceException 异常:
函数:
private void DataGViewDetails_CellClick(object sender, DataGridViewCellEventArgs e)
        {
           
 ……

DataGViewStation.CurrentRow.Cells[8].Value.ToString());
把  8  改成 列名试试!
[其他解释]
表头是ColumnHeader,点表头也可以触发这一事件,但表头只有HeaderText没有Value,所以点击表头时,它的Value是null

点击表头时e.RowIndex是-1,所以加上上面的限制,就可以保证只有点击单元格时,才执行下面的代码了
[其他解释]
里面的值是空,自然会报这个错误。
[其他解释]
看错了~
[其他解释]
第二张图片是这样的:


[其他解释]
引用:
Cells[8]这个如果是NULL,他tostring()就会出错啊~~~


不会出现是NULL的情况。
[其他解释]
你添加单击表头这个事件要干嘛?选中整行?
[其他解释]
引用:
引用:
Cells[8]这个如果是NULL,他tostring()就会出错啊~~~

不会出现是NULL的情况。

这种情况 一般都是值为null造成,
[其他解释]
引用:
你添加单击表头这个事件要干嘛?选中整行?


没有添加表头事件啊,就是添加了DataGridView中的相关事件了。不过感觉单击表头事件也触发相关事件,
[其他解释]
引用:
C# code?1234567private void DataGViewDetails_CellClick(object sender, DataGridViewCellEventArgs e){    if (e.RowIndex >= 0 && e.ColumnIndex >= 0)    {        m_iSWId = Convert.ToInt32(DataGVie……



最早代码就这点

 private void DataGViewDetails_CellClick(object sender, DataGridViewCellEventArgs e)
        {
             m_iSWId =            Convert.ToInt32(DataGViewStation.CurrentRow.Cells[8].Value.ToString());
       
        }

发现 当DataGridView 中没有显示项时,单击表头出现了: NULLReferenceException  异常,考虑可能是默认选中第一项,而第一项为空,导致的,于是添加判断:

     private void DataGViewDetails_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (DataGViewStation.RowCount != 0) // --- 修改了 ;为空的Dgv 时单击表头error 
             {
                m_iSWId = Convert.ToInt32(DataGViewStation.CurrentRow.Cells[8].Value.ToString());
              }
        }


添加这个判断后,单击空的DataGridView时,不再出现错误了。


可是这是单击表头,也触发这个事件,难道表头也是其中一行??
[其他解释]
引用:
C# code?1234567private void DataGViewDetails_CellClick(object sender, DataGridViewCellEventArgs e){    if (e.RowIndex >= 0 && e.ColumnIndex >= 0)    {        m_iSWId = Convert.ToInt32(DataGVie……


版主,你的方法是对的。可是不知道什么原因?求解释
[其他解释]
引用:
C# code?1234567private void DataGViewDetails_CellClick(object sender, DataGridViewCellEventArgs e){    if (e.RowIndex >= 0 && e.ColumnIndex >= 0)    {        m_iSWId = Convert.ToInt32(DataGVie……



另外一个问题出现了:当DataGridView中有数据时,单击表头

 private void DataGViewDetails_SelectionChanged(object sender, EventArgs e)  // Dgv1中select changed
        {
            if (m_iIsEmptyTable != 0)                             // 如果DataGirdViewstation中查询的表不为空,
            {
                m_iSWId = Convert.ToInt32(DataGViewStation.CurrentRow.Cells[8].Value.ToString());
                LoadDgvItems(ComposeSqlForItems());
                SetGgv(DataGViewItem);
            }
          
        }



另外一个事件出现错误了,同样的错误。
[其他解释]

引用:
引用:表头是ColumnHeader,点表头也可以触发这一事件,但表头只有HeaderText没有Value,所以点击表头时,它的Value是null

点击表头时e.RowIndex是-1,所以加上上面的限制,就可以保证只有点击单元格时,才执行下面的代码了

另外一个问题出现了:当DataGridView中有数据时,单击表头

……


在命令窗口中:


[其他解释]
引用:
表头是ColumnHeader,点表头也可以触发这一事件,但表头只有HeaderText没有Value,所以点击表头时,它的Value是null

点击表头时e.RowIndex是-1,所以加上上面的限制,就可以保证只有点击单元格时,才执行下面的代码了


另外一个问题出现了:当DataGridView中有数据时,单击表头

 private void DataGViewDetails_SelectionChanged(object sender, EventArgs e)  // Dgv1中select changed
        {
            if (m_iIsEmptyTable != 0)                             // 如果DataGirdViewstation中查询的表不为空,
            {
                m_iSWId = Convert.ToInt32(DataGViewStation.CurrentRow.Cells[8].Value.ToString());
                LoadDgvItems(ComposeSqlForItems());
                SetGgv(DataGViewItem);
            }
          
        }

出现同样的问题,这次是在有数据的时候发生的?
[其他解释]
  m_iSWId =Convert.ToInt32(DataGViewStation.CurrentRow.Cells[8].Value.ToString());
你这个把DataGViewStation里面的值强行转换为int类型,是获取不到值的,类型都不一样,强行转换自会显示为空值
[其他解释]
我也想加上这个判断:


[其他解释]
 private void DataGViewDetails_SelectionChanged(object sender, EventArgs e)  // Dgv1中select changed
        {


            //if (DataGridViewCellEventArgs(e).RowIndex >= 0 && DataGridViewCellEventArgs(e).ColumnIndex >= 0)
            //{
            //if ((e.RowIndex >= 0) && (e.ColumnIndex >= 0))
            if (DataGViewStation.CurrentRow != null)
            {
                if (m_iIsEmptyTable != 0)  // 如果DataGirdViewstation中查询的表不为空,
                {

                    m_iSWId = Convert.ToInt32(DataGViewStation.CurrentRow.Cells["ColuSwid"].Value.ToString());


                    LoadDgvItems(ComposeSqlForItems());
                    SetGgv(DataGViewItem);
                }
                //}
            }
       
          
        }


加了 红色 代码的判断,问题解决了,

但是我还是不清楚问题的原因,

感觉就是 DataGridView的表头是其中的一行,DataGirdView的事件对它起作用。
[其他解释]
神马情况啊啊啊哈哈哈

热点排行