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
}
}现在不清楚 原因的所在了/ 想把单元表头的事件处理去掉,不知道怎么弄。????
开始的时候以为是 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
}
}现在不清楚 原因的所在了/ 想把单元表头的事件处理去掉,不知道怎么弄。????
{
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
}
}
{
if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
{
m_iSWId = Convert.ToInt32(DataGViewStation.CurrentRow.Cells[8].Value.ToString());
}
}
没有添加表头事件啊,就是添加了DataGridView中的相关事件了。不过感觉单击表头事件也触发相关事件,
最早代码就这点 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时,不再出现错误了。
可是这是单击表头,也触发这个事件,难道表头也是其中一行??
把 8 改成 列名试试!
{
if (m_iIsEmptyTable != 0) // 如果DataGirdViewstation中查询的表不为空,
{
m_iSWId = Convert.ToInt32(DataGViewStation.CurrentRow.Cells[8].Value.ToString());
LoadDgvItems(ComposeSqlForItems());
SetGgv(DataGViewItem);
}
}另外一个事件出现错误了,同样的错误。
另外一个问题出现了:当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);
}
}出现同样的问题,这次是在有数据的时候发生的?
你这个把DataGViewStation里面的值强行转换为int类型,是获取不到值的,类型都不一样,强行转换自会显示为空值
{
//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的事件对它起作用。