如何解决这个indexoutofrange异常plz帮助我 [英] How can I solve this indexoutofrange exception plz help me

查看:84
本文介绍了如何解决这个indexoutofrange异常plz帮助我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<asp:gridview id="Gridview1" runat="server" emptydatatext="Data is not available" onrowdatabound="Gridview1_RowDataBound" autogeneratecolumns="false">
    <columns>
        <asp:TemplateField HeaderText="ID">
            <itemtemplate>
                <asp:TextBox ID="txtID" runat="server" Text='<%#Eval("ID") %>' />
            
        
        <asp:TemplateField HeaderText="Quantity">
            <itemtemplate>
                <asp:TextBox ID="txtQuantity" runat="server" Text='<%#Eval("Quantity","0") %>' />
            
        
        <asp:TemplateField HeaderText="Rate">
            <itemtemplate>
                <asp:TextBox ID="txtRate" runat="server" Text='<%#Eval("Rate","0") %>' />
            
        
        <asp:TemplateField HeaderText="Total">
            <itemtemplate>
                <asp:TextBox ID="txtTotal" runat="server" Text='<%#Eval("Total","0") %>' />





CS Code

========



CS Code
========

protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        int index=0;

        //if (e.Row.RowType >= null)
        //{
            TextBox txtRate = (TextBox)Gridview1.Rows[index].Cells[1].FindControl("txtRate");
            TextBox txtQuantity = (TextBox)Gridview1.Rows[index].Cells[2].FindControl("txtQuantity");
            TextBox txtTotal = (TextBox)Gridview1.Rows[index].Cells[3].FindControl("Total");
            int R = int.Parse(txtRate.Text);
            int Q = int.Parse(txtQuantity.Text);
            int T = int.Parse(txtTotal.Text);

            T = Convert.ToInt32(R * Q);
            txtTotal.Text = T.ToString();
        //}
    }
}





我尝试过:



plz帮助我解决这个异常如何处理



What I have tried:

plz help me for this exception how can i handle

推荐答案

当然最好的方法是使用你的调试器 - 它会告诉你究竟是什么,在哪里出问题! !!

至于提示:你正在索引行和单元格,但你怎么能确定有行和单元格?
Of course the best way is to use your debugger - it will tell you exactly what and where the problem is!!!
As for a hint: You are indexing into Rows and Cells, but how can you be sure that there are rows and cells?


你应该试试类似这样的东西并删除Gridview1.Rows [index]代码:



you should try something like this and remove the Gridview1.Rows[index] code:

if ((e.Row.RowType == DataControlRowType.DataRow))
{
    TextBox txtRate = e.Row.FindControl("txtRate");
}





我假设您的索引变量出现问题。



I assume that there something going wrong with your index variable.


为什么不使用Google来查找异常实际意味着什么。然后你仔细看看你的代码来修复它的来源。



您甚至不必猜测异常的来源。每个异常都包含一个所谓的堆栈跟踪,它告诉您抛出异常的位置。
Why don't you use Google to look up what the exception actually means. And then you take a good look at your code to fix the line where it comes from.

You don't even have to guess where the exception comes from. Every exception contains a so called stack trace that tells you from where the exception was thrown.


这篇关于如何解决这个indexoutofrange异常plz帮助我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆