asp.net vb GridView第五列减第6列等于第七列 怎么做第五列将此字段转为TemplateField textbox1这样出错
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then            e.Row.Cells(6).Text = CStr(CInt(e.Row.Cells(4).Text) - CInt(e.Row.Cells(5).Text))
        End If
    End Sub将此字段转为TemplateField textbox1
我怎么写谢谢大家帮一下。

解决方案 »

  1.   

    本帖最后由 net_lover 于 2011-01-14 14:26:30 编辑
      

  2.   

    出错Exception Error Message---- System.FormatException: 输入字符串的格式不正确。 在 System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) 在 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) 在 System.Int32.Parse(String s) 在 zyq.GridView1_RowDataBound(Object sender, GridViewRowEventArgs e) 位置 D:\Inetpub\sfgl\zyq.aspx.vb:行号 174 在 System.Web.UI.WebControls.GridView.OnRowDataBound(GridViewRowEventArgs e) 在 System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) 在 System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) 在 System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) 在 System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) 在 System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) 在 System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) 在 System.Web.UI.WebControls.DataBoundControl.PerformSelect() 在 System.Web.UI.WebControls.BaseDataBoundControl.DataBind() 在 System.Web.UI.WebControls.GridView.DataBind() 在 zyq.list() 位置 D:\Inetpub\sfgl\zyq.aspx.vb:行号 28 
      

  3.   

    注意检查x.Texte.Row.Cells(5).Text看是什么啊
    是数字吗?打印出来看看啊要学会调试啊
      

  4.   

    你好可以是可以了,我初进入页面的时候计算了一下 ,我在textbox1中输入数字后面的值怎么不变呢,我怎么做
      

  5.   

      If e.Row.RowType = DataControlRowType.DataRow Then  CInt(e.Row.Cells(6).Text)=
     CStr(CInt(e.Row.Cells(4).Text) - CInt(e.Row.Cells(5).Text))
      End If
      End Sub
      

  6.   

    我初进入页面的时候计算了一下 ,我在textbox1中输入数字后面的值怎么不变呢,我怎么做
      

  7.   

    你的问题根本就没有说清楚。你要的是在浏览器页面里面输入后,进行相加吧?只能采用JS。你为什么用GridView1_RowDataBound?
      

  8.   

    就是初进入页面的时候GridView1计算了,我如果在textbox1改变数字不会自动计算必须点更新才能计算
      

  9.   

    晕,为什么一定要绑定到gridview上才进行计算啊,为什么不计算好了再绑定到gridview中,当你textbox1改变数字是在后台重新绑定gridview,并刷新gridview不接结了吗
      

  10.   

    ...不会JS就在后台写。。TextBox的AutoPostBack设为true,双击进入TextBox1_textchange事件
    再次执行孟子大神的方法。。
      

  11.   

    给你一个完整的例子,直接复制下来就能运行看效果了,。你照着做<%@ Page Language="VB" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">  Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If Not Page.IsPostBack Then
          ' 创建示例数据绑定源
          Dim dt As New System.Data.DataTable()
          Dim dr As System.Data.DataRow
          dt.Columns.Add(New System.Data.DataColumn("UserId", GetType(System.Int32)))
          dt.Columns.Add(New System.Data.DataColumn("Num", GetType(System.Int32)))
          dt.Columns.Add(New System.Data.DataColumn("Price", GetType(System.Int32)))
          dt.Columns.Add(New System.Data.DataColumn("Total", GetType(System.String)))
          Dim rd As New System.Random()
          For i As Integer = 1 To 9
            dr = dt.NewRow()
            dr(0) = i
            dr(1) = i
            dr(2) = i
            dr(3) = ""
            dt.Rows.Add(dr)
          Next
          Dim dv As New System.Data.DataView(dt)
          GridView1.DataSource = dv
          GridView1.DataBind()
        End If
      End Sub
      Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
          Dim x As TextBox = CType(e.Row.FindControl("x"), TextBox)
          e.Row.Cells(3).Text = Int32.Parse(x.Text) + Int32.Parse(e.Row.Cells(2).Text)
        End If
      End Sub
    </script>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
      <title></title>
      <script>
        function Count(t) {
          var tr = t.parentNode;
          while (tr.tagName != "TR") tr = tr.parentNode;
          if (tr.tagName != "TR") return;
          txt = parseInt(t.value)
          if (isNaN(txt)) txt = 0;
          tr.cells[3].innerHTML = txt + parseInt(tr.cells[2].innerHTML)
        }
      </script>
    </head>
    <body>
      <form id="form1" runat="server">
      <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"
        AutoGenerateColumns="false">
        <Columns>
          <asp:BoundField DataField="UserId" />
          <asp:TemplateField>
            <ItemTemplate>
              <asp:TextBox ID="x" runat="server" Text='<%#Eval("Num") %>' onblur='Count(this)'></asp:TextBox>
            </ItemTemplate>
          </asp:TemplateField>
          <asp:BoundField DataField="Price" />
          <asp:BoundField DataField="Total" />
        </Columns>
      </asp:GridView>
      </form>
    </body>
    </html>