I am trying to insert (or bind ) the value from a textBox to a column in a Gridview (which is bound to a sqlDataSource) and then perform some simple math on several cells in a row and display the result in another column. I have added the columns in the
itemTemplate in the GridView, but am unable to insert the value (HC) and perform the math.
Any help would be greatly appreciated.
Thanks
<asp:SqlDataSource ID="SqlDataSource_GetRating" runat="server"
ConnectionString="<%$ ConnectionStrings:New_ASPNET %>"
SelectCommand="SELECT Golf_Player.PlayerID, Golf_Player.NameLast, Golf_Player.SeniorOrRegularTBox, Golf_Course_Teebox.Slope, Golf_Course_Teebox.Rating, Golf_Course.CourseID, Golf_Course_Teebox.CourseID AS Expr1, Golf_Course_Teebox.SeniorOrRegular, Golf_Course.CourseName FROM Golf_Course INNER JOIN Golf_Course_Teebox ON Golf_Course.CourseID = Golf_Course_Teebox.CourseID INNER JOIN Golf_Player ON Golf_Course_Teebox.SeniorOrRegular = Golf_Player.SeniorOrRegularTBox WHERE (Golf_Player.PlayerID = @PlayerID)">
<SelectParameters>
<asp:ControlParameter ControlID="DataList_Membership" Name="PlayerID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView_Handicap" runat="server" AutoGenerateColumns="False" OnRowDatabound="GridView_Handicap_RowDataBound"
DataSourceID="SqlDataSource_GetRating">
<Columns>
<asp:BoundField DataField="PlayerID" HeaderText="PlayerID"
InsertVisible="False" ReadOnly="True" SortExpression="PlayerID" />
<asp:BoundField DataField="NameLast" HeaderText="NameLast"
SortExpression="NameLast" />
<asp:BoundField DataField="Slope" HeaderText="Slope" SortExpression="Slope" />
<asp:BoundField DataField="Rating" HeaderText="Rating"
SortExpression="Rating" />
<asp:BoundField DataField="CourseID" HeaderText="CourseID"
InsertVisible="False" ReadOnly="True" SortExpression="CourseID" />
<asp:BoundField DataField="SeniorOrRegular" HeaderText="SeniorOrRegular"
SortExpression="SeniorOrRegular" />
<asp:BoundField DataField="CourseName" HeaderText="CourseName"
SortExpression="CourseName" />
<asp:TemplateField HeaderText="Index"> <ItemTemplate> <asp:Label ID="lblHI" runat="server" Text=""></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Handicap"> <ItemTemplate> <asp:Label ID="lblHandicap" runat="server" Text=""></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns>
</asp:GridView>
Protected Sub GridView_Handicap_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView_Handicap.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Cells(7).Text = lblHC.Text
e.Row.Cells(8).Text = e.Row.Cells(8) * (e.Row.Cells(2) / e.Row.Cells(3))
End If
End Sub
End Class
Protected Sub DetailsView_Handicap_DataBound(sender As Object, e As System.EventArgs) Handles DetailsView_Handicap.DataBound
If DetailsView_Handicap.CurrentMode = DetailsViewMode.ReadOnly Then
If DetailsView_Handicap.Rows.Count > 0 Then
Dim hc As String = DetailsView_Handicap.Rows(0).Cells(1).Text
lblHC.Text = hc
End If
End If
End Sub
you need to convert the math values to string so that can be displayed as a text value. and to perform math functions the values should be either double, int or decimal or any numeric values before. soo convert them to and forth
Thanks,
mayhap
Marked as answer by john1506 on Nov 27, 2012 04:30 PM
john1506
Member
319 Points
448 Posts
Insert textbox value into Gridview
Nov 26, 2012 03:55 PM|LINK
I am trying to insert (or bind ) the value from a textBox to a column in a Gridview (which is bound to a sqlDataSource) and then perform some simple math on several cells in a row and display the result in another column. I have added the columns in the itemTemplate in the GridView, but am unable to insert the value (HC) and perform the math.
Any help would be greatly appreciated.
Thanks
<asp:SqlDataSource ID="SqlDataSource_GetRating" runat="server" ConnectionString="<%$ ConnectionStrings:New_ASPNET %>" SelectCommand="SELECT Golf_Player.PlayerID, Golf_Player.NameLast, Golf_Player.SeniorOrRegularTBox, Golf_Course_Teebox.Slope, Golf_Course_Teebox.Rating, Golf_Course.CourseID, Golf_Course_Teebox.CourseID AS Expr1, Golf_Course_Teebox.SeniorOrRegular, Golf_Course.CourseName FROM Golf_Course INNER JOIN Golf_Course_Teebox ON Golf_Course.CourseID = Golf_Course_Teebox.CourseID INNER JOIN Golf_Player ON Golf_Course_Teebox.SeniorOrRegular = Golf_Player.SeniorOrRegularTBox WHERE (Golf_Player.PlayerID = @PlayerID)"> <SelectParameters> <asp:ControlParameter ControlID="DataList_Membership" Name="PlayerID" PropertyName="SelectedValue" Type="Int32" /> </SelectParameters> </asp:SqlDataSource> <asp:GridView ID="GridView_Handicap" runat="server" AutoGenerateColumns="False" OnRowDatabound="GridView_Handicap_RowDataBound" DataSourceID="SqlDataSource_GetRating"> <Columns> <asp:BoundField DataField="PlayerID" HeaderText="PlayerID" InsertVisible="False" ReadOnly="True" SortExpression="PlayerID" /> <asp:BoundField DataField="NameLast" HeaderText="NameLast" SortExpression="NameLast" /> <asp:BoundField DataField="Slope" HeaderText="Slope" SortExpression="Slope" /> <asp:BoundField DataField="Rating" HeaderText="Rating" SortExpression="Rating" /> <asp:BoundField DataField="CourseID" HeaderText="CourseID" InsertVisible="False" ReadOnly="True" SortExpression="CourseID" /> <asp:BoundField DataField="SeniorOrRegular" HeaderText="SeniorOrRegular" SortExpression="SeniorOrRegular" /> <asp:BoundField DataField="CourseName" HeaderText="CourseName" SortExpression="CourseName" /> <asp:TemplateField HeaderText="Index"> <ItemTemplate> <asp:Label ID="lblHI" runat="server" Text=""></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Handicap"> <ItemTemplate> <asp:Label ID="lblHandicap" runat="server" Text=""></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> Protected Sub GridView_Handicap_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView_Handicap.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then e.Row.Cells(7).Text = lblHC.Text e.Row.Cells(8).Text = e.Row.Cells(8) * (e.Row.Cells(2) / e.Row.Cells(3)) End If End Sub End Class Protected Sub DetailsView_Handicap_DataBound(sender As Object, e As System.EventArgs) Handles DetailsView_Handicap.DataBound If DetailsView_Handicap.CurrentMode = DetailsViewMode.ReadOnly Then If DetailsView_Handicap.Rows.Count > 0 Then Dim hc As String = DetailsView_Handicap.Rows(0).Cells(1).Text lblHC.Text = hc End If End If End Submayhap
Member
105 Points
35 Posts
Re: Insert textbox value into Gridview
Nov 26, 2012 09:00 PM|LINK
you need to convert the math values to string so that can be displayed as a text value. and to perform math functions the values should be either double, int or decimal or any numeric values before. soo convert them to and forth
Thanks,
mayhap
oned_gk
All-Star
31709 Points
6477 Posts
Re: Insert textbox value into Gridview
Nov 27, 2012 12:46 AM|LINK
Simply calculate it in sql
Use bound column to display HC field
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Insert textbox value into Gridview
Nov 27, 2012 03:32 AM|LINK
Hello,
Please use Cell.Text property: