if(GridView1.Rows [i] .Cells [3] .Text.Equals(" credit"))无法正常工作

时间:2015-03-01 16:42:43

标签: c# asp.net gridview

我在其中有网格视图我在信用卡和借记列中有借方/贷方列和金额列,如果v_flag = d然后返回借方,则返回贷方html我写道:

<asp:TemplateField HeaderText="debit/credit">
            <ItemTemplate>
                 <asp:Label ID="debit" runat="server" Text='<%# (Eval("v_flag").ToString() =="d" ) ? "debit": "credit" %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:BoundField  DataField="v_amount" 
                                HeaderText="المبلغ" ItemStyle-Font-Size="Small"/>

并且它工作正常,但是当我试图仅将信用总额放在文本框中的gridview旁边时,借方总额仅在文本框中的gridview旁边 使用此代码:

   for (int i = 0; i < GridView1.Rows.Count; ++i)
            {
                if (GridView1.Rows[i].Cells[3].Text.Equals("credit"))
                {
                    sumCredit += Convert.ToDouble(GridView1.Rows[i].Cells[4].Text);
                    TxtCredit.Text = sumCredit.ToString();
                }
                else
                {
                    sumDebit += Convert.ToDouble(GridView1.Rows[i].Cells[4].Text);
                    TxtDebit.Text = sumDebit.ToString();
                }
            }

如果(GridView1.Rows [i] .Cells [3] .Text.Equals(“credit”))没有工作,它总是直接转到else语句

3 个答案:

答案 0 :(得分:0)

使用FindControl进入if-else

var debitTB = GridView1.Rows[i].FindControl("debit") as Label;
if (debitTB.Text.Equals("credit"))

同样foreach会让您的代码更清晰。

foreach(var row in GridView1.Rows){
    var debitTB = row.FindControl("debit") as Label;

答案 1 :(得分:0)

GridView1.Rows [i] .Cells [3] .Text.Equals(&#34; credit&#34;)就像你在调用单元格javascript函数的innerText一样。

所以改变

GridView1.Rows[i].Cells[3].Text.Equals("credit")

通过

var tempLabel= GridView1.Rows[i].FindControl("debit") as Label;
    if(tempLabel.Text.Equals("credit"))

答案 2 :(得分:0)

试试这个

for (int i = 0; i < GridView1.Rows.Count; ++i)
        {
      Label lblcredit = GridView1.Rows[i].FindControl("credit") as Label;
            if (lblcredit.Text == "credit")
            {
                sumCredit += Convert.ToDouble(GridView1.Rows[i].Cells[4].Text);
                TxtCredit.Text = sumCredit.ToString();
            }
            else
            {
                sumDebit += Convert.ToDouble(GridView1.Rows[i].Cells[4].Text);
                TxtDebit.Text = sumDebit.ToString();
            }
        }

希望得到这个帮助。