2011-01-15 9 views
0

私は3列GridViewの列

DataTable dt = new DataTable(); 
dt = new DataTable(); 
dt.Columns.Add("ID", typeof(int)).AutoIncrement = true; 
dt.Columns.Add("Name", typeof(string)); 
dt.Columns.Add("Price(Grouch)/Hectares", typeof(float)); 
DataColumn[] keys = new DataColumn[2]; 
keys[0] = dt.Columns["ID"]; 
dt.PrimaryKey = keys; 
dt.Rows.Add("1", "Seaside Location", 1.5); 

Session[key] = dt; 
return dt; 

でのGridViewを作成するには、このコードを書いていてテキストボックスを乗算するにはどうすれば量と、このコードでテキストボックスを追加したいと思います。

私は、別のテキストボックスに必要な数量を合計するとよいでしょう。

たとえば2 * 1.5 = 3

どうすればいいですか?

私の巨大な問題は、私は第3列の値を取る方法を知らないということです。この例では値1.5です。

答えて

0

私が正しくあなたを理解している場合は、価格のテキストボックスをしたいです-columnと合計価格のテキストボックス。 TemplateColumnを使用すると、テキストボックスに価格を表示し、フッターにtotalpriceを表示することができます。

ASPX:

<asp:GridView ID="GridView1" runat="server" ShowFooter="true" AutoGenerateColumns="false" > 
     <Columns> 
     <asp:BoundField HeaderText="ID" DataField="Name" /> 
     <asp:BoundField HeaderText="Name" DataField="Name" /> 
     <asp:TemplateField HeaderText="Price(Grouch)/Hectares"> 
      <ItemTemplate> 
       <asp:TextBox ID="TxtPrice" runat="server"></asp:TextBox> 
      </ItemTemplate> 
      <FooterTemplate> 
       <asp:TextBox ID="TxtTotal" runat="server" Text="0"></asp:TextBox> 
      </FooterTemplate> 
     </asp:TemplateField> 
     </Columns> 
    </asp:GridView> 

VB.Netのため申し訳ありませんが、私はimportan部分はGridViewののRowDataBoundである、あなたは私はとにかく何を意味するか見願っています。このヘルプ!!!

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
     If Not IsPostBack Then 
      BindGrid() 
     End If 
    End Sub 

    Private Sub BindGrid() 
     Dim dt As New DataTable 
     dt.Columns.Add("ID", GetType(Int32)).AutoIncrement = True 
     dt.Columns.Add("Name", GetType(String)) 
     dt.Columns.Add("Price(Grouch)/Hectares", GetType(Single)) 
     dt.PrimaryKey = New DataColumn() {dt.Columns("ID")} 
     Dim newRow As DataRow = dt.NewRow 
     newRow("ID") = 1 
     newRow("Name") = "Seaside Location" 
     newRow("Price(Grouch)/Hectares") = 1.5 
     dt.Rows.Add(newRow) 
     newRow = dt.NewRow 
     newRow("ID") = 2 
     newRow("Name") = "City Location" 
     newRow("Price(Grouch)/Hectares") = 7.9 
     dt.Rows.Add(newRow) 
     Me.GridView1.DataSource = dt 
     Me.GridView1.DataBind() 
    End Sub 

    Private totalPrice As Single = 0 
    Private Sub GridRowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound 
     If e.Row.RowType = DataControlRowType.DataRow Then 
      Dim row As DataRow = DirectCast(e.Row.DataItem, DataRowView).Row 
      Dim txtPrice As TextBox = DirectCast(e.Row.FindControl("TxtPrice"), TextBox) 
      Dim price As Single = DirectCast(row("Price(Grouch)/Hectares"), Single) 
      txtPrice.Text = price.ToString 
      totalPrice = totalPrice + price 
     ElseIf e.Row.RowType = DataControlRowType.Footer Then 
      Dim txtTotal As TextBox = DirectCast(e.Row.FindControl("TxtTotal"), TextBox) 
      txtTotal.Text = totalPrice.ToString 
     End If 
    End Sub 
+0

感謝私は解決策を見つけるために – dali1985

+0

@ user576998:2ヶ月後? –

+0

いいえ、アカウントに問題があったため、ここにログインする日数が多かったです!もう一度ありがとうございます! – dali1985

0

あなたはその配列を反復処理することができます3番目の列から値を取得するには:

GridView.Rows[index].Cells[2].Value.ToString()); 

例:

String TextIn4thRow3rdColumn = myGridView.Rows[3].Cells[2].Value.ToString()); 
関連する問題