I have a problem where I don't know how to combine between Facebox and OnselectedIndex function in asp.net.
Here is my code.
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="False" class="table table-bordered data-table" OnSelectedIndexChanged = "OnSelectedIndexChanged" >
<columns>
<asp:TemplateField HeaderText="No." ItemStyle-HorizontalAlign="center">
<ItemTemplate>
<%# Container.DataItemIndex + 1%>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Request ID" DataField="reqID"><ItemStyle HorizontalAlign="Center" /></asp:BoundField>
<asp:BoundField HeaderText="Username" DataField="reqName" />
<asp:BoundField HeaderText="Department" DataField="reqDept" />
<asp:BoundField HeaderText="Categories" DataField="reqCategories" />
<asp:BoundField HeaderText="Type" DataField="reqType" />
<asp:BoundField HeaderText="Description" DataField="reqDesc" />
<asp:ButtonField Text="Select" CommandName="Select" ItemStyle-Width="150" />
<asp:TemplateField HeaderText="Action" itemstyle-horizontalalign="Center"><ItemTemplate><a runat="server" href="#info" rel="facebox" >More</a></ItemTemplate></asp:TemplateField>
</columns></asp:GridView>
Here is my facebox code :
<div id="info" style="display:none;">
<asp:GridView runat="server" ID="GridView2" AutoGenerateColumns="False" class="table table-bordered data-table">
<columns>
<asp:TemplateField HeaderText="No." ItemStyle-HorizontalAlign="center">
<ItemTemplate>
<%# Container.DataItemIndex + 1%>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Request ID" DataField="reqID" />
<asp:BoundField HeaderText="Verified By" DataField="verifiedBy" />
<asp:BoundField HeaderText="Verified Date" DataField="verifiedDate" />
<asp:BoundField HeaderText="Approved By" DataField="approvedBy" />
<asp:BoundField HeaderText="Approved Date" DataField="approvedDate" />
<asp:BoundField HeaderText="Accepted By" DataField="acceptedBy" />
<asp:BoundField HeaderText="Accepted Date" DataField="acceptedDate" />
<asp:BoundField HeaderText="Done By" DataField="doneBy" />
<asp:BoundField HeaderText="Done Date" DataField="doneDate" />
</columns></asp:GridView>
and here is my OnSelectedIndexChanged code :
Protected Sub OnSelectedIndexChanged(sender As Object, e As EventArgs)
Dim id As String
id = GridView1.SelectedRow.Cells(1).Text
conn.Open()
sqlstring = "SELECT * FROM ticketList where reqID = '" & id & "'"
cmd = New SqlCommand(sqlstring, conn)
dr = cmd.ExecuteReader
If dr.HasRows Then
dr.Close()
GridView2.DataSource = cmd.ExecuteReader
GridView2.DataBind()
End If
cmd.Dispose()
dr.Close()
conn.Close()
End Sub
Any idea when I click "More" then it will go to OnSelectedIndexChanged function and show data in my facebox?.
You need to add Handles GridView1.SelectedIndexChanged to the code:
Protected Sub OnSelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
Dim id As String
id = GridView1.SelectedRow.Cells(1).Text
conn.Open()
sqlstring = "SELECT * FROM ticketList where reqID = '" & id & "'"
cmd = New SqlCommand(sqlstring, conn)
dr = cmd.ExecuteReader
If dr.HasRows Then
dr.Close()
GridView2.DataSource = cmd.ExecuteReader
GridView2.DataBind()
End If
cmd.Dispose()
dr.Close()
conn.Close()
End Sub
Related
I have problem this gridview when I click on image button in gridview for delete row. I have this code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Username" HeaderText="Login Name">
<ItemStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="Question" HeaderText="Question">
<ItemStyle Width="200px" />
</asp:BoundField>
<asp:BoundField DataField="Answer" HeaderText="Answer">
<ItemStyle Width="150px" />
</asp:BoundField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="False" AutoPostback = "True"
CommandName="Select" ImageUrl="~/images/trash.png" Text="Select" OnClientClick="return confirm('Do you want to delete?');" />
</ItemTemplate>
</asp:TemplateField>
when i click trash image for delete any row it gives me error. Invalid postback or callback argument.
This is my vb.net code
Private Sub updatedata(ByVal sql As String)
connectdb()
cmd = New SqlCommand
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = sql
cmd.ExecuteNonQuery()
conn.Close()
End Sub
Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles GridView1.SelectedIndexChanged
Dim sql4 As String = "delete from login WHERE username='" & GridView1.SelectedRow.Cells(0).Text.Trim() & "' "
updatedata(sql4)
I'm having a problem with my dropdown lists in my child gridview displaying values. For whatever reason the first dropdown in the row works fine and displays the values from the database, the other ones don't display anything.
Below is the code for my gridviews:
<asp:GridView ID="GVAccounts" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333"
GridLines="Horizontal"
style="position: relative; margin-top: 10px">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="branch" HeaderText="Branch"
SortExpression="branch" />
<asp:BoundField DataField="no" HeaderText="Account"
SortExpression="account" />
<asp:TemplateField HeaderText="Name" SortExpression="name">
<EditItemTemplate>
<asp:TextBox ID="TextName" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelName" runat="server" Text='<%# Eval("name") + " " + Eval("surname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="type" HeaderText="Type" SortExpression="type" />
<asp:BoundField DataField="sub" HeaderText="Sub" SortExpression="sub" />
<asp:BoundField DataField="TotalAmount" HeaderText="Hold"
SortExpression="TotalAmount" />
<asp:BoundField DataField="loc" HeaderText="LOC" DataFormatString="{0:C}" SortExpression="loc" />
<asp:BoundField DataField="locstatus" HeaderText="LOC status"
SortExpression="locstatus" />
<asp:BoundField DataField="HoldCalc" HeaderText="OD/EX Amt" SortExpression="HoldCalc" />
<asp:BoundField DataField="odtimes" HeaderText="#OD" SortExpression="odtimes" />
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%">
<div id="gridChild" style="display: inline; position: relative; left: 15px; overflow: auto">
<asp:GridView ID="gvChildGrid" runat="server" AutoGenerateColumns="false"
BorderStyle="Double" BorderColor="#5D7B9D" Width="80%">
<HeaderStyle BackColor="#5D7B9D" Font-Bold="true" ForeColor="White" />
<RowStyle BackColor="#E1E1E1" />
<AlternatingRowStyle BackColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="true" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true"
oncheckedchanged="chkSelect_CheckedChanged" EnableViewState="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="effective" HeaderText="Effective"
HeaderStyle-HorizontalAlign="Left" >
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="desc_" HeaderText="Desc"
HeaderStyle-HorizontalAlign="Left" >
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="code" HeaderText="TC"
HeaderStyle-HorizontalAlign="Left" >
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="amount" HeaderText="Amount"
HeaderStyle-HorizontalAlign="Left" >
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="tracer" HeaderText="Cheq #"
HeaderStyle-HorizontalAlign="Left" >
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="empl" HeaderText="Empl"
HeaderStyle-HorizontalAlign="Left" >
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="balance" HeaderText="OD/EXT Amt"
HeaderStyle-HorizontalAlign="Left" >
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:TemplateField HeaderText="Note">
<ItemTemplate>
<asp:DropDownList ID="DropDownNote" runat="server"
onselectedindexchanged="DropDownNote_SelectedIndexChanged"
AutoPostBack="True">
<asp:ListItem></asp:ListItem>
<asp:ListItem>MWC</asp:ListItem>
<asp:ListItem>CBM</asp:ListItem>
<asp:ListItem>Return</asp:ListItem>
<asp:ListItem>TSF</asp:ListItem>
<asp:ListItem>OK NO S/C</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Note1">
<ItemTemplate>
<asp:DropDownList ID="ddNote2" runat="server"
onselectedindexchanged="ddNote2_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Selected="True">NSF</asp:ListItem>
<asp:ListItem>Funds Not Clear</asp:ListItem>
<asp:ListItem>Post Dated</asp:ListItem>
<asp:ListItem>Stale Dated</asp:ListItem>
<asp:ListItem>Stop Payment</asp:ListItem>
<asp:ListItem>Encoding Incorrect</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Branch">
<ItemTemplate>
<asp:DropDownList ID="ddBranch" runat="server" DataSourceID="BranchDataSource"
DataTextField="branch" DataValueField="branch" AutoPostBack="true">
</asp:DropDownList>
<asp:SqlDataSource ID="BranchDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ThirdPartyDataConnectionString %>"
SelectCommand="SELECT [branch] FROM [branch]"></asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Account">
<ItemTemplate>
<asp:TextBox ID="TextNo" runat="server" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type">
<ItemTemplate>
<asp:DropDownList ID="ddType" runat="server" DataSourceID="typeSource"
DataTextField="Type" DataValueField="Type" AutoPostBack="true">
</asp:DropDownList>
<asp:SqlDataSource ID="typeSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ThirdPartyDataConnectionString %>"
SelectCommand="SELECT [Type] FROM [DMDType]"></asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sub">
<ItemTemplate><asp:TextBox ID="TextSub" Width="25px" runat="server"></asp:TextBox></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" CssClass="GVFixedHeader" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
Below is the method that is having the issue with displaying:
Private Function CheckForSubmit() As Boolean
Try
conn = New SqlConnection(connectionString)
Dim cmdNote As New SqlCommand("select note, note1, noteBranch, noteAccountNo, noteType, noteSub, id from DmdOD where id = #id order by effective, time_, seqno_dmddmhi_dmhi", conn)
cmd = New SqlCommand("select submitted, id, effective FROM DmdOD where no = #no and entered = '" & ddDate.SelectedItem.Text & "' order by effective, time_, seqno_dmddmhi_dmhi", conn)
Dim objDBId As Object = 0
Dim id = 0
Dim objDate As Date
Dim dateStr As String = ""
If ddDate.SelectedIndex <> -1 Then
cmd = New SqlCommand("select submitted, id, entered FROM DmdOD where no = #no and entered = '" & ddDate.SelectedItem.Text & "' order by effective, time_, seqno_dmddmhi_dmhi", conn)
' cmd.Parameters.Add(New SqlParameter("effective", ddDate.SelectedItem.Text))
Else
cmd = New SqlCommand("select submitted, id, entered FROM DmdOD where no = #no order by effective, time_, seqno_dmddmhi_dmhi", conn)
End If
cmd.CommandType = CommandType.Text
Dim i = 0
For Each row As GridViewRow In GVAccounts.Rows
cmd.Parameters.Add(New SqlParameter("#no", row.Cells(1).Text))
Dim gv As GridView = DirectCast(row.FindControl("gvChildGrid"), GridView)
conn.Open()
Dim idReader As SqlClient.SqlDataReader = cmd.ExecuteReader()
'get ids in order
While (idReader.Read())
If Not objId.Contains(idReader.GetValue(1)) Then
objId.Add(idReader.GetValue(1))
End If
End While
conn.Close()
conn.Open()
Dim reader As SqlClient.SqlDataReader = cmd.ExecuteReader()
While (reader.Read())
objSubmit = reader.GetValue(0)
objDBId = reader.GetValue(1)
objDate = reader.GetValue(2)
dateStr = objDate.ToShortDateString
If dateStr = ddDate.SelectedItem.Text Then
Exit While
End If
End While
conn.Close()
If objSubmit = True And dateStr = ddDate.SelectedItem.Text Then
For Each r As GridViewRow In gv.Rows
cmdNote.Parameters.Add(New SqlParameter("#id", objId(i)))
Dim ddNote As Control = r.FindControl("DropDownNote")
Dim ddl As DropDownList = DirectCast(ddNote, DropDownList)
Dim ddNote2 As Control = r.FindControl("ddNote2")
Dim ddlNote As DropDownList = DirectCast(ddNote2, DropDownList)
ddlNote.Items.Insert(0, New ListItem(String.Empty, String.Empty))
Dim ddBranch As Control = r.FindControl("ddBranch")
Dim ddBranchBox As DropDownList = DirectCast(ddBranch, DropDownList)
ddBranchBox.Items.Insert(0, New ListItem(String.Empty, String.Empty))
Dim txtAccount As Control = r.FindControl("TextNo")
Dim txtAccountBox As TextBox = DirectCast(txtAccount, TextBox)
Dim ddType As Control = r.FindControl("ddType")
Dim ddTypeBox As DropDownList = DirectCast(ddType, DropDownList)
Dim txtSub As Control = r.FindControl("TextSub")
Dim txtSubBox As TextBox = DirectCast(txtSub, TextBox)
r.Cells(0).Enabled = False
r.Cells(8).Enabled = False
r.Cells(9).Enabled = False
r.Cells(10).Enabled = False
r.Cells(11).Enabled = False
r.Cells(12).Enabled = False
r.Cells(13).Enabled = False
'Display fields
gv.Columns(8).Visible = True
gv.Columns(9).Visible = True
gv.Columns(10).Visible = True
gv.Columns(11).Visible = True
gv.Columns(12).Visible = True
gv.Columns(13).Visible = True
ddBranchBox.Visible = True
txtAccount.Visible = True
ddTypeBox.Visible = True
txtSub.Visible = True
'Get Data for fields
conn.Open()
cmdNote.ExecuteNonQuery()
Dim readNotes As SqlClient.SqlDataReader = cmdNote.ExecuteReader()
readNotes.Read()
'Note
If IsDBNull(readNotes.GetValue(0)) Then
ddl.SelectedItem.Text = ""
Else
ddl.SelectedItem.Text = readNotes.GetValue(0)
End If
'Note1
If IsDBNull(readNotes.GetValue(1)) Then
'GVAccounts.Columns(12).Visible = False
ddlNote.Visible = False
ddlNote.SelectedItem.Text = ""
Else
'ddlNote.SelectedItem.Text = readNotes.GetValue(1)
ddlNote.SelectedItem.Selected = False
ddlNote.Items.FindByText(readNotes.GetValue(1)).Selected = True
End If
'Branch
If IsDBNull(readNotes.GetValue(2)) Then
ddBranchBox.Visible = False
ddBranchBox.SelectedItem.Text = ""
Else
ddBranchBox.SelectedItem.Text = readNotes.GetValue(2)
End If
'Account
If IsDBNull(readNotes.GetValue(3)) Then
txtAccountBox.Visible = False
txtAccountBox.Text = ""
Else
txtAccountBox.Text = readNotes.GetValue(3)
End If
'Type
If IsDBNull(readNotes.GetValue(4)) Then
ddTypeBox.Visible = False
ddTypeBox.SelectedItem.Text = ""
Else
ddTypeBox.SelectedItem.Text = readNotes.GetValue(4)
End If
'Sub
If IsDBNull(readNotes.GetValue(5)) Then
txtSubBox.Visible = False
txtSubBox.Text = ""
Else
txtSubBox.Text = readNotes.GetValue(5)
End If
conn.Close()
i += 1
cmdNote.Parameters.Clear()
Next
ButtonSubmit.Enabled = False
ButtonSave.Enabled = False
ButtonSaveTop.Enabled = False
End If
i = 0
'cmd.Parameters.Clear()
'cmdNote.Parameters.Clear()
objSubmit = False
dateStr = ""
cmd.Parameters.RemoveAt(0)
cmdNote.Parameters.Clear()
objId.Clear()
Next
If objSubmit = True Then
Return objSubmit
End If
Catch ex As Exception
LblErr.ForeColor = Drawing.Color.DarkRed
LblErr.Text = "CheckForSubmit - " & ex.Message
Finally
conn.Dispose()
End Try
Return Nothing
End Function
Edit: I should also note that when I step through the code I see that the dropdowns do in fact have values, yet nothing is being displayed for them on the gridview, I'm stumped.
Updated answer
I would compare drop down lists "note" and "note1"
e.g. note works but note1 doesn't.
The difference I can see between the 2 is that note has an empty list item. Try adding an empty list item to note 1.
Failing that, copy the working code from note and gradually replace it with the values from Note1, checking if/when it fails.
I am trying to set the rows colours in the parent grid view base on the data. For example in the coding, if the currency is USD then set the row to chocolate colour else set it to red but instead my program shows that its all in red even though there is currency USD data in the gridview.
heres my code behind,
Protected Sub gvUserInfo_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
e.Row.Cells(1).Visible = False
Dim currency As String = e.Row.Cells(3).Text
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("HedgingTestConnectionString").ConnectionString)
If (e.Row.RowType = DataControlRowType.DataRow) Then
For Each cell As TableCell In e.Row.Cells
If currency = "USD" Then
cell.ForeColor = Color.Chocolate
Else
cell.BackColor = Color.Red
Label1.Text = e.Row.Cells(3).Text
End If
Next
con.Open()
Dim gv As GridView = DirectCast(e.Row.FindControl("gvChildGrid"), GridView)
Dim ref As Integer = Convert.ToInt32(e.Row.Cells(1).Text)
Dim cmd As New SqlCommand("select * from TT where Reference_NO=" & ref, con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
con.Close()
gv.DataSource = ds
gv.DataBind()
End If
End Sub
Below is my gridview code,
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Hedging_ID,CCY" HeaderStyle-Font-Size="Medium" HeaderStyle-BackColor="SeaGreen" OnRowDataBound="gvUserInfo_RowDataBound" width="100%"
HorizontalAlign="Center" RowStyle-HorizontalAlign="Center" GridLines="None">
<Columns>
<asp:TemplateField ItemStyle-Width="20px">
<ItemTemplate>
<a href="JavaScript:divexpandcollapse('div<%# Eval("ref_ID")%>');">
<img id="imgdiv<%# Eval("ref_ID")%>" border="0" src="plus.png" />
</a>
</ItemTemplate>
<ItemStyle Width="20px"></ItemStyle>
</asp:TemplateField>
<asp:BoundField DataField="ref_ID" HeaderText="ID" SortExpression="ref_ID" />
<asp:BoundField DataField="Bank_Name" HeaderText="Bank" SortExpression="Bank_Name" />
<asp:BoundField DataField="CCY" HeaderText="Currency" SortExpression="CCY" />
<asp:BoundField DataField="Ref_Date" DataFormatString="{0:d}" HeaderText="Contract Date" SortExpression="Ref_Date" />
<asp:BoundField DataField="Ref_End_Date" DataFormatString="{0:d}" HeaderText="Maturity Date" SortExpression="Ref_End_Date" />
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%">
<div id="div<%# Eval("Hedging_ID")%>" style="display: none; position: relative; left: 15px; overflow: auto">
<asp:GridView ID="gvChildGrid" runat="server" AutoGenerateColumns="false" BorderStyle="Double" BorderColor="#df5015" GridLines="None" Width="700px" HorizontalAlign="Center">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
<RowStyle BackColor="#E1E1E1" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="White" />
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
<Columns>
<asp:BoundField DataField="Invoice_Number" HeaderText="Invoice Number" SortExpression="Invoice_Number" />
<asp:BoundField DataField="Payment_Date" DataFormatString="{0:d}" HeaderText="Payment Date" SortExpression="Payment_Date" />
<asp:BoundField DataField="Payment_Amount" DataFormatString="{0:c2}" HeaderText="Payment Amount" SortExpression="Payment_Amount" />
</Columns>
</asp:GridView>
</div>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="SeaGreen" Font-Size="Medium"></HeaderStyle>
<RowStyle HorizontalAlign="Center"></RowStyle>
</asp:GridView>
Change the condition like this
If currency = "USD" Then
cell.BackColor= Color.Chocolate
Else
cell.BackColor = Color.Red
Label1.Text = e.Row.Cells(3).Text
End If
May be thats because you have put "ForeColor"
I have checked all available articles and none seem to help me.
I have a GridView that is located inside a Repeater control. The data that populates the Gridview is dynamic and is grouped by a SectionID. I need for each repeated GridView to list the data rows grouped according to their SectionID.
How do I do this?
Thanks.
Here is what I have so far:
Public Sub GrabRepeaterData()
Dim connstr As String = ConfigurationManager.ConnectionStrings("MyString").ToString()
Dim ss As New SqlConnection(connstr)
Dim sqlStr As String = "SELECT SectionID, Name FROM Table1"
Dim selectCMD As New SqlClient.SqlCommand(sqlStr, ss)
Dim dt As New DataTable
Dim ds As New DataSet
Dim dataAdapter As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter
dataAdapter.SelectCommand = selectCMD
dataAdapter.Fill(dt)
selectCMD.Dispose()
dataAdapter.Dispose()
ss.Close()
ss.Dispose()
dt.Columns.Add("Column1")
dt.Columns.Add("Column2")
dt.Columns.Add("Column3")
dt.Columns.Add("Column4")
dt.Columns.Add("Column5")
dt.Columns.Add("Column6")
dt.Columns.Add("Column7")
dt.Columns.Add("Column8")
For Each row As DataRow In dt.Rows
Dim SectionID As String = ""
SectionID = row("SectionID")
Dim Column1 As String = ""
Dim Column2 As String = ""
Dim Column3 As Boolean
Dim Column4 As String = ""
Dim Column5 As String = ""
Dim Column6 As String = ""
Dim Column7 As Boolean
Dim Column8 As Boolean
Dim ProgConn As String = ""
ProgConn = ConfigurationManager.ConnectionStrings("MyString").ToString()
Dim ProgSqlQuery As New SqlConnection(ProgConn)
Dim ProgResults As New SqlCommand("SELECT [Column1], [Column2], [Column3], [Column4], [Column5], [Column6], [Column7], [Column8] FROM Table2 WHERE SectionID = #SectionID ORDER BY Column4 DESC", ProgSqlQuery)
ProgResults.Parameters.AddWithValue("#SectionID", SectionID).Value = SectionID
ProgSqlQuery.Open()
Dim rProg As SqlDataReader = ProgResults.ExecuteReader()
While rProg.Read()
If Not rProg("Column1").Equals(DBNull.Value) Then
Column1 = CStr(rProg("Column1"))
End If
If Not rProg("Column2").Equals(DBNull.Value) Then
Column2 = CStr(rProg("Column2"))
End If
If Not rProg("Column3").Equals(DBNull.Value) Then
Column3 = CStr(rProg("Column3"))
End If
If Not rProg("Column4").Equals(DBNull.Value) Then
Column4 = CStr(rProg("Column4"))
End If
If Not rProg("Column5").Equals(DBNull.Value) Then
Column5 = CStr(rProg("Column5"))
End If
If Not rProg("Column6").Equals(DBNull.Value) Then
Column6 = CStr(rProg("Column6"))
End If
If Not rProg("Column7").Equals(DBNull.Value) Then
Column7 = CStr(rProg("Column7"))
End If
If Not rProg("Column8").Equals(DBNull.Value) Then
Column8 = CStr(rProg("Column8"))
End If
End While
rProg.Close()
ProgResults.Dispose()
ProgSqlQuery.Close()
ProgSqlQuery.Dispose()
row("Column1") = Column1
row("Column2") = Column2
row("Column3") = Column3
row("Column4") = Column4
row("Column5") = Column5
row("Column6") = Column6
row("Column7") = Column7
row("Column8") = Column8
row.EndEdit()
dt.AcceptChanges()
Next
CustomInfoRepeater.DataSource = dt
CustomInfoRepeater.DataBind()
End Sub
Protected Sub CustomInfoRepeater_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles CustomInfoRepeater.ItemDataBound
If e.Item.ItemType = ListItemType.Item Then
Try
Dim grdVw As GridView = TryCast(e.Item.FindControl("CustomInfoGridView"), GridView)
grdVw.DataSource = DirectCast(e.Item.DataItem, DataTable).Rows
grdVw.DataBind()
Dim CustPanel As UpdatePanel = DirectCast(CustomInfoRepeater.Items(0).FindControl("CustomInfoPanel"), UpdatePanel)
CustPanel.Update()
Catch
End Try
End If
End Sub
Here is my HTML:
<asp:Repeater ID="CustomInfoRepeater" runat="server">
<ItemTemplate>
<div class="download-box3">
</h2>
<asp:UpdatePanel ID="CustomInfoPanel" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:PostBackTrigger ControlID="CustomInfoGridView" />
</Triggers>
<ContentTemplate>
<asp:GridView ID="CustomInfoGridView" runat="server" Width="100%" AutoGenerateColumns="False"
DataKeyNames="Column1" GridLines="None" RowStyle-Height="40px" EnableViewState="False"
CellPadding="4" ForeColor="#333333" RowStyle-VerticalAlign="Middle">
<Columns>
<asp:BoundField DataField="Column1" />
<asp:TemplateField ItemStyle-Width="35px" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:ImageButton ID="newIcon" runat="server" Width="31px" CssClass="myGridImage"
Visible="false" ImageUrl="images/new.png" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="35px" />
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="30px" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:ImageButton ID="DeleteButton" runat="server" AlternateText="Delete" ImageUrl="images/xmark.png"
OnClientClick="return confirm('Are you sure you want to delete this entry?')"
CommandArgument='<%# Eval("Column1") %>' CommandName="Remove" CssClass="myGridImage">
</asp:ImageButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="30px" />
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="50px" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:LinkButton ID="GenComUpdateButton" runat="server" CssClass="buttonsmall" Text="update"
CommandArgument='<%# Eval("Column1") %>' CommandName="GenComments"></asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Impact" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="30px"
ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:Image ID="statusIcon" runat="server" Width="20px" CssClass="myGridImage" Visible="false"
ImageUrl="" AlternateText="*" />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="50px" HeaderText="Files" ItemStyle-HorizontalAlign="Center"
ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:ImageButton ID="filesIcon" runat="server" Width="23px" CssClass="myGridImage"
CommandArgument='<%# Eval("Column3") %>' CommandName="Download" Visible="false"
ImageUrl="images/pdf.png" AlternateText="*" />
<asp:LinkButton ID="AttachAssetsBtn" runat="server" CssClass="buttonredsmall" Text="upload"
CommandArgument='<%# Eval("Column1") %>' CommandName="Uploads" Visible="false"></asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
</asp:TemplateField>
<asp:BoundField DataField="Column3" HeaderText="Summary" HeaderStyle-HorizontalAlign="Center"
ItemStyle-VerticalAlign="Middle">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle VerticalAlign="Middle" HorizontalAlign="Left" />
</asp:BoundField>
<asp:TemplateField ItemStyle-Width="50px" HeaderText="Reviewed" ItemStyle-HorizontalAlign="Center"
ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:Image ID="reviewedIcon" runat="server" Width="23px" CssClass="myGridImage" Visible="false"
ImageUrl="images/checkmark.png" AlternateText="*" />
<asp:CheckBox ID="reviewedCheckBox" runat="server" CssClass="inputtext" Visible="false"
AutoPostBack="true" OnCheckedChanged="GenComCheckUpdate" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
</asp:TemplateField>
<asp:BoundField DataField="Column4" HeaderText="Updated" HeaderStyle-HorizontalAlign="Center"
ItemStyle-VerticalAlign="Middle" ItemStyle-Width="75px">
<ItemStyle VerticalAlign="Middle" HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Column6" Visible="false" />
<asp:BoundField DataField="Column7" Visible="false" />
</Columns>
<EmptyDataTemplate>
<span class="data-none">No Comments have been added to this section.</span>
</EmptyDataTemplate>
<RowStyle CssClass="RowStyle" BackColor="#F7F6F3" ForeColor="#333333" />
<EmptyDataRowStyle CssClass="EmptyRowStyle" />
<PagerStyle CssClass="PagerStyle" BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle CssClass="SelectedRowStyle" BackColor="#E2DED6" Font-Bold="True"
ForeColor="#333333" />
<HeaderStyle CssClass="HeaderStyle" BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle CssClass="EditRowStyle" BackColor="#999999" />
<AlternatingRowStyle CssClass="AltRowStyle" BackColor="White" ForeColor="#284775" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:Repeater>
Thanks for your help!
What I understand from the code is you are merging two datasources into one. Then binding it to the Repeater and then fetching the datasource for GV from the repeater DataItem and binding it to the GridView.
What I would suggest is keep the datasources separate and do it in two steps:
1: Bind the repeater to just the SectionID i.e. data returned by SELECT SectionID, Name FROM Table1
2: In the Repeater ItemDataBound, get the SectionID and fetch data with the query you have in ProgResults. Then bind that to the GV.
I totally agree with gbs. Here's how you can do it:
In the markup, add a hidden field inside repeater, outside the updatepanel:
<asp:Repeater ID="CustomInfoRepeater" runat="server">
<ItemTemplate>
<asp:HiddenField ID="hdnSectionID" Value='<%# Eval("SectionID")%>' runat="server" />
<asp:Label ID="lblName" Text='<%# Eval("Name")%>' runat="server"></asp:Label>
<div class="download-box3">
</h2>
<asp:UpdatePanel ID="CustomInfoPanel" runat="server" UpdateMode="Conditional">
<%-- Rest of the markup goes here --%>
Rewrite GrabRepeaterData() sub only to populate the repeater:
Public Sub GrabRepeaterData()
Dim connstr As String = ConfigurationManager.ConnectionStrings("MyString").ToString()
Dim dt As New DataTable
Using conn As SqlConnection = New SqlConnection(connstr)
conn.Open()
Dim sqlStr As String = "SELECT SectionID, Name FROM Table1"
Dim cmd As New SqlClient.SqlCommand(sqlStr, conn)
Dim adapter As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter
adapter.SelectCommand = cmd
adapter.Fill(dt)
End Using
CustomInfoRepeater.DataSource = dt
CustomInfoRepeater.DataBind()
End Sub
In repeater's ItemDataBound, find the HiddenField, find it's value, find the GridView inside UpdatePanel, populate it this way:
Protected Sub CustomInfoRepeater_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles CustomInfoRepeater.ItemDataBound
If e.Item.ItemType = ListItemType.Item Then
Dim sectionID As Integer = 0
Dim hdnSectionID = TryCast(e.Item.FindControl("hdnSectionID"), HiddenField)
Dim CustomInfoPanel = TryCast(e.Item.FindControl("CustomInfoPanel"), UpdatePanel)
If (CustomInfoPanel IsNot Nothing AndAlso hdnSectionID IsNot Nothing AndAlso Integer.TryParse(hdnSectionID.Value, sectionID)) Then
Dim CustomInfoGridView = TryCast(CustomInfoPanel.FindControl("CustomInfoGridView"), GridView)
If (CustomInfoGridView IsNot Nothing) Then
Dim connstr As String = ConfigurationManager.ConnectionStrings("MyString").ToString()
Dim dt As New DataTable
Using conn As SqlConnection = New SqlConnection(connstr)
conn.Open()
Dim cmd As New SqlCommand("SELECT [Column1], [Column2], [Column3], [Column4], [Column5], [Column6], [Column7], [Column8] FROM Table2 WHERE SectionID = #SectionID ORDER BY Column4 DESC", conn)
cmd.Parameters.AddWithValue("#SectionID", sectionID)
Dim adapter As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter
adapter.SelectCommand = cmd
adapter.Fill(dt)
End Using
CustomInfoGridView.DataSource = dt
CustomInfoGridView.DataBind()
End If
End If
End If
End Sub
Hey all i am wondering if it was possible to evaluate the datafield each time it places a new record into a row?
my gridview code is this:
<asp:Panel ID="pnlGrid" runat="server">
<div class="left_main_container" style="margin-bottom:20px;">
<asp:GridView ID="grdView" runat="server" CssClass="GridViewStyle"
AutoGenerateColumns="False" GridLines="None" Width="100%">
<Columns>
<asp:BoundField DataField="id" Visible="False" />
<asp:BoundField DataField="theName" HeaderText="Name" />
<asp:BoundField DataField="status" HeaderText="Status" />
</Columns>
<RowStyle CssClass="RowStyle" />
<EmptyDataRowStyle CssClass="EmptyRowStyle" />
<PagerStyle CssClass="PagerStyle" />
<SelectedRowStyle CssClass="SelectedRowStyle" />
<HeaderStyle CssClass="HeaderStyle" />
<EditRowStyle CssClass="EditRowStyle" />
<AlternatingRowStyle CssClass="AltRowStyle" />
</asp:GridView>
</div>
</asp:Panel>
The field i need to evaluate is status. I need to find out if the status is yes or if its no. If its no then i need to make it a link for the user to be able to change it to a yes.
The gridview is populated by this code:
Dim objConn As MySqlConnection
Dim objCmd As MySqlCommand
objConn = New MySqlConnection(strConnString)
objConn.Open()
Dim strSQL As String
strSQL = "SELECT id, status, " & _
"CONCAT(' ', first_name, last_name) AS theName " & _
"FROM(builder_requests) " & _
"ORDER BY status, id DESC;"
Dim dtReader As MySqlDataReader
objCmd = New MySqlCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()
grdView.DataSource = dtReader
grdView.DataBind()
dtReader.Close()
dtReader = Nothing
objConn.Close()
objConn = Nothing
Any help would be great!
I like to use a TemplateField and a function in your code-behind to do this sort of thing.
I am assuming your status field is a string field not boolean but if not let me know and I can adjust the example.
Replace your BoundField with...
Markup:
<asp:TemplateField>
<ItemTemplate>
<%# Eval("status")%>
<asp:LinkButton ID="cmdChangeStatus" runat="server" CommandName="ChangeStatusToYes" CommandArgument='<%# Eval("id") %>'
Visible='<%# SetChangeStatusVisibility(Eval("status")) %>'>Change to Yes</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
And add code behind function...
Code:
Protected Function SetChangeStatusVisibility(status As Object) As String
Dim strStatus As String = status.ToString()
If strStatus = "no" Then
Return "True"
Else
Return "False"
End If
End Function
Then you can handle the GridView.RowCommand event to change the status value based on the id value in CommandArgument :)