I have a gridview in vb.net wich provides a performance value of a list of business names for the previuous week, current week and next week as follows:
LOB W-1 W W+1
--------------------
AMEX 10 15 30
PPR 11 12 14
REM 12 11 10
What I need is for this values to be a hyperlink that opens another window based on the business name and the week to provide further detail. For example, if you want further datail of the value 15 for AMEX and the current week (W) then by pressing the value 15 a new window will open with information for AMEX for the current week.
I have the gridview created but I can not figure out how to do the hyperlinks. Any ideas?
See below code.
<asp:DropDownList ID="DateSelection" runat="server" Height="21px" Width="134px" >
</asp:DropDownList>
<asp:Button ID="Button_Update" runat="server" Text="UPDATE" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
GridLines="None" ForeColor="#333333">
<Columns>
<asp:BoundField DataField="LOB" HeaderText="LOB" />
<asp:BoundField DataField="W-1" HeaderText="W-1" />
<asp:BoundField DataField="W" HeaderText="W" />
<asp:BoundField DataField="W+1" HeaderText="W+1" />
</Columns>
</asp:GridView>
vb code
Dim dv_Groups As New System.Data.DataView
Dim dt_Groups As New System.Data.DataTable
dv_Groups = Group.Select(DataSourceSelectArguments.Empty)
dt_Groups = dv_Groups.ToTable()
Dim dv_Main As New System.Data.DataView
Dim dt_Main As New System.Data.DataTable
dv_Main = SQL_Main.Select(DataSourceSelectArguments.Empty)
dt_Main = dv_Main.ToTable()
Dim dt_Report As New DataTable()
dt_Report.Columns.Add("LOB", Type.GetType("System.String"))
dt_Report.Columns.Add("W-1", Type.GetType("System.String"))
dt_Report.Columns.Add("W", Type.GetType("System.String"))
dt_Report.Columns.Add("W+1", Type.GetType("System.String"))
Dim FindRow() As DataRow
Dim SearchText As String
Dim DateS As Date
Dim DateWeek As String
DateS = DateSelection.SelectedItem.Text
Dateweek = Format(DatePart(DateInterval.WeekOfYear, DateS))
For i As Integer = 0 To dt_Groups.Rows.Count - 1
dt_Report.Rows.Add()
dt_Report.Rows(i)(0) = dt_Groups.Rows(i)(0)
SearchText = "LOB like '" & Trim(dt_Report.Rows(i)(0)) & "%' And DateWeek = '" & Dateweek - 1 & "' "
FindRow = dt_Main.Select(SearchText)
dt_Report.Rows(i)(1) = FindRow(0).Item("A_SVL").ToString
SearchText = "LOB like '" & Trim(dt_Report.Rows(i)(0)) & "%' And DateWeek = '" & Dateweek & "' "
FindRow = dt_Main.Select(SearchText)
dt_Report.Rows(i)(2) = FindRow(0).Item("A_SVL").ToString
SearchText = "LOB like '" & Trim(dt_Report.Rows(i)(0)) & "%' And DateWeek = '" & Dateweek + 1 & "' "
FindRow = dt_Main.Select(SearchText)
dt_Report.Rows(i)(3) = FindRow(0).Item("S_SVL").ToString
Next
GridView1.DataSource = dt_Report
GridView1.DataBind()
Many Thanks
You can use a HyperLinkField or TemplateField to add links to your gridview, as shown here:
https://stackoverflow.com/a/8859655/849182
Related
When I click on the vote button the detail will insert but all the the page detail insert I want only perticular button click data should insert.
Code:
Protected Sub vote(sender As Object, e As EventArgs) {
//( lblName,lbl_id ,lbl_name,Label1 this is all reapeater field id )
Dim lblName As Label
Dim lbl_id As Label
Dim lbl_name As Label
Dim Label1 As Label
For Each item As RepeaterItem In rpt_path.Items
lblName = CType(item.FindControl("lblName"), Label)
lbl_id = CType(item.FindControl("lbl_id"), Label)
lbl_name = CType(item.FindControl("lbl_name"), Label)
Label1 = CType(item.FindControl("Label1"), Label)
con=("Connection String")
con.Open()
Dim command As New OleDbCommand("insert into CFC(C_E_No,Name,Class_id,Party_id)values(#C_E_No,#Name,#Class_id,#Party_id)", con)
command.Parameters.AddWithValue("(#C_E_No", CType(item.FindControl("lblName"), Label).Text)
command.Parameters.AddWithValue("#Name", CType(item.FindControl("lbl_id"), Label).Text)
command.Parameters.AddWithValue("#Class_id", CType(item.FindControl("lbl_name"), Label).Text)
command.Parameters.AddWithValue("#Party_id", CType(item.FindControl("Label1"), Label).Text)
command.ExecuteNonQuery()
con.Close()
Design
<asp:Label ID="lbl_iid" runat="server" Text="Label" Visible="false"></asp:Label>
</div>
<asp:Label ID="lbl_iname" runat="server" Text="Label" Visible="false"></asp:Label>
<asp:Label ID="lbl_icid" runat="server" Text="Label" Visible="false"></asp:Label>
<asp:Label ID="lbl_ipid" runat="server" Text="Label" Visible="false"></asp:Label>
Coding on Vote Button Click
Protected Sub vote(sender As Object, e As EventArgs)
Dim str As String = CType(sender, Button).CommandArgument
Session("vid") = str
MsgBox(Session("vid"))
Connection String
cmd = New OleDbCommand("select * from candi where C_E_No='" & str.ToString() & "' ")
ds = New DataSet
cmd.Connection = con
con.Open()
da = New OleDbDataAdapter(cmd)
MsgBox(cmd.CommandText)
da.Fill(ds, "candi")
lbl_iid.Text = ds.Tables(0).Rows(0)(0)
lbl_iname.Text = ds.Tables(0).Rows(0).Item(1).ToString()
lbl_icid.Text = ds.Tables(0).Rows(0).Item(2).ToString()
lbl_ipid.Text = ds.Tables(0).Rows(0).Item(4).ToString()
con.Close()
Connection String
cmd = New OleDbCommand("insert into CFC(Stu_E_No,C_E_No,Name,Class_id,Party_id)values('" & Session("stdid") & "','" & lbl_iid.Text & "','" & lbl_iname.Text & "','" & lbl_icid.Text & "','" & lbl_ipid.Text & "') ")
MsgBox(cmd.CommandText)
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
MsgBox("Succes")
I am trying to learn how to put totals on my gridview and have been searching for 2 days to find why mine don't work. The onrowdatabound event fires, but it doesn't recognize the rows as datarows, it only picks up the footer (I added a textbox and put the value in to make sure I wasn't losing it). I am very new to this and am probably doing something wrong that is very simple.
my codebehind is
Protected Sub WeeklyGridView_RowDataBound(ByVal sender As Object, _
ByVal e As GridViewRowEventArgs) Handles WeeklyGridView.OnRowDataBound
Dim appleTotal As Integer = 0
Dim orangeTotal As Integer = 0
Dim bananaTotal As Integer = 0
Dim pearTotal As Integer = 0
Dim grapeTotal As Integer = 0
Dim peachTotal As Integer = 0
Dim cherryTotal As Integer = 0
Dim pineTotal As Integer = 0
Dim totalTotal As Integer = 0
Dim ThedataType As String
ThedataType = e.Row.RowType.ToString
TextBox1.Text = ThedataType 'this always shows Footer
If e.Row.RowType = DataControlRowType.DataRow Then 'this never fires
appleTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Apple"))
orangeTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
"Orange"))
bananaTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
"Banana"))
pearTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
"Pear"))
grapeTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
"Grape"))
peachTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
"Peach"))
cherryTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
"Cherry"))
pineTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
"Pine"))
totalTotal += Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, _
"TOTAL"))
ElseIf e.Row.RowType = DataControlRowType.Footer Then 'this always fires
e.Row.Cells(0).Text = "Totals:"
' for the Footer, display the running totals
e.Row.Cells(3).Text = appleTotal.ToString("g")
e.Row.Cells(4).Text = orangeTotal.ToString("g")
e.Row.Cells(5).Text = bananaTotal.ToString("g")
e.Row.Cells(6).Text = pearTotal.ToString("g")
e.Row.Cells(7).Text = grapeTotal.ToString("g")
e.Row.Cells(8).Text = peachTotal.ToString("g")
e.Row.Cells(9).Text = cherryTotal.ToString("g")
e.Row.Cells(10).Text = pineTotal.ToString("g")
e.Row.Cells(11).Text = totalTotal.ToString("g")
e.Row.Font.Bold = True
End If
End Sub
The gridview is updated based on criteria selected by the user and is made visible and populated when they click on a search button.
The ridiculous code for the search button is: (and I know it's terrible but I'm trying)
Protected Sub SearchButton_Click(sender As Object, e As ImageClickEventArgs) Handles SearchButton.Click
Dim wherecls As String = "trees in ("
Dim whereFNcls As String = "fruitNumber between ("
Dim whereString As String = ""
Dim i As Integer = 0
Dim selectQry As String = "SELECT cast(trees as varchar(3)) as Trees, MIN(fruitnumber) AS FN_Start, MAX(fruitnumber) AS FN_End, COUNT(CASE WHEN fruitType = 'apple' THEN 1 ELSE NULL END) AS apple, COUNT(CASE WHEN fruitType = 'orange' THEN 1 ELSE NULL END) AS orange, COUNT(CASE WHEN fruitType = 'banana' THEN 1 ELSE NULL END) AS banana, COUNT(CASE WHEN fruitType = 'pear' THEN 1 ELSE NULL END) AS pear, COUNT(CASE WHEN fruitType = 'grape' THEN 1 ELSE NULL END) AS grape, COUNT(CASE WHEN fruitType = 'peach' THEN 1 ELSE NULL END) AS peach, COUNT(CASE WHEN fruitType = 'cherry' THEN 1 ELSE NULL END) AS cherry, COUNT(CASE WHEN fruitType = 'pine' THEN 1 ELSE NULL END) AS pine, COUNT(CASE when dcosg is not null THEN 1 ELSE NULL END) AS Total FROM fruitReport WHERE (orchard = #orchard) and "
orchardTextBox.Text = orchardDropDown.SelectedValue
' check if items selected in both listboxes
If trees_Listbox.Items.Count > 0 Then
If fruitminListBox.Items.Count > 0 Then
'cycle through items in fruitnum listbox to create an "in" clause for sql query
For Each item As ListItem In trees_Listbox.Items
whereString += String.Join(",", item) + ", "
Next
whereString = Left(whereString, Len(whereString) - 2) + ")"
selectQry += "(" + wherecls + whereString + ")"
whereFNcls = "(fruitNumber between "
For Each itemFNmin As ListItem In fruitminListBox.Items
'create a "between" clause for the min and max FN values entered by user.
whereOEcls += itemFNmin.Value + " and " + fruitmaxListBox.Items(i).ToString + ") or (fruitNumber between " '(fruitnumber between number and number) or
i += 1
Next
'trim off the last text portion of the whereOEcls
whereOEcls = Left(whereOEcls, Len(whereFNcls) - 25)
selectQry += " and (" + whereFNcls + ") GROUP BY trees ORDER BY trees"
fruityData.SelectCommand = selectQry
WeeklyGridView.Visible = True
Else
'see if FN is empty but trees is selected
For Each item As ListItem In trees_Listbox.Items
whereString += String.Join(",", item) + ", "
Next
whereString = Left(whereString, Len(whereString) - 2)
selectQry += wherecls + whereString + ") GROUP BY trees ORDER BY trees"
fruityData.SelectCommand = selectQry
WeeklyGridView.Visible = True
End If
Else
If fruitminListBox.Items.Count > 0 Then
'check if trees is empty but FN is selected
whereFNcls = "(fruitNumber between "
For Each itemFNmin As ListItem In fruitminListBox.Items
'create a "between" clause for the min and max FN values entered by user.
whereFNcls += itemFNmin.Value + " and " + fruitmaxListBox.Items(i).ToString + ") or (fruitNumber between " '(fruitnumber between number and number) or
i += 1
Next
whereFNcls = Left(whereFNcls, Len(whereFNcls) - 26)
selectQry += whereFNcls + ") GROUP BY trees ORDER BY trees"
fruityData.SelectCommand = selectQry
WeeklyGridView.Visible = True
Else
'if both are empty search only on orchard
selectQry = Left(selectQry, Len(selectQry) - 5) + " group by trees order by trees"
fruityData.SelectCommand = selectQry
WeeklyGridView.Visible = True
End If
End If
End Sub
And lastly, my gridview is...
<asp:GridView ID="WeeklyGridView" runat="server" HorizontalAlign="Center" DataSourceID="fruityData" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" Visible="False" ShowFooter="True" ShowHeaderWhenEmpty="True">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:BoundField DataField="trees" HeaderText="trees" SortExpression="trees" />
<asp:BoundField DataField="FN_Start" HeaderText="FN_Start" SortExpression="FN_Start" ReadOnly="True" />
<asp:BoundField DataField="FN_End" HeaderText="FN_End" SortExpression="FN_End" ReadOnly="True" />
<asp:BoundField DataField="apple" HeaderText="apple" SortExpression="apple" ReadOnly="True" />
<asp:BoundField DataField="orange" HeaderText="orange" SortExpression="orange" ReadOnly="True" />
<asp:BoundField DataField="banana" HeaderText="banana" SortExpression="banana" ReadOnly="True" />
<asp:BoundField DataField="pear" HeaderText="pear" SortExpression="pear" ReadOnly="True" />
<asp:BoundField DataField="grape" HeaderText="grape" SortExpression="grape" ReadOnly="True" />
<asp:BoundField DataField="peach" HeaderText="peach" SortExpression="peach" ReadOnly="True" />
<asp:BoundField DataField="cherry" HeaderText="cherry" SortExpression="cherry" ReadOnly="True" />
<asp:BoundField DataField="pine" HeaderText="pine" SortExpression="pine" ReadOnly="True" />
<asp:BoundField DataField="TOTAL" HeaderText="TOTAL" SortExpression="TOTAL" ReadOnly="True" />
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
<asp:SqlDataSource ID="fruityData" runat="server" ConnectionString="<%$ ConnectionStrings:fruityStuff %>" >
<SelectParameters>
<asp:ControlParameter ControlID="orchardTextBox" DefaultValue="theGrove" Name="orchard" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Any and all help would be greatly appreciated.
You don't call WeeklyGridView.DataBind() after you've assigned the SqlDataSource.SelectCommand. Try this:
fruityData.SelectCommand = selectQry
fruityData.DataBind()
I knew it was something stupid. I defined the variables inside of the event handler. I should have done it like this.
Dim appleTotal As Integer = 0
Dim orangeTotal As Integer = 0
Dim bananaTotal As Integer = 0
Dim pearTotal As Integer = 0
Dim grapeTotal As Integer = 0
Dim peachTotal As Integer = 0
Dim cherryTotal As Integer = 0
Dim pineTotal As Integer = 0
Protected Sub WeeklyGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles WeeklyGridView.OnRowDataBound
I am having issues with the databind method of the gridview control I could use some help on. Some background... I have a search interface with a variety of textboxes (First Name, Last Name, Year, Gender etc.) The user will enter some data and press the search button. At which time I am dynamically generating the SQL for the datasource and binding it to the gridview control.
Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
' Check Program Year
If ddYear.SelectedValue = 0 Then
lblStatus.Text = "Please select a program year!"
Exit Sub
Else
lblStatus.Text = ""
End If
ds1.SelectCommand = "SELECT [StudentId], [ProgramYear], [LastName], [Middle], [FirstName], [HighSchoolCode], [Sex], [DateOfBirth] FROM [Student] WHERE [ProgramYear] = " & ddYear.SelectedValue & ""
Dim useCase As Integer = 0
If tbFname.Text <> "" Then useCase = 1
If tbLname.Text <> "" Then useCase = 2
If tbFname.Text <> "" And tbLname.Text <> "" Then useCase = 3
If tbFname.Text <> "" And tbM.Text <> "" Then useCase = 4
If tbID.Text <> "" Then useCase = 5
If tbHSCode.Text <> "" Then useCase = 6
If tbLname.Text <> "" And ddGender.SelectedValue <> "0" Then useCase = 7
If tbHSCode.Text <> "" And ddGender.SelectedValue <> "0" Then useCase = 8
Select Case useCase
Case 1 'First Name
ds1.SelectCommand += " and ([FirstName] LIKE '%' + '" & tbFname.Text & "' + '%')"
Case 2 'Last Name
ds1.SelectCommand += " and ([LastName] LIKE '%' + '" & tbLname.Text & "' + '%')"
Case 3 'First and Last Name
ds1.SelectCommand += " and ([FirstName] LIKE '%' + '" & tbFname.Text & "' + '%') and ([LastName] LIKE '%' + '" & tbLname.Text & "' + '%')"
Case 4 'First and Middle
ds1.SelectCommand += " and ([FirstName] LIKE '%' + '" & tbFname.Text & "' + '%') and ([Middle] = '" & tbM.Text & "')"
Case 5 'Student ID
ds1.SelectCommand += " and ([StudentId] = '" & tbID.Text & "')"
Case 6 'HS Code
ds1.SelectCommand += " and ([HighSchoolCode] = '" & tbHSCode.Text & "')"
Case 7 'Last Name and Sex
ds1.SelectCommand += " and ([Sex] = '" & ddGender.SelectedValue & "' and ([LastName] LIKE '%' + '" & tbLname.Text & "' + '%'))"
Case 8 'HS Code and Sex
ds1.SelectCommand += " and ([HighSchoolCode] = '" & tbHSCode.Text & "' and [Sex] = '" & ddGender.SelectedValue & "')"
Case Else
ds1.SelectCommand += " order by [LastName] desc"
End Select
ds1.DataBind()
GridView1.DataBind()
End Sub
This works well however when I enable paging and apply the OnPageIndexChanging method as below I run into trouble.
Protected Sub gridview1_PageIndexChanging(sender As Object, e As GridViewPageEventArgs)
gridview1.PageIndex = e.NewPageIndex
GridView1.DataSource = ds1
gridview1.databind()
End Sub
What happens is that I click on the paging footer of the gridview to change pages and the gridview does not apply the new binding until I press the submit button again. I am not sure why this is happening any advice is appreciated.
Gridview:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" DataKeyNames="StudentId" DataSourceID="ds1" AllowPaging="True" PageSize="15">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>.
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student Id" InsertVisible="False" SortExpression="StudentId">
<ItemTemplate>
<a href="searchDetail.aspx?StudentID=<%# eval("StudentId") %>" >
<asp:Label ID="xyz" runat="server" Text='<%# Bind("StudentId") %>' /></a>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" />
<asp:BoundField DataField="Middle" HeaderText="Middle" SortExpression="Middle" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" />
<asp:BoundField DataField="Sex" HeaderText="Sex" SortExpression="Sex" />
<asp:BoundField DataField="HighSchoolCode" HeaderText="HS Code" SortExpression="HighSchoolCode" />
<asp:BoundField DataField="ProgramYear" HeaderText="Program Year" SortExpression="ProgramYear" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
DS1
<asp:SqlDataSource ID="ds1" runat="server" ConnectionString="<%$ ConnectionStrings:NMERITEXString %>"></asp:SqlDataSource>
What you need to do in gridview1_PageIndexChanging is setting gridview1.PageIndex and do the same thing as in btnSubmit_Click.
To avoid code repetition, move the content of btnSubmit_Click to a separate sub, let's call it LoadGrid
Private Sub LoadGrid()
' Check Program Year
If ddYear.SelectedValue = 0 Then
lblStatus.Text = "Please select a program year!"
Exit Sub
Else
lblStatus.Text = ""
End If
ds1.SelectCommand = "SELECT [StudentId], [ProgramYear], [LastName], [Middle], [FirstName], [HighSchoolCode], [Sex], [DateOfBirth] FROM [Student] WHERE [ProgramYear] = " & ddYear.SelectedValue & ""
Dim useCase As Integer = 0
If tbFname.Text <> "" Then useCase = 1
If tbLname.Text <> "" Then useCase = 2
If tbFname.Text <> "" And tbLname.Text <> "" Then useCase = 3
If tbFname.Text <> "" And tbM.Text <> "" Then useCase = 4
If tbID.Text <> "" Then useCase = 5
If tbHSCode.Text <> "" Then useCase = 6
If tbLname.Text <> "" And ddGender.SelectedValue <> "0" Then useCase = 7
If tbHSCode.Text <> "" And ddGender.SelectedValue <> "0" Then useCase = 8
Select Case useCase
Case 1 'First Name
ds1.SelectCommand += " and ([FirstName] LIKE '%' + '" & tbFname.Text & "' + '%')"
Case 2 'Last Name
ds1.SelectCommand += " and ([LastName] LIKE '%' + '" & tbLname.Text & "' + '%')"
Case 3 'First and Last Name
ds1.SelectCommand += " and ([FirstName] LIKE '%' + '" & tbFname.Text & "' + '%') and ([LastName] LIKE '%' + '" & tbLname.Text & "' + '%')"
Case 4 'First and Middle
ds1.SelectCommand += " and ([FirstName] LIKE '%' + '" & tbFname.Text & "' + '%') and ([Middle] = '" & tbM.Text & "')"
Case 5 'Student ID
ds1.SelectCommand += " and ([StudentId] = '" & tbID.Text & "')"
Case 6 'HS Code
ds1.SelectCommand += " and ([HighSchoolCode] = '" & tbHSCode.Text & "')"
Case 7 'Last Name and Sex
ds1.SelectCommand += " and ([Sex] = '" & ddGender.SelectedValue & "' and ([LastName] LIKE '%' + '" & tbLname.Text & "' + '%'))"
Case 8 'HS Code and Sex
ds1.SelectCommand += " and ([HighSchoolCode] = '" & tbHSCode.Text & "' and [Sex] = '" & ddGender.SelectedValue & "')"
Case Else
ds1.SelectCommand += " order by [LastName] desc"
End Select
ds1.DataBind()
GridView1.DataSource = ds1;
GridView1.DataBind()
End Sub
then call LoadGrid() inside btnSubmit_Click
Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
'set to first page
gridview1.PageIndex = 0
LoadGrid()
End Sub
and also call LoadGrid() in gridview1_PageIndexChanging. Don't forget to add Handles gridview1.PageIndexChanging
Protected Sub gridview1_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles gridview1.PageIndexChanging
gridview1.PageIndex = e.NewPageIndex
LoadGrid()
End Sub
Ok I have it working now...
I had to add the loadgrid() sub to the page load as follows. Thanks for your assistance!
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Page.IsPostBack Then
LoadGrid()
End If
End Sub
I have problem linking 2 datatables using a pID. It isn't an automated number. The thing is, I have to save the pID number into the database when I select an option in a drop down list. But the options shown are course names (Text).
After selecting from the drop down list, input text into a textbox and then click on a button to save both the pID number and description into the database. In 1 database table, it has the course name and pID number. The 2nd database table has the pID number and description(Save info into here).
I'm only able the save the selectedindex, which isn't the pID number.
.aspx.vb Code
Dim OdbcCon As New System.Data.Odbc.OdbcConnection
Dim cmd As New OdbcCommand
OdbcCon.Open()
cmd.Connection = OdbcCon
cmd.CommandText = " insert into StdBookInfo(pID,Description) values ('" & DropDownListStudent.SelectedIndex & "','" & TextBox1.Text & "') "
cmd.ExecuteNonQuery()
Code for drop down list:
sqlcmd3 = "select Course_code + ' - ' + package from StudentPackage order by pID asc "
sqlcmd4 = "select pID from StudentPackage order by Course_code,package asc "
cmd3.Connection = OdbcCon
cmd3.CommandText = sqlcmd3
DropDownListStudent.Items.Add("")
reader3 = cmd3.ExecuteReader
While reader3.Read
DropDownListStudent.Items.Add(reader3.Item(0).ToString)
End While
DropDownListStudent.SelectedIndex = -1
reader3.Close()
cmd3.Dispose()
cmd4.Connection = OdbcCon
cmd4.CommandText = sqlcmd4
reader4 = cmd4.ExecuteReader
I = 0
While reader4.Read
pID(I) = reader4.Item(0)
I = I + 1
End While
reader4.Close()
cmd4.Dispose()
OdbcCon.Close()
You need to set the Text and Value properties of each item of DropDownList, then use SelectedValue instead SelectedIndex.
<asp:DropDownList id="DropDownListStudent" Runat="Server">
<asp:ListItem Text="Item 1" Value="1"/>
<asp:ListItem Text="Item 2" Value="2"/>
<asp:ListItem Text="Item 3" Value="3"/>
<asp:ListItem Text="Item 4" Value="4"/>
<asp:ListItem Text="Item 5" Value="5"/>
</asp:DropDownList>
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", 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());