我在DataGrid()里有:
DataTable tb = Class.DataBindToDS(Sqlstr2);
tb.Columns.Add(new DataColumn("回访总数",typeof(string)));
dataGrid.DataSource = tb;
dataGrid.DataBind();
然后有 private void dataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType  ==  ListItemType.Item ||e.Item.ItemType  ==  ListItemType.AlternatingItem) 

int   yewuCount=0; 

for(int   i=3;i <dataGrid.Columns.Count-5;i++) 

yewuCount+=Convert.ToInt32(e.Item.Cells[i].Text); 

e.Item.Cells[dataGrid.Columns.Count-5].Text=yewuCount.ToString(); 
}
}但报错:指定的参数已超出有效值的范围。参数名:   index 
我在跟踪后发现dataGrid.Columns.Count的值竟是0。请问各位该怎么改?

解决方案 »

  1.   

    加个判断试试
    if(dataGrid.Columns.Count>=5)
    {
      int yewuCount=0;
      ...
    }
      

  2.   

    肯定有数据被读取啊
    因为我把dataGrid_ItemDataBound()事件注销后
    就可以显示出表格和里面的数据啊加判断条件显然是没用的,因为dataGrid.Columns.Count调试的时候值是 0  
      

  3.   

     private void dataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
            {
                if(e.Item.ItemType  ==  ListItemType.Item ||e.Item.ItemType  ==  ListItemType.AlternatingItem) 
                { 
                    int   yewuCount=0; 
                    
                    for(int   i=3;i <dataGrid.Columns.Count-5;i++) 
                    { 
                        yewuCount+=Convert.ToInt32(e.Item.Cells[i].Text); 
                         e.Item.Cells[i-3].Text=yewuCount.ToString();                 } 
                               }
            }
      

  4.   

    为什么后面要改成e.Item.Cells[i-3]?
      

  5.   

    我是要找出e.Item.Cells这一列的数据啊
      

  6.   

    自动生成的列得把ItemDataBound的代码去掉。放到ItemCreated中就可以了。
      

  7.   

    就是说把ItemDataBound中的代码放到ItemCreated中去执行。ItemDataBound事件去掉就行了。
      

  8.   

    放到ItemCreated中依然报同样的错
    我调试了下,发现调试到dataGrid.DataSource = tb;时我看tb.cloums.count的值时就报错了。而如果没有
    tb.Columns.Add(new DataColumn("回访总数",typeof(string)));这句就显示tb.cloums.count=9。
    而当我把ItemDataBound中的代码放在dataGrid_ItemCommand中执行的时候,到上面那步显示tb.cloums.count=10
    了,就是说不报错了,但是却不执行ItemCommand中的代码,就是说运行出来我要求的那一列虽然有,但里面的值全为空。
    还请高手继续指点
      

  9.   

    不是放到ItemCommand事件中。是放到ItemCreated中。要看清楚了。是创建datagrid的时候做。
      

  10.   

    看来是要把赋值放在tb.Columns.Add(new   DataColumn("回访总数",typeof(string)));这句之后执行才行,我要考虑怎么改代码,如果哪位高手知道还请帮忙
      

  11.   

    我知道啊,放在ItemCreated中同样报错啊
      

  12.   

    哦。对了。我忘了。应该这么写
                if(e.Item.ItemType  ==  ListItemType.Item ||e.Item.ItemType  ==  ListItemType.AlternatingItem) 
                { 
                    int   yewuCount=0; 
                    
                    for(int   i=3;i <e.Item.Cells.Count-5;i++) 
                    { 
                        yewuCount+=Convert.ToInt32(e.Item.Cells[i].Text); 
                    } 
                    e.Item.Cells[e.Item.Cells.Count-5].Text=yewuCount.ToString(); 
                }
      

  13.   

    我晕了,这下又说这句 yewuCount+=Convert.ToInt32(e.Item.Cells[i].Text); 输入字符串的格式不正确
      

  14.   

    那是你的e.Item.Cells[i].Text有不能转换为数字的。可以这样处理。
    int yewuValue=0;
    Int32.TryParse(Item.Cells[i].Text,out yewuValue);
    yewuCount+=yewuValue;
    就不会报错了
      

  15.   

    晕,我的代码里怎么Int32.后面点不出东西来啊?
      

  16.   

    还是报错了,这一句Int32.TryParse(Item.Cells[i].Text,out   yewuValue); 说名称“yewuValue”在类或命名空间中不存在
      

  17.   

    int   yewuValue=0; 这句加在前面没有?
                                    for(int       i=3;i   <e.Item.Cells.Count-5;i++)   
                                    {   
                                            int   yewuValue=0; 
    Int32.TryParse(Item.Cells[i].Text,out   yewuValue); 
    yewuCount+=yewuValue; (e.Item.Cells[i].Text);   
                                    }   
    要这么写的。
      

  18.   

    喷血,还是说Int32.Parse(e.Item.Cells[i].Text);这句,输入字符串的格式不正确
      

  19.   

     int yewuValue=0;   
    try
    {
      yewuValue=int32.parse(e.Item.Cells[i].Text);
    }
    catch
    {
     yewuValue=0;
    }
    yewuCount+=yewuValue;  
    用这个试试吧。
      

  20.   

    不是数字格式或为空、或者值超出int类型的范围都可能。
    你可以根据实际情况判断吧。