大哥们来帮忙

yan9299 2008-02-03 12:14:41
我在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。请问各位该怎么改?
...全文
197 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
sknice 2008-02-04
  • 打赏
  • 举报
回复
马上要回家了,没心思看技术题了,帮你顶一下
libirds 2008-02-03
  • 打赏
  • 举报
回复
不是数字格式或为空、或者值超出int类型的范围都可能。
你可以根据实际情况判断吧。
yan9299 2008-02-03
  • 打赏
  • 举报
回复
真的很感谢,我觉得可能是真的有值不能转换过来,但我就不知道为什么会有不能转换的,晕
libirds 2008-02-03
  • 打赏
  • 举报
回复
int yewuValue=0;
try
{
yewuValue=int32.parse(e.Item.Cells[i].Text);
}
catch
{
yewuValue=0;
}
yewuCount+=yewuValue;
用这个试试吧。
yan9299 2008-02-03
  • 打赏
  • 举报
回复
喷血,还是说Int32.Parse(e.Item.Cells[i].Text);这句,输入字符串的格式不正确
libirds 2008-02-03
  • 打赏
  • 举报
回复
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);
}
要这么写的。

yan9299 2008-02-03
  • 打赏
  • 举报
回复
还是报错了,这一句Int32.TryParse(Item.Cells[i].Text,out yewuValue); 说名称“yewuValue”在类或命名空间中不存在
yan9299 2008-02-03
  • 打赏
  • 举报
回复
晕,我的代码里怎么Int32.后面点不出东西来啊?
libirds 2008-02-03
  • 打赏
  • 举报
回复
那是你的e.Item.Cells[i].Text有不能转换为数字的。可以这样处理。
int yewuValue=0;
Int32.TryParse(Item.Cells[i].Text,out yewuValue);
yewuCount+=yewuValue;
就不会报错了
yan9299 2008-02-03
  • 打赏
  • 举报
回复
我晕了,这下又说这句 yewuCount+=Convert.ToInt32(e.Item.Cells[i].Text); 输入字符串的格式不正确
libirds 2008-02-03
  • 打赏
  • 举报
回复
还是报错么?好了告诉一声。
libirds 2008-02-03
  • 打赏
  • 举报
回复
哦。对了。我忘了。应该这么写
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();
}
yan9299 2008-02-03
  • 打赏
  • 举报
回复
我知道啊,放在ItemCreated中同样报错啊
yan9299 2008-02-03
  • 打赏
  • 举报
回复
看来是要把赋值放在tb.Columns.Add(new DataColumn("回访总数",typeof(string)));这句之后执行才行,我要考虑怎么改代码,如果哪位高手知道还请帮忙
libirds 2008-02-03
  • 打赏
  • 举报
回复
不是放到ItemCommand事件中。是放到ItemCreated中。要看清楚了。是创建datagrid的时候做。
yan9299 2008-02-03
  • 打赏
  • 举报
回复
放到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中的代码,就是说运行出来我要求的那一列虽然有,但里面的值全为空。
还请高手继续指点

libirds 2008-02-03
  • 打赏
  • 举报
回复
就是说把ItemDataBound中的代码放到ItemCreated中去执行。ItemDataBound事件去掉就行了。
libirds 2008-02-03
  • 打赏
  • 举报
回复
自动生成的列得把ItemDataBound的代码去掉。放到ItemCreated中就可以了。
yan9299 2008-02-03
  • 打赏
  • 举报
回复
我是要找出e.Item.Cells这一列的数据啊
yan9299 2008-02-03
  • 打赏
  • 举报
回复
为什么后面要改成e.Item.Cells[i-3]?
加载更多回复(5)

62,039

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧