C# 无法从我的gridview获取数据
在gridview中获取特定单元格的数据时遇到问题 我是这样做的:C# 无法从我的gridview获取数据,c#,asp.net,.net,gridview,C#,Asp.net,.net,Gridview,在gridview中获取特定单元格的数据时遇到问题 我是这样做的: double total = 0; for (int i = 0; i < GridFactures.Rows.Count; i++) total += Convert.ToDouble(GridFactures.Rows[i].Cells[6].Text.ToString()); lblTotalTTC.Text = "Montant total TTC : " + total; 双倍合计=0; 对于(in
double total = 0;
for (int i = 0; i < GridFactures.Rows.Count; i++)
total += Convert.ToDouble(GridFactures.Rows[i].Cells[6].Text.ToString());
lblTotalTTC.Text = "Montant total TTC : " + total;
双倍合计=0;
对于(int i=0;i
有问题的列在我的aspx文件中声明:
<asp:TemplateField HeaderText="Montant TTC">
<ItemTemplate>
<asp:Label ID="lblMontantTTC" runat="server" Text='<%#Eval("MontantTTC") %>'/>
</ItemTemplate>
</asp:TemplateField>
我肯定这是我想检查的第六栏。
我放置了一个中断符并GridFactures.Rows[i].Cells[6].Text.ToString()
始终包含“”
没别的了。。。
感谢您的帮助记住它是一个从零开始的索引,所以[6]表示单元格7如果我没有弄错的话(并且您的标签是单元格中的第一个/唯一的控件)- 您需要请求索引0处的控件,或者通过id找到它,然后请求.Text 像这样:
GridFactures.Rows[i].Cells[6].Controls[0].Text.ToString()
我知道为什么。我自己也遇到了同样的情况。您需要检查为空的字段,如果是,请先将其转换为零
total += (GridFactures.Rows[i].Cells[6].Text == "")? 0d : Convert.ToDouble(GridFactures.Rows[i].Cells[6].Text.ToString());
使用LINQ而不是循环怎么样
double mySum =
GridFactures.Rows
.Cast<GridViewRows>()
.Sum(row =>
Double.Parse(((Label)row.FindControl("lblMontantTTC")).Text));
double mySum=
网格制作。行
.Cast()
.Sum(行=>
Double.Parse(((Label)row.FindControl(“lblmontttc”)).Text));
如果您没有看到任何值,那么您的网格实际上还没有数据绑定,或者在网格上禁用了ViewState。这完全取决于您在页面生命周期中执行此计算的位置/时间。而不是此代码:
for (int i = 0; i < GridFactures.Rows.Count; i++)
total += Convert.ToDouble(GridFactures.Rows[i].Cells[6].Text.ToString());
for(int i=0;i
试试这个:
for (int i = 0; i < GridFactures.Rows.Count; i++)
{
Control ctrl = GridFactures.Rows[i].Cells[6].FindControl("lblMontantTTC");
if (ctrl != null)
{
Label lbl = ctrl as Label;
if (lbl != null)
{
total += Convert.ToDouble(lbl.Text);
}
}
}
for(int i=0;i
呵呵,一个经常犯的错误必须要确定;)不幸的是,这不起作用。实际上,我无法从gridview获取任何数据。出于测试目的,请尝试使用GridFactures.Rows[I].FindControl(“lblMontanttc”)方法,看看您是否可以通过这种方式找到它。再次感谢,但它也不起作用。。。我不理解它显示{text=“40.01”}(带有间谍)我需要获取此值,但无法访问实例的text属性使用此:total=total+Convert.ToDouble(((System.Web.UI.WebControls.Label)(GridFactures.Rows[i].FindControl(“lblmontttc”)).text);谢谢你的帮助