I have write this code to create a gridview with 3 columns
DataTable dt = new DataTable();
dt = new DataTable();
dt.Columns.Add("ID", typeof(int)).AutoIncrement = true;
dt.Columns.Add("Name"开发者_StackOverflow中文版, 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;
I would like to add in this code a textbox with the quantity.
When i give the quantity i want in another textbox to have the total.
for example 2*1.5=3
How can i do that?
My huge problem is that i dont know how to take the values of the 3rd column.The value 1.5 in this example.
If i've understood you correctly, you want a TextBox in the price-column and a textbox for the total price. You could use a TemplateColumn to show the price in a textbox and the footer to show the 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>
Sorry for VB.Net, i hope you see what i mean anyway, the importan part is in RowDataBound of the GridView:
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
To get the value from the third column you can iterate over that array:
GridView.Rows[index].Cells[2].Value.ToString());
Example:
String TextIn4thRow3rdColumn = myGridView.Rows[3].Cells[2].Value.ToString());
精彩评论